Solution for non-optional parameters when using DSL

Hey all,
I encountered a problem while writing a DSL api which is that I can’t force the api user to supply some of the parameters, even if they are mandatory for the api.

Passing them in the constructor was an option, but it kind of misses the point of the DSL readability.
Contracts are also an option, but they are limited to top level functions.

I’ve decided to approach this problem using a custom linter:

Did any of you encounter this problem and found a more elegant solution?

Could you please maybe show an example of why it’s impossible to force the user to supply those parameters? because there are multiple ways that are still fluent and concise but that will also force the user to supply the parameters

Well, as I said, there is the constructor option but it defeats the purpose of DSL in my opinion.
Other option is Kotlin Contracts, but it’s limited to top level functions only, so still not good enough.

What other ways are there to force a DSL property to be set?

1 Like

There’s the typestate pattern: you give your DSL builder type some type parameters that change as it gets DSL methods called on it, and some of the methods aren’t usable except when the type parameters line up right for them.

Not sure I understand how this can solve the simple usecase, maybe you can add a code example.
And what about properties?