Created
August 15, 2018 22:50
-
-
Save akehoyayoi/fa0201c1881feafd5f22cb812fb99939 to your computer and use it in GitHub Desktop.
mixin example
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
class TreeControl[T] { | |
// これらのIFを使いたい | |
dataNodes: T[]; | |
def getLevel(node: T): Int = {} | |
} | |
class NestedTreeControl[T] extends TreeControl[T] {} | |
class FlatTreeControl[T] extends TreeControl[T] {} | |
trait TreeControlEnhancer[T] { | |
self: TreeControl[T] => // ここで自己型解決しておけば | |
def getParent(node: T): T = { | |
val currentLevel = getLevel(node); // TreeControl.getLevelが使える | |
... | |
} | |
} | |
// TreeControlEnhancerを使うときは下記のような書き方でOK | |
val treeControl = new NestedTreeControl[Type] with TreeControlEnhancer[Type] |
型情報が必要なら、以下の2つしか無いはず
- 素直に継承なりラッパークラスなりにする
- trait用のクラスを定義 & implements する(クラスを継承でなくインターフェイスとして利用) & voidを返すメソッドを仮定義 & prototypeに代入することで実装を得る…。
これは TSのページに MixIn として紹介されてるやつそのものですね。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
型情報が落ちてもいいならこんなんでできるはず。