Enter extensions with a common constraint without raising an error, and don't do what you might expect

Consider the following code snippet using F # 4.0, .NET 4.6:

type X<'T> = Y of 'T

type XS = X<string>
type XI = X<int>

type X<'T when 'T :> string> with
    static member X = 2
    static member take (s: 'T) = s

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module XS =
    let foo = 10
    let create s = XS.Y s
    let test = XI.take 2    // expected only string allowed, XI.take should not exist

I would expect the type extension type X<'T when 'T :> string> withto either be respected (in this case, it would mean an error because it is stringsealed or restricted 'Tas string), or to enhance the syntax, an error.

In addition, I can use the following syntax, which will be a syntax error in the definition of the usual type (without with):

type X<'T> when 'T :> string with
    static member X = 2
    static member take (s: 'T) = s

I assume the restriction is simply ignored in the extension. Is it for design? Or should it work, and if so, how?

, , , , ( , ).

+4
1

, , :

+3

Source: https://habr.com/ru/post/1657575/


All Articles