hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Referencing generic member from within the same type

Last post 09-21-2008, 3:03 by uds. 2 replies.
Sort Posts: Previous Next
  •  09-03-2008, 8:23 6835

    Referencing generic member from within the same type

    Hi all

    I can define a generic method in the type as in the following example:


    type A() =
        member a.X(x) = x
           
    let a = new A()
    let x1 = a.X(12)
    let x2 = a.X("qw")



    But as soon as I "lock" type of parameter in X by referencing it from another method, it's not anymore generic.
    Here is an example:


    type A() =
        member a.X(x) = x
        member a.Y() =
            (a.X(23),
             a.X("abc"))   // This expression has type string but is here used with type int.
       
    let a = new A()
    let x1 = a.X(12)
    let x2 = a.X("qw")    // This expression has type  string but is here used with type int.



    How is it possible to keep method X generic if it's referenced from within the same type?
    Some magic with type parameters maybe?


    Interesting, changing X to "let" definition works fine:

    type A() =
        let X x = x
        member a.Y() =
            (X 23, X "abc")




    thanks!

    - Sergey

  •  09-19-2008, 15:41 7143 in reply to 6835

    Re: Referencing generic member from within the same type

    Give the member a complete signature:

        member a.X<'a>(x:'a):'a = x

  •  09-21-2008, 3:03 7158 in reply to 7143

    Re: Referencing generic member from within the same type

    thank you for your help

    I've tried to add a member template parameter, but never tried a full signature...

     

    - Sergey

     

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems