Skip to content

Instantly share code, notes, and snippets.

@Gab-km
Created December 5, 2012 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gab-km/4213804 to your computer and use it in GitHub Desktop.
Save Gab-km/4213804 to your computer and use it in GitHub Desktop.
F#による構造的部分型の真似事
// target : exec メソッドを持つオブジェクト
// num : exec メソッドに渡す値
let inline exec< ^T when ^T : (member exec : int -> string)> target num =
(^T : (member exec : int -> string) (target, num))
// exec メソッドを持つインターフェイス
type IExec =
abstract exec : int -> string
// IExec インターフェイスを実装するクラス
type Hoge() =
interface IExec with
member this.exec num = (num * 2).ToString()
// exec メソッドを持つクラス
type Fuga() =
member self.exec num = (num * (-1)).ToString()
let hoge = Hoge()
exec (hoge :> IExec) 2 // #=> val it : string = "4"
let fuga = Fuga()
exec fuga 2 // #=> val it : string = "-2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment