I'm not sure about overloading on member functions, but you can make Write recursive using something like this:

1
2
3
4
let rec Write (x: obj) =
  match x with
  | :? string -> // do something
  | :? int64 as y -> Int64.to_string y |> box |> Write

This would send back an obj to Write, but still work for pattern matching on type.

Regards,

z.

By on 1/11/2008 2:17 PM ()

Ah - that might do the trick, thanks. I'll try it on Monday.

I'd still be interested to know if inner functions can be overloaded, though. I suspect it should be possible, right?

By on 1/12/2008 11:16 AM ()

I'd still be interested to know if inner functions can be overloaded, though. I suspect it should be possible, right?

Functions on a objects interface can be overloaded by the addition of the OverloadID attribute:

1
2
3
4
5
type Foo =
    [<OverloadID("Bar1")>]
    member x.Bar x = ()
    [<OverloadID("Bar2")>]
    member x.Bar () = ()

However, an inner function cannot be overloaded, there's a couple of reasons for this. Firstly type inference and generating meaning full error messages when things go wrong becomes more difficult. Secondly and perhaps more importanly you are allowed to redefine inner values in F#, this is useful if you have to calculate a lot of intermediate values and don't want to have to invent new names for each step:

1
2
3
4
5
let main() =
    let x = 1 + 1
    let x = x * 4
    let x = x / 6 // ... etc.
    x

So when reusing a name you redefine rather that create a new overload:

1
2
3
4
5
type Foo2 =
    member x.Bar x =
        let whatever (x:string) = 1
        let whatever (x:int) = "1"
        whatever 1

So if you replaced "whatever 1" whith "whatever "1"" you'd gett a type error since this definition is no longer in scope.

Cheers,
Rob

By on 1/12/2008 10:37 PM ()

Great, thanks for the clarification! I'll go back to using that match syntax, but it's a shame. I think the ability to overload inner functions would be a useful addition to the language.

Thanks again.

By on 1/13/2008 1:27 AM ()

(Also, how do I use :? to match a value of type 'byte array'? I get a syntax error on the first square bracket if I say "| :? byte[] ->", and on the 'a' if I say "| :? byte array".

Thanks!)

By on 1/12/2008 11:21 AM ()

(Also, how do I use :? to match a value of type 'byte array'? I get a syntax error on the first square bracket if I say "| :? byte[] ->", and on the 'a' if I say "| :? byte array".

Thanks!)

In addition to using the alternate array<byte> syntax, you could place "byte[]" or "byte array" inside of parentheses.

By on 1/13/2008 5:15 PM ()

Try using the alternative syntax: array<byte>

By on 1/12/2008 3:12 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