Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. Comments:
  2. // This is a single line
  3. /*
  4. These
  5. are multiple
  6. lines
  7. /* can be nested */
  8. */
  9. /**
  10. ### This is a doc comment
  11. *you can use markdown in here*
  12. */
  13.  
  14. Variables:
  15.  
  16. int a = 0 // type: int
  17. const int b = 0 // type: int
  18. const c = 0 // type: int, constant (inferred)
  19. var d = 0 // type: int, (inferred)
  20. e = 0 // type: any
  21.  
  22. Functions:
  23.  
  24. // a is type any, b is type int, function returns any
  25. def foo a, int b
  26. // code here
  27. return a // explicit return
  28. a // implicit return
  29.  
  30. // a is type any, b is type int?, function returns int
  31. def foo a, int? b -> int
  32. // code here
  33. return int! a // has to be cast to int
  34.  
  35. // a is type any?, b is type (int -> int?), function returns int
  36. def foo a?, (int -> int?) b -> int
  37. return int! b()
  38.  
  39. Varargs:
  40.  
  41. def foo int a...
  42. print(a(1)) if a:size() > 0
  43.  
  44. Casting:
  45.  
  46. int? a = null
  47. int b = int! a // cast to type
  48. int c = !! a // autocast
  49.  
  50. Type checking:
  51.  
  52. var a = 10
  53. var tpe = type a // tpe is of type (type<int>)
  54.  
  55. if a is type int // runtime type checking
  56. // code here
  57.  
  58. Lambdas:
  59.  
  60. a = \a, b, c -> bar(a, b, c)
  61.  
  62. // or multiple lines
  63. a = \a, b, c ->
  64. bar(a, b, c)
  65. baz()
  66.  
  67. // no parameters
  68. a = \-> bar()
  69.  
  70. // shorter
  71. a = do bar()
  72.  
  73. if, else if:
  74.  
  75. if a == b
  76. // code here
  77. else if a == c
  78. // and here
  79. else
  80. // and there
  81.  
  82. foo = bar if a == b // one line
  83.  
  84. If statemenets can return values:
  85.  
  86. bar = if foo
  87. // code here
  88. boo() // implicit return
  89. else
  90. doo() // implicit return
  91.  
  92. Or shorter:
  93.  
  94. bar = boo() if foo else doo()
  95.  
  96. Arrays:
  97. array = {1, 2, 3, 4} // type: [4 int]
  98. [4 float] array = {1, 2, 3, 4} // type: [4 float]
  99.  
  100. Lists and maps:
  101.  
  102. var list = [1, 2, 3, 4] // type: list<int>
  103. list<float> list = [1, 2, 3, 4] // type: list<float>
  104.  
  105. var map = ["foo" = 0] // type: map<string, int>
  106. map<any, float> map = ["foo" = 1] // type: map<any, float>
  107.  
  108. map2 = ["foo" = 0] // type: any
  109.  
  110. Function calls:
  111.  
  112. Normal call:
  113.  
  114. foo()
  115. foo(a, b, c)
  116.  
  117. Call with...
  118.  
  119. foo "string" // string literal
  120. foo 1242 // number literal
  121. foo \a, b -> b // lambda
  122. foo do
  123. goo()
  124. boo() // lambda
  125.  
  126. foo {1, 2, 3} // array
  127. foo [1, 2, 3] // list
  128. foo {"a" = b} // map
  129.  
  130. Structs:
  131.  
  132. struct foo
  133. int a = 20
  134. var c = 50
  135. [5 int] d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement