Youssef Shoaib [MOD]
07/31/2023, 2:24 PMcontext(A) fun <A> given(): A = this@A
to be used like given<List<String>>()
. I think this is better than the this@
syntax since it supports generics (like List<String>
) and can "summon" supertypes of a context receiver which gets resolved to the apt subtype context
Edit: I realise that maybe stdlib can't have context
declarations. Instead, it can be defined equivalently as:
fun <A> A.given(): A = this
The only issue is that it pollutes the namespace of Any.
ephemient
07/31/2023, 2:35 PMcontext(T)
val T.static.context: T
get() = this@T
List<String>.context
ephemient
07/31/2023, 2:37 PMf(given(), given())
from inferring types, which I would find kind of sketchy with context receiversJavier
07/31/2023, 2:38 PMgiven<A>()
is more idiomatic.
But the issue you mentioned can have senseYoussef Shoaib [MOD]
07/31/2023, 3:06 PMf(given(), given())
doesn't actually infer types. An unspecified given()
always resolves to the closest receiver. If the closest receiver is part of a receiver group (i.e. the closest receiver is context(A, B)
), then you get an error. Personally, I would've liked if f(given(), given())
would've worked, but I see the ambiguity and resolution issues with it.