4 ms·
In my language-ext library it's pretty trivial: class Hours : NewType<Hours, double> { public Hours(double x) : base(x) {} } It gives you:
by louthy 7y ago
In my language-ext library it's pretty trivial:
class Hours : NewType<Hours, double> {
public Hours(double x) : base(x) {}
}
It gives you:
* Equality operators, Equals, and IEquatable
* Ordering operators, CompareTo, and IComparable
* GetHashCode, ToString
* Map, Select, Bind, SelectMany, Fold, ForAll, Exists, Iter
* Explict conversion operator to convert from the NewType to the internal wrapped type
* Serialisation
* Hours.New(x) constructor function (often useful when used with other methods that take first-class functions).
It's even possible with the more complex variants to provide predicates that will run on construction to constrain the values going in.
There's also NumType for integer numeric types like int, long, etc. and FloatType for floating-point numeric types like float, double, etc. They give additional functionality by default.
Also, units of measure [4]:
Length x = 10*inches;
Area y = x * x;
Time t = 10*sec;
[1] NewType - https://github.com/louthy/language-ext/tree/master/LanguageExt.Core/DataTypes/NewType https://github.com/louthy/language-ext/tree/master/LanguageE...
[2] NumType - https://github.com/louthy/language-ext/tree/master/LanguageExt.Core/DataTypes/NumType https://github.com/louthy/language-ext/tree/master/LanguageE...
[3] FloatType - https://github.com/louthy/language-ext/tree/master/LanguageExt.Core/DataTypes/FloatType https://github.com/louthy/language-ext/tree/master/LanguageE...
[4] Units of Measure - https://github.com/louthy/language-ext/tree/master/LanguageExt.Core/DataTypes/UnitsOfMeasure https://github.com/louthy/language-ext/tree/master/LanguageE...
- thelazydogsback 7y agoOf course F# has units-of-measure built-in
- louthy 7y agoErm, no it doesn't. It has the ability to define units of measure, but the language doesn't have UoM built-in. There are UoM in FSharp.Core [1], so it's the same as including it from a library. Obviously F#’s first class support for UoM is better than my implementation, but for all intents and purposes the behaviour is the same. [1] https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/SI.fs https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSha...
- yawaramin 7y agoMost people would consider 'distributed with F#' as being equivalent to 'built in to F#'. Actually it's even cooler that the SI units are in the standard library rather than baked into the language.
- thelazydogsback 7y agoIt's pretty clear what I meant - that F# has support of UoM's. Where the canned ones live is is neither here nor there. I have seen your lib before - nice work.