Proposed Duck Typing & Type Augmenttaion

  • Wednesday, July 11, 2012 6:54 PM
     
     

    Hello,

    I have just noticed a strange behavior when using duck typing and type augmentation. Here is the code:

    namespace Test
     module M1 =

        type System.Double with
            static member doit (v:double) = v


        let inline duck (v : ^a) = (^a : (static member doit: ^a -> ^a) (v))

        let (v:System.Double) = 2.8

        System.Double.doit(v)  // ok
        
       // type checker complains
        duck v

     module M2 =

        type A = CA
            with
              static member doit (v:A) = v  

        type B = CB

        // Both ok
        M1.duck CA
        A.doit CA        

     module M3 =

        type M2.B
            with
              static member doit (v:M2.B) = v  
        
        // type checker complains
        M1.duck M2.CB
        M2.B.doit (M2.CB)  // ok

    Has anyone an explanation to this behavior?

    Thank you

    Regards

    Jean-Christophe

All Replies

  • Wednesday, July 11, 2012 7:09 PM
    Moderator
     
     Proposed

    Static member constraints only work when there are corresponding real .NET members on the type being used.  However, extension methods result in static methods on another type (even though you call them as if they were defined on the original type), so they can't be used with static member constraints.  In your examples, System.Double and M2.B don't really have "doit" methods so methods that statically require a "doit" method on those types won't work.

    • Proposed As Answer by Carsten Koenig Thursday, July 12, 2012 5:03 AM
    •