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
Description
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:-
- 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)
- using attributes (esp in UL)
- having code in the UL
- Because its a
struct
, you can stillDigit
() to bypass the ctor [which is OK in this case as0
happens to be valid] - The
digit
vsDigit
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
Labels
No labels