Skip to content

Instantly share code, notes, and snippets.

@azenla
Forked from TrainerGuy22/gist:2355e4accc8949c14384
Last active August 29, 2015 14:02
Show Gist options
  • Save azenla/948c17fed156b3c864a5 to your computer and use it in GitHub Desktop.
Save azenla/948c17fed156b3c864a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/al
// Inspired by JavaScript, Java, Groovy, Ceylon, Swift, C, C++, D, and whatever else.
// We need closures!
namespace test;
// It's redundant, I know. Trying to set an example for the stdlib.
import std.datatypes.{String, Integer, Float, Double};
import std.Process;
private void closures() {
Closure say = (val) -> {
println(val); // Implicit Type Coercion to String
};
Closure sayStrict = (String val) {
println(val); // Strict Typing
};
say("Hello World"); // Operator Overload - Calls: say.call("Hello World");
say(1); // Type Coercion
sayStrict("Hello");
sayStrict(1); // Fails - Compile Time Error of no such method/closure found
}
// : means 'of type'.
private String saySomething(const LetsDoSomethingCool? something) {
// Null-safe-operator!
// If something is null, it'll just return " Something else."
return generics(something?.something) + " Something else.";
}
// Demoing generics. We'll try to avoid template hell though.
// No clue what should happen if two variables under X are of different types.
// Up for debate though.
private X generics<X>(const X test) {
return X;
}
function Integer main(const String[] argv) {
num2();
// Idea backlog : Have an async event that fires when a boolean turns to true.
return 0;
}
num2() {
LetsDoSomethingCool something_cool = new MeToo();
LetsDoSomethingCool something_cool2 = new MeThree();
// Just let them fall out of scope.
// Or we can delete them.
delete something_cool2;
}
protected class LetsDoSomethingCool {
public String? something;
// The ? means it's nullable, and isn't required.
// Yes, I effectively killed function overloading.
this(const String something, const String[]? suffix) {
this.something = something;
if(suffix) {
// Operator overloading. :D
this.something.append(suffix as String);
}
}
// Why not.
!this() {
auto new_something = "Hello.";
something = new_something;
// For now, I'm modeling the stdlib off of node.js's libs. :P
// printLine will just default to Process.stdout if you don't specify.
printLine("I'm saying " + saySomething(something = something) + "!");
}
// Just showing off override. Function does nothing.
// Honestly, you should use fancy setters/getters anyway.
String? getSomething() {
return this.something;
}
}
class MeToo extends LetsDoSomethingCool {
this(const String? something) {
if(something)
super(something = "Something #3.");
else
// More crazyness. ~= makes a copy in the VM instead of passing in by reference. Fancy stuff.
super(something ~= something);
}
!this() {
super();
// Yes, your really looking at this.
switch(this instanceof <>) {
// Just goes till the end of the bracket or another case. No break; crud.
case MeThree:
printLine("Woah. Mind = blown.");
}
}
// Just showing off override. Function does nothing.
override String? getSomething() {
return this.something;
}
}
class MeThree extends MeToo {
this() {
super("Something #4");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment