Correcting rewrite type info after compete call.

#KT-10934 Fixed
#KT-10896 Fixed
This commit is contained in:
Stanislav Erokhin
2016-02-08 16:41:39 +03:00
parent 74b2c8cd31
commit 434bd0707d
7 changed files with 191 additions and 4 deletions
+38
View File
@@ -0,0 +1,38 @@
//KT-10934 compiler throws UninferredParameterTypeConstructor in when block that covers all types
class Parser<TInput, TValue>(val f: (TInput) -> Result<TInput, TValue>) {
operator fun invoke(input: TInput): Result<TInput, TValue> = f(input)
fun <TIntermediate, TValue2> mapJoin(
selector: (TValue) -> Parser<TInput, TIntermediate>,
projector: (TValue, TIntermediate) -> TValue2
): Parser<TInput, TValue2> {
return Parser({ input ->
val res = this(input)
when (res) {
is Result.ParseError -> Result.ParseError(res.productionLabel, res.child, res.rest)
is Result.Value -> {
val v = res.value
val res2 = selector(v)(res.rest)
when (res2) {
is Result.ParseError -> Result.ParseError(res2.productionLabel, res2.child, res2.rest)
is Result.Value -> Result.Value(projector(v, res2.value), res2.rest)
}
}
}
})
}
}
/** A parser can return one of two Results */
sealed class Result<TInput, TValue> {
class Value<TInput, TValue>(val value: TValue, val rest: TInput) : Result<TInput, TValue>() {}
class ParseError<TInput, TValue>(val productionLabel: String,
val child: ParseError<TInput, *>?,
val rest: TInput) : Result<TInput, TValue>() {}
}
fun box() = "OK"
@@ -0,0 +1,26 @@
interface Option<out T> {
val s: String
}
class Some<T>(override val s: String) : Option<T>
class None(override val s: String = "None") : Option<Int>
fun whenTest(a: Int): Option<Any> = when (a) {
239 -> {
if (a == 239) Some("239") else None()
}
else -> if (a != 239) Some("$a") else None()
}
fun ifTest(a: Int): Option<Any> = if (a == 239) {
if (a == 239) Some("239") else None()
} else if (a != 239) Some("$a") else None()
fun box(): String {
if (whenTest(2).s != "2") return "Fail 1"
if (whenTest(239).s != "239") return "Fail 2"
if (ifTest(2).s != "2") return "Fail 3"
if (ifTest(239).s != "239") return "Fail 4"
return "OK"
}