Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Digit value type #9

Open
Open
@bartelink

Description

@bartelink

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions