Guest User

Untitled

a guest
Jul 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. interface Script
  2.  
  3. // Builder to help describe a script.
  4. interface ScriptBuilder {
  5. fun doSomething()
  6.  
  7. // A script can call other scripts, including itself.
  8. fun callSubscript(script: Script)
  9.  
  10. fun doSomethingElse()
  11. }
  12.  
  13. fun Script(builder: ScriptBuilder.() -> Unit): Script {
  14. TODO()
  15. }
  16.  
  17. val firstScript = Script {
  18. doSomething()
  19. }
  20.  
  21. val secondScript = Script {
  22. doSomethingElse()
  23.  
  24. // This compiles:
  25. callSubscript(firstScript)
  26.  
  27. // This does not compile because "Variable secondScript must be initialized":
  28. callSubscript(secondScript)
  29. }
  30.  
  31. // If a contract allowed the `Script` function to specify that its builder will not be invoked
  32. // before the function returns, then a script could use itself inside its builder lambda because
  33. // the compiler can infer that the variable is initialized (given that its initializing method
  34. // `Script` will have returned, by contract).
  35. fun Script2(builder: ScriptBuilder.() -> Unit): Script {
  36. contract {
  37. callsInPlace(builder, InvocationKind.LAZY)
  38. }
  39. TODO()
  40. }
Advertisement
Add Comment
Please, Sign In to add comment