Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Digit value type #9

Open
bartelink opened this issue Aug 21, 2014 · 0 comments
Open

Digit value type #9

bartelink opened this issue Aug 21, 2014 · 0 comments

Comments

@bartelink
Copy link
Contributor

As covered in various conversations, a lot about Digit:-

[<Struct>]
type Digit =
    val value: int
    new(value) =
        if value < 0 || value > 9 then 
            invalidArg "value" "A digit value should be from 0 to 9" 
        { value = value }
    override this.ToString() = string this.value 

bothers me:-

  1. having 8 lines (out of 37) in the UL (and if it was a single line in some cases it can sit beside another single line def allowing us to convey relatedness so really it's 9)
  2. using attributes (esp in UL)
  3. having code in the UL
  4. Because its a struct, you can still Digit () to bypass the ctor [which is OK in this case as 0 happens to be valid]
  5. The digit vs Digit clashes it causes are a mess

The following lipstick makes it a bit nicer:

let inline between min max value = 
    if value >= min && value <= max then 
        sprintf "A digit value should be between %A and %A" min max 
        |> invalidArg "value" 
    value

type Digit = struct 
    val value:int
    new value = { value = value |> between 0 9 } 
    override this.ToString() = string this.value end

If you felt strongly about perf requirements enough to prematurely optimize (or a profiler proof said do it, I think I'd probably make it the following one liner:

type Digit = struct val value:int; new value = { value = value |> between 0 9 } end

(I'd remove the reliance on ToString() (there's still %A and your Json converters)

I'd still be annoyed to have to place the between helper somewhere (though in practice there's always an Operators.fs in any 'real' project :D (And then to have to fight about a better name)

Final conclusion: I'd prefer

type Digit = Digit of int

thoughts ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant