Last active
December 14, 2015 02:59
-
-
Save PhilipWitte/5017538 to your computer and use it in GitHub Desktop.
Reign Language Goals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Person | |
{ | |
var name : text | |
var age : uint | |
init new(.name, .age=) | |
init load(path) { # load from path # } | |
func greet() | |
{ | |
Console.write("Hi, my name is {name}.") | |
Console.write("I am {age} years old.") | |
} | |
} | |
func main() | |
{ | |
var andrew = Person.("Andrew", 27) | |
var philip = Person.new("Philip", 25) | |
var sarah = Person.load("people/Sarah.xml") | |
andrew.greet() | |
philip.greet() | |
sarah.greet() | |
} | |
# ----- Notice, no need to "import" 'Console' since the | |
# ----- language finds symbols logically, for all files compiled. | |
# ----- However, for convenience, and to resolve conflicts, use: | |
use Console | |
type Person { | |
... | |
func greet() { | |
write(...) # don't need to prefix 'Console' | |
} | |
} | |
# ----- Type deduction (ONLY POSSIBLE WAY): | |
func foo(x:int, y:int|dec) # x is int, y is int or dec (float) | |
func bar(x, y) { | |
return x + y | |
} | |
func main() { | |
var a = 0 # int | |
var b = 0.0 # dec | |
var c = "hi" # text | |
foo(a, b) # works | |
bar(a, b) # works | |
bar(a, c) # error! | |
} | |
# ----- Template Types/Fucs (ONLY POSSIBLE WAY): | |
type Vector(T = dec) | |
{ | |
var x, y, z, w : T | |
func += (n) { x, y, z, w += n } | |
func += (v) { x, y, z, w += v.(x, y, z, w) } | |
... | |
func dot() { return x*x + y*y + z*z + w*w } | |
func dot(v) { return x, y, z, w *>+ v.(x, y, z, w) } | |
func cross() { x, y, z, w *,+ w, z, y, x } | |
init cross(a, b) { a.(x, y, z, w) *,+ v.(w, z, y, x) } | |
} | |
func main() | |
{ | |
var a = Vector.(1, 2, 3, 4) | |
var b = Vector(int).(1, 2, 3, 4) | |
var c = Vector.cross(a, b) | |
var d = Vector(dec64).cross(b, c) | |
a.cross(a, b) # init can be used like func, but not the other way around | |
a += b # calls 'func += (v)' | |
a += 2 # calls 'func += (n)' | |
# funcs are found in declaration order | |
# so you can have multiple overload with the same type | |
# the first one will be tried first, but if it doesn't work | |
# then the compler move on to the next and tries that, until | |
# it matches one, or fails completely and throws a compiler error | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment