Is there any plan to implement <https://youtrack.j...
# language-proposals
j
Is there any plan to implement https://youtrack.jetbrains.com/issue/KT-4075 or any reason why this is difficult ? Can we help in any way ? I'm currently building a DSL and the API suffers quite a lot from not being able to overload setters 😕
@elizarov tagging you here as I saw you are also interested by this proposal
If this is not something the Kotlin team wants to add in the language, how hard would it be to implement it with a compiler plugin ? I would not be interested in things like editor support (completions, etc) at first sight.
e
Hard. Especially to ensure all the refactorings and etc work.
j
What if we are not interested in editing/refactoring support, really just byte code generation ? I own the compilation of the DSL (so the interface/classes that would somehow define overloaded setters using annotations or whatsoever) and of the Kotlin scripts that are using the DSL (that make the calls to those overloaded setters). What I have in mind is something like the following. Let's say we have the following code:
Copy code
class Duration(val milliseconds: Long)

// Foo.kt -- in the DSL library
class Foo {
  @GenerateLongToDurationOverload
  var duration: Duration = Duration(0)
}

// MyScript.kt -- the script that depends on Foo.kt
fun main() {
  // With the compiler plugin, we can do:
  Foo().duration = 100
}

@GenerateLongToDurationOverload
fun longToDuration(input: Long): Duration = Duration(input)
and the following annotations:
Copy code
@GenerateLongToDurationOverload
fun longToDuration(input: Long): Duration = Duration(input)

/** Meta-annotation that should be put on annotations that generate setter overloads. */
@Target(AnnotationTarget.ANNOTATION_CLASS)
@Retention(AnnotationRetention.BINARY)
annotation class GenerateOverload

/**
 * This annotation should be put on exactly one function with a single parameter and can be put
 * to any number of properties whose type is the same of the output of the annotated function.
 */
@GenerateOverload
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
annotation class GenerateLongToDurationOverload
Then the compiler plugin would generate code that would make the result the same as if we had written:
Copy code
// Foo.kt
class Foo {
  var duration: Duration = Duration(0)

  // this will have the same visibility as duration setter.
  fun duration$pluginId$longToDuration(duration: Long) {
    duration = longToDuration(duration)
  }
}

// Script.kt
fun main() {
  Foo().duration$pluginId$longToDuration(100)
}
Would it be possible to achieve that using plugins ? Can you tell us more about the official team plans about KT-4075, or is it confidential ?
e
Hacking something that works should be relatively easy. Turning it into an actual language feature is hard.
Long-term, it is unlikey we’ll every support it. More likely, is that we’ll either support some form of user-defined type conversion or some form of union types (or both) that would make this kind of setter overload unnesseary for the majority of use-cases.
👌 1
👀 2
👍 1
q
I'm trying to work out an arrow meta solution for it
Been kinda busy running into brick walls, but I'm getting closer
Also @elizarov why would expanding the type system be easier than adding overload support?
e
It would not be easier, but it will bring much more use as opposed to solving one narrow problem.
j
Union types or conversion sounds even better indeed 🙂 Sorry to ask any question, but are there any official plans/KEEP about those already ? Would love to help in any way.
e
Not yet. This is not going to happen anytime soon.