Wednesday, February 23, 2011

Studying F# : Type Extension

Ruby's Open Class is the one of cool features.
irb(main):001:0> class String
irb(main):002:1>   def say_hello
irb(main):003:2>     "Hello #{self}"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> "World".say_hello
=> "Hello World"

In F#, there is Type Extension. This is also cool code.
> type System.String with
-     member s.sayHello = "Hello " + s;;

type String with
  member sayHello : string

> "World".sayHello;;
val it : string = "Hello World"
to be continued...

No comments:

Post a Comment