You cannot overload let-bound functions. You can only overload methods (class members).

By on 6/4/2009 3:10 PM ()

Hello, it may be a stupid question (if so please forgive me), but is there a reason that let-bindings cannot be overloaded, but they can be shadowed ?

In the following code the type inference is able to determine that 'anyrecord' is type "MostLocalRecord" because it fits the type checks and is the last defined type. It infers that 'somerecord' is type "LeastLocalRecord" because that is the only possibility. Couldn't the same hold true for let bindings; either the last defined ones work out, or some previous ones should be used instead.

type LeastLocalRecord = { name: string; }
let f {LeastLocalRecord.name=name} = {LeastLocalRecord.name=name}

type MostLocalRecord = { name: string; }
// This 'overload' to function f cannot be bound to the name 'f',
// the formal error text is "Duplicate definition of value 'f'"
//let f {MostLocalRecord.name=name} = {MostLocalRecord.name=name}

let anyrecord = { name = "Anybody" }
let somerecord = f {name= "Somebody"}

I agree that it might (in some cases) lead to type inference that is not always clear as day to the programmer, but often a strategic type annotation here and there would do the trick (just as we are used to), I would guess.

Best regards,
Robert

By on 11/16/2009 4:13 PM ()

You cannot overload let-bound functions. You can only overload methods (class members).

Then, I hope there is a way to write this as one function - a function which can take either one of the types listed and internally somehow differentiates among them. For reference types I can use

1
match value with | :? string as s 

but what can I do for value types?

By on 6/4/2009 3:31 PM ()

Then, I hope there is a way to write this as one function - a function which can take either one of the types listed and internally somehow differentiates among them. For reference types I can use

1
match value with | :? string as s 

but what can I do for value types?

Just the same; the function can take type 'obj', and even value types like 'int' are objects (boxing will happen) and this will just work.

By on 6/4/2009 7:11 PM ()

Just the same; the function can take type 'obj', and even value types like 'int' are objects (boxing will happen) and this will just work.

I would assume there is a performance penalty because of boxing - am I right?

By on 6/8/2009 5:40 AM ()

You could use a Choice type (either the predefined ones or a custom union type).

By on 6/8/2009 7:05 AM ()

How about:

match box 1 with | :? int as i -> true ...

By on 6/4/2009 4:56 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