This can probably be done in a more elegant way somehow, but I can suggest using the @Deprecated annotation with DeprecationLevel.ERROR for a function with the appropriate signature defined for the receiver type, for example:
@Deprecated("Cannot be used in a html block.", level = DeprecationLevel.ERROR) fun HtmlReceiver.html(action: HtmlReceiver.() -> Unit): Nothing = error("...")
Or it could be a member function. By the way, the completion of the IDE behaves differently based on whether it is an extension or a member.
This will make calls like internal unacceptable:
html { html {
(demo of this code)
The top-level function can still be called inside the DSL block via FQN, for example. com.example.html { } , so this trick only protects users from incorrectly calling top-level functions.
source share