molikuner
01/04/2020, 7:44 PMabstract class BaseApi
class ApiA(val foo: String) : BaseApi
class ApiB(val bar: Int) : BaseApi
//--------------------------
// case 1: // ideally without the type annotation needed
val x: ApiA = api {
a("Hello, world!")
}
// case 2: // also ideally without type annotation needed
val x: ApiB = api {
b(42)
}
// case 3: // ideally compile-time error
val x = api {}
// case 4: // ideally compile-time error
val x = api {
a("Hello, world!")
b(42)
}
Does anybody know how I could do something like that or whether this is even possible in kotlin?Nikky
01/04/2020, 7:56 PMfun ApiScope.a(A_Scope.() -> A_Scope)
also make the api lambda like so..
fun <T> api(ApiScope.() -> T): T
this makes it so whatever is the last call in api is the type of api returned
case 3 would fail properly.. case 4 not
i would just do
api.a("Hello World) {
// do more stuff in scope here
}
or directly assign from the calls of a
and b
Nikky
01/04/2020, 7:56 PMmolikuner
01/04/2020, 8:00 PMbuilder {
propOne("Hello, world!")
propTwo(42)
}
but disallow:
builder {
propOne("Hello, world!")
}
or
builder {
propTwo(42)
}
molikuner
01/04/2020, 8:01 PMNikky
01/05/2020, 10:12 AMmolikuner
01/05/2020, 10:15 AM