https://kotlinlang.org logo
#javascript
Title
# javascript
h

hikamaeng

03/27/2024, 1:37 AM
Did you know that only Kotlin JS cannot implement lambdas? I hope it becomes possible soon.
Copy code
class Some private constructor(val v:Any){
  companion object:(Any)->Some{
    override fun invoke(v:Any) = Some(v)
  }
}

fun <T, R> needFactory(v:T, factory:(T)->R):R = factory(v)

needFactory(3, Some)
Implementing lambda is one of the simplest ways to create a factory ^^
a

Arjan van Wieringen

03/27/2024, 6:12 AM
Maybe I am dumb, but what is the use of this code? Why not just do Some(3).
a

Artem Kobzar

03/27/2024, 8:16 AM
We try to use the native JS functions to not create a new object/class for each lambda/function. But you are right, we should definitely to fix it. Could you please create an issue on YouTrack?
e

Edoardo Luppi

03/27/2024, 8:36 AM
@Arjan van Wieringen that's probably a simplified example. And it can make sense to want to pass a lambda to a function like
needFactory
I think the key point is the behavior of the language is different depending on the target, which is unexpected. To me it looks a good enhancement, the only thing, as Artem wrote, is I wouldn't like more overhead than what's necessary. That is, if a fix for this messes up something else, then better avoid.
h

hikamaeng

03/29/2024, 12:18 PM
sorry about wrong sample ^^
2 Views