Created
December 5, 2012 08:28
-
-
Save Gab-km/4213804 to your computer and use it in GitHub Desktop.
F#による構造的部分型の真似事
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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