jdemeulenaere
02/13/2020, 10:08 AMjdemeulenaere
02/13/2020, 10:09 AMjdemeulenaere
02/13/2020, 10:54 AMelizarov
02/13/2020, 11:32 AMjdemeulenaere
02/13/2020, 12:25 PMclass 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:
@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:
// 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 ?elizarov
02/13/2020, 1:23 PMelizarov
02/13/2020, 1:26 PMQuy D X Nguyen
02/14/2020, 3:40 AMQuy D X Nguyen
02/14/2020, 3:40 AMQuy D X Nguyen
02/14/2020, 3:43 AMelizarov
02/14/2020, 6:08 AMjdemeulenaere
02/14/2020, 1:35 PMelizarov
02/14/2020, 1:47 PM