Why doesn't F # support extending the system type with its type abbreviation?
For example, if I try to expand int
is int
not a true type name, this code will fail:
type int with
member this.IsEven = this % 2 = 0
Instead, I have to use System.Int32
:
type System.Int32 with
member this.IsEven = this % 2 = 0
//test
let i = 20
if i.IsEven then printfn "'%i' is even" i
Why can't I use the type abbreviation int
?
+3
source to share
1 answer
I think glib's answer is mostly right - functions are not implemented by default. However, another possible reason is that type abbreviations are limited, so it is not immediately obvious if the scope of the abbreviation affects the scope of the expansion. For example:
module X =
type t = int
module Y =
// imagine we could hypothetically do this
type X.t with
member i.IsEven = i % 2 = 0
open Y
fun (x:System.Int32) -> x.IsEven // should this compile even though t isn't in scope here?
+6
source to share