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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 

/// 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

By on 6/16/2006 5:53 PM ()

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

By on 6/19/2006 8:29 PM ()

Chris,

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

1
2
3
4
5
6
7
8
9
10
11
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:

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

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

---O

By on 6/19/2006 9:27 PM ()

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.

By on 6/20/2006 5:46 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper