andylamax
10/24/2024, 10:57 PMfun Human.canSing() : Boolean = true
And it occurred to me that, the same function should be able to be written as follows
fun (Human).canSing() : Boolean = true
Why the extra parantheses you ask?? Bear with me. But for now, the second syntax and the first one should resolved to exactly the same thing just like how expressions would be treated
val x = 3 + 4 // same as val x = (3 + 4)
Wether you put parentheses or not is up to you.
This then propelled me into context parameters, With this kind of syntax, we open room to a new syntax one that supports context parameters (or Intersection Types as I like to delude myself by hopping that user defined intersection types will be coming to kotlin). The new syntax would then look like this
fun (Human & Cat & Dog).canSing() : Boolean {
// code goes here
}
I don't know about you, bout I prefer this syntax over the one proposed earlier with context(Human,Cat,Dog)
At the same time, this syntax also open doors for the long awaited union types
fun (Human | Cat | Dog).canSing() : Boolean {
}
Ofcourse, I do not know how much work is going to have to into the compiler to achieve this (I am not a compiler expert). But this syntax looks almost native to kotlin (atleast for me)
Thoughts????Joffrey
10/24/2024, 10:59 PM&
or |
is very problematic because it implies a single instance of the intersection/union type, which is not what context parameters are (they are several distinct instances, each of their own types).
It's way more natural to me to use commas to separate these parameters, just like in other parameter lists (regular function parameters, lambda parameters, etc.).Joffrey
10/24/2024, 11:03 PMreceiver.canSing()
), while context parameters are implicitly passed from the current scope. The proposed syntax is explicitly designed to make the declaration site look like the call site (contexts come "from above"), just like for the receiver (which is the thing before the dot):
context(cat: Cat, dog: Dog)
fun Human.canSing(): Boolean = ...
fun main() {
with(Cat(), Dog()) {
Human().canSing()
}
}
ephemient
10/25/2024, 12:05 AMclass A {
fun B.foo() { println("A.B") }
}
class B {
fun A.foo() { println("B.A") }
}
yep. the explicit receiver in the current language (even without context with(A()) { B().foo() } // A.B
with(B()) { A().foo() } // B.A
andylamax
10/25/2024, 2:06 AMJokubas Alekna
10/25/2024, 11:59 AMwhere
syntax will be used most likely.stantronic
10/28/2024, 5:49 PMJP Sugarbroad
10/30/2024, 10:04 PMNothing