Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Script
- // Builder to help describe a script.
- interface ScriptBuilder {
- fun doSomething()
- // A script can call other scripts, including itself.
- fun callSubscript(script: Script)
- fun doSomethingElse()
- }
- fun Script(builder: ScriptBuilder.() -> Unit): Script {
- TODO()
- }
- val firstScript = Script {
- doSomething()
- }
- val secondScript = Script {
- doSomethingElse()
- // This compiles:
- callSubscript(firstScript)
- // This does not compile because "Variable secondScript must be initialized":
- callSubscript(secondScript)
- }
- // If a contract allowed the `Script` function to specify that its builder will not be invoked
- // before the function returns, then a script could use itself inside its builder lambda because
- // the compiler can infer that the variable is initialized (given that its initializing method
- // `Script` will have returned, by contract).
- fun Script2(builder: ScriptBuilder.() -> Unit): Script {
- contract {
- callsInPlace(builder, InvocationKind.LAZY)
- }
- TODO()
- }
Advertisement
Add Comment
Please, Sign In to add comment