hubFS: THE place for F#

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

Operator overloading with default indexer

Last post 06-20-2006, 17:46 by dsyme. 4 replies.
Sort Posts: Previous Next
  •  06-14-2006, 18:39 356

    Operator overloading with default indexer

    Does F# allow operator overloading? Here is an example of code that is generating an error.

    type ITest = interface
        abstract Item : int -> float with get
        abstract Item : string -> float with get
    end

    Error: Duplicate definition of value in 'ITest.Item.get.2'.

    Is there a different way to do this in F#?

    Thanks in advance.

    Regards

    Chris
  •  06-16-2006, 17:53 361 in reply to 356

    Re: Operator overloading with default indexer

    Overloading by number of arguments is supported without issue.

    Arbitrary member overloading by type is supported through an thinly documented feature called "OverloadID".  F# simply asks that a unique name be given for each overload that is ambiguous by name and number of arugments - strictly speacking this should not be necessary once a full type signature has been given, but it is necessary in the case of inferred signatures. 

    At some point we will simply require that these sorts of overloads are given full type signatures, when the OverloadID will then become redundant and ignored, and possibly deprecated in some v2.0 of the language.

    Anyway, to use this, simply add the OverloadID attribute to each member, giving a different qualifying name in each case.  These names must be identical if you also give a signature. 

    type ITest = interface
        [<OverloadID("Item.get.Int")>]

        abstract Item : int -> float with get
        [<OverloadID("Item.get.string")>]
        abstract Item : string -> float with get
    end

    And from prim-types.fsi:



    /// Adding the OverloadID attribute to a member permits it to
    /// be part of a group overloaded by the same name and arity.  The string
    /// must be a unique name amongst those in the overload set.  Overrides
    /// of this method, if permitted, must be given the same OverloadID,
    /// and the OverloadID must be specified in both signature and implementation
    /// files if signature files are used.
    type OverloadIDAttribute =
      class
        inherit System.Attribute
        val v: string
        new : string -> OverloadIDAttribute
      end

  •  06-19-2006, 20:29 368 in reply to 361

    Re: Operator overloading with default indexer

    Hi Don,

    I am doing something wrong because I cannot get it to work. I pasted your example into VS2005 and it complained about a syntax error. Here is another simple example:

    open System
    open Microsoft.FSharp

    type Test = class
        val name : string
        // Constructors
        [OverloadID("Test.new.string")]
        new(nm:string) = {name = nm; }
        [OverloadID("Test.new.Int")]
        new(idx:int) = { name = Int32.to_string(idx); }
    end

    I keep getting a syntax error. It's got to be something simple.

    Thanks for patiently answering all my questions.

    Regards

    Chris
  •  06-19-2006, 21:27 369 in reply to 368

    Re: Operator overloading with default indexer

    Chris,

    The syntax error is in the attribute declaration.  It should read:

    open System
    open Microsoft.FSharp

    type Test = class
        val name : string
        // Constructors
        [<OverloadID("Test.new.string")>]
        new(nm:string) = {name = nm; }
        [<OverloadID("Test.new.Int")>]
        new(idx:int) = { name = Int32.to_string(idx); }
    end

    All attributes in F# are wrapped in [< >] unlike the C# style of [ ] (and appears to be a simple typo in Don's code above).

    Let me know if that doesn't work as it executes cleanly like so:

    let ab = new Test("hi");;
    let ac = new Test(400);;

    creating objects ab and ac with names = "hi" and "400" (as strings) respectively.

    ---O

     

  •  06-20-2006, 17:46 373 in reply to 369

    Re: Operator overloading with default indexer

    Apologies!  Normally I make a point of type-checking and running code before I post it!

    I'll edit the post above, just to avoid confusing later readers.

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