私はこのような構成を避けようとしています:
val result = this.getClass.getSimpleName
if (result.endsWith("$")) result.init else result
この例では、then
and else
ブランチは単純ですが、複雑なブランチをイメージ化することができます。私は以下を構築しました:
object TernaryOp {
class Ternary[T](t: T) {
def is[R](bte: BranchThenElse[T,R]) = if (bte.branch(t)) bte.then(t) else bte.elze(t)
}
class Branch[T](branch: T => Boolean) {
def ?[R] (then: T => R) = new BranchThen(branch,then)
}
class BranchThen[T,R](val branch: T => Boolean, val then: T => R)
class Elze[T,R](elze: T => R) {
def :: (bt: BranchThen[T,R]) = new BranchThenElse(bt.branch,bt.then,elze)
}
class BranchThenElse[T,R](val branch: T => Boolean, val then: T => R, val elze: T => R)
implicit def any2Ternary[T](t: T) = new Ternary(t)
implicit def fct2Branch[T](branch: T => Boolean) = new Branch(branch)
implicit def fct2Elze[T,R](elze: T => R) = new Elze(elze)
}
これを定義すると、上記の簡単な例を次のように置き換えることができます。
this.getClass.getSimpleName is {s: String => s.endsWith("$")} ? {s: String => s.init} :: {s: String => s}
しかし、どうすれば取り除くことができs: String =>
ますか?私はそのようなものが欲しい:
this.getClass.getSimpleName is {_.endsWith("$")} ? {_.init} :: {identity}
コンパイラーは型を推測するために追加のものが必要だと思います。
?
メソッド名最初の文字として、他のalphanumの文字の前と:
左結合性のために。したがって、左から右に型推論が機能するように、新しいメソッド名を再考する必要があります。ありがとう!
HasIs
、IsWithCondition
、ConditionAndTrueCase
左から右への式の部分を構築することになるクラス。)