Saturday, February 26, 2011

Studying F# : Type Extension #2

In phosphorescence: Studying F# : Type Extension, I mentioned about Type Extension compared with ruby's Open Class. But, there are difference obviously. F#'s Type Extension cannot allow overload with same signature and override.

Overload with different signature works.
> type System.String with                       
-     member s.ToString(arg) = arg + " : " + s;;               

type String with
  member ToString : arg:string -> string

> "World".ToString();;                          
val it : string = "World"
> "World".ToString("hoge");;
val it : string = "hoge : World"

but overload with same signature does not work because extended one is ignored.
> type System.String with
-     member s.ToString() = "characters: " + s;;

type String with
  member ToString : unit -> string
> "World".ToString();;
val it : string = "World"
And, override, is totally fails, because keywords both override and base are allowed with existing inherit clause.
> type System.String with                                         
-     override s.ToString() = "characters: " + base.ToString();;    

      override s.ToString() = "characters: " + base.ToString();;
  -------------^^^^^^^^^^^^

/Users/youhei/stdin(2,14): error FS0854: Method overrides and interface implementations are not permitted here

No comments:

Post a Comment