-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shortcut functions #496
Shortcut functions #496
Conversation
Without the use of |
Using
|
Yeah I meant option 1. 2 and 3 don't seem viable.
…On Mon, Oct 15, 2018 at 2:19 PM Friedrich Götze ***@***.***> wrote:
Using @DslMarker should really help, but this annotation has to be
applied to another annotation (you have to define yourself), which then has
to be applied all Foo.Builder types. Therefore, after creating an
annotation like @KotlinPoetDsl, one of the following has to be done:
- Annotate every builder-class with it
- Make the builder *open*, so an inherited class can be annotated
- Add annotated wrapperclasses for each builder
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#496 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEEEdTNdkWLg0qAQUPMW0RwzGNnHnPiks5ulNGhgaJpZM4XbsHK>
.
|
there is a fourth option: @Target(AnnotationTarget.TYPE)
@DslMarker
annotation class KotlinPoetDSL
private typealias ABuilder = (@KotlinPoetDSL A).() -> Unit
class A{
val aval : String = ""
}
class B {
val bval :String = ""
}
fun a( block: ABuilder): Unit = TODO()
fun A.b(block : (@KotlinPoetDSL B).() -> Unit): Unit = TODO()
fun main(args: Array<String>) {
a {
aval
b {
aval //can't access
bval
}
}
} (I think with typealias is better, because then you can't forget to apply the dsl-marker. |
We control the types, though. Why wouldn't we just annotate them directly? |
don't know ;-) |
I don't think we want this in the core library. It's neat and nice but it means we offer two competing APIs. This one is nice if you have full context, but the Builder API is most flexible. |
For my own Project, I created these shortcuts for many of kotlinpoets builders and I like to share them.
pros for usercode:
FunSpec.constructorBuilder(...)
forTypeSpec.primaryConstructor(...)
,FunSpec.getterBuilder(...)
forPropertySpec.getter(...)
etc...I also added a Test which is also a small demonstration.
Shortcuts I didn't use are missing, i.e. for
TypeAliasSpec
.Here's a preview snipped taken from the testclass: