I am not sure if this question should be here but ...
# language-evolution
j
I am not sure if this question should be here but here we go: How can I change the reference? that function is returning
List<Int>
as it is inferring, I guess, the last context receiver. I even get a compile error if I try to assign them to variables as you can see in the images, so I guess it is not currently possible, right?
Copy code
context(List<String>, List<Int>)
fun foo() = this@List
I really miss a #context-receivers channel, not only to ask about how to use them and possible design changes to how we develop programs without them versus them, but to report missing features and/or bugs
w
I wouldn't say it's recommended to have similar contexts sharing the same scope. I would recommend to look at context receivers as a way to make your code more concise, not to reduce syntax. But to answer your question, you can work around it using typealiases.
Copy code
typealias StringList = List<String>

context(StringList, List<Int>)
fun foo() = this@StringList
I really miss a #context-receivers channel
I agree that there is not a good source to find how to use context receivers idiomatically. I'd say that posting here is a good start for sharing your thoughts. If you want to influence the evolution of the language, your best chance is at all times to share good use cases to support your proposal/concern.
j
Thank you! I have tried that approach, but looks like Context Receivers with type parameters are broken on K2, so time to wait! Anyway, I hope they improve the labeling support,
this@List<String>
should be allowed https://youtrack.jetbrains.com/issue/KT-60810/FIR-Context-receivers-with-type-parameters-doesnt-work
y
You also don't need to use labels at all. In fact, labels are entirely useless since you can do:
Copy code
context(A) fun <A> given(): A = this@A
context(List<String>, List<Int>) fun test() {
  val strs = given<List<String>>()
  val ints = given<List<Int>>()
}
And it resolves perfectly
On mobile so formatting is horrible, but this demonstrates that it works (playground):
Copy code
fun main() = with(listOf("hello")) {
    with(listOf(42)){
        test()
    }
}

context(A) fun <A> given(): A = this@A
 context(List<String>, List<Int>) fun test() {
   val strs = given<List<String>>()
     val ints = given<List<Int>>()
     println(strs)
     println(ints)
 }
given
or a function like it should 100% exist in the stdlib. It's a must have in the context receiver world, especially when you're inside a contextual lambda where currently this@ doesn't work.
given
is just IMO the better solution, and it needs no language support really
j
Yeah, it makes sense , maybe you could suggest it on language proposal 😛
y
I think it's more suited to #stdlib probably
👌 1