add @static support for extension method #17887
Replies: 0 comments 11 replies
-
To clarify, currently in Dotty, |
Beta Was this translation helpful? Give feedback.
-
@hepin1989 you can already achieve what you want, without using |
Beta Was this translation helpful? Give feedback.
-
Is |
Beta Was this translation helpful? Give feedback.
-
@LPTK What if the type is defined in Java? |
Beta Was this translation helpful? Give feedback.
-
It's completed but not documented, it's going to be part of Scala 3. |
Beta Was this translation helpful? Give feedback.
-
@hepin1989 hmm... then I guess you're out of luck. But that's quite a niche use case, and I'm not sure it warrants special support. |
Beta Was this translation helpful? Give feedback.
-
In my opinion it isn't, for example if I write: object Test {
@static val foo: Int =
List(1,2,3).reduce(_ + _)
} The generated code does contain a static field foo in class Test, but the body of this field ends up calling a lambda defined in |
Beta Was this translation helpful? Give feedback.
-
I missed the context when |
Beta Was this translation helpful? Give feedback.
-
refs :Kotlin/KEEP#347 |
Beta Was this translation helpful? Give feedback.
-
some recent discussion at https://contributors.scala-lang.org/t/scala-3-extension-method-for-object-and-java-class/6135 |
Beta Was this translation helpful? Give feedback.
-
Re extension methods for java "companion objects": I think this could fairly easily be made to work once you have erased definitions. The problem is that extension (erased a: String.type) def hello = "I am String" Now you should be able to call
Could even be made more ergonomic if the compiler would define all java “companion objects” as instances of erased classes. Then you as a user don’t need to think about adding erased or not. In a way java companion objects have always been "erased". So would be cool to build that on top of first class |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Dotty now has extension method support : https://dotty.epfl.ch/docs/reference/contextual/extension-methods.html
and Dotty is adding
@static
support too, can we add static support for extension method so we can addfactory
like methods to the origin type.Dart has an issue for this too:dart-lang/language#723
Beta Was this translation helpful? Give feedback.
All reactions