Required object-invocation


#1

I have three classes:

Class Accessor(val accessor: String)

Class FileBuilder(callBack: (File) -> Unit){
     val Accessor.clazz get() = ClazzBuilder({...})
     val Accessor.method get() = MethodBuilder({...})
      // trying to avoid: 
     // fun Accessor.method(someParams) = MethodBuillder().build(someParams)
    // fun Accessor.method(otherParams) = MethodBuillder().build(otherParams)
}
Class ClazzBuilder(callBack: (Clazz) -> Unit){
     val Accessor.method get() = MethodBuillder({...})
      // trying to avoid:
     // fun Accessor.method(someParams) = MethodBuillder().build(someParams)
    // fun Accessor.method(otherParams) = MethodBuillder().build(otherParams)
}
class MethodBuilder(callBack: (Method) -> Unit){
    //multiple invoke-functions with other params
}

with my function you can create Something like:

public.static,fun("hoi", "par1" of String::class){}

The function-arguments and thus the complete construction of the the Method are delegated to the MethodBuillder, meaning both the FileBuilderClass and the ClazzBuilderClass don’t have to be changed if I want to add a new way of creating the method.

One major drawback is that an User can quit the process to early by not invoking the method-field for example.
therefor the API is not as great to use anymore.

Therefor, I would like it if there was an annotation which I can place on the returntype that requires to invoke the method (in general or before leaving a scope or lambda) and that throws an exception at compiletime.

I think that this can make the creation of DSL’s in some contexts a lot cleaner.


Good examples of Kotlin based DSLs