Created
January 29, 2013 10:02
-
-
Save Simn/4663165 to your computer and use it in GitHub Desktop.
Dynamic implementation switch with abstracts
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
// generated Js | |
(function () { "use strict"; | |
var I = function() { } | |
var C1 = function() { | |
}; | |
C1.__interfaces__ = [I]; | |
C1.prototype = { | |
print: function(s) { | |
console.log("C1: " + s); | |
} | |
} | |
var C2 = function() { | |
}; | |
C2.__interfaces__ = [I]; | |
C2.prototype = { | |
print: function(s) { | |
console.log("C2: " + s); | |
} | |
} | |
var Main = function() { } | |
Main.main = function() { | |
var flag = true; | |
var c = flag?new C1():new C2(); | |
c.print("Hello"); | |
flag = false; | |
var c1 = flag?new C1():new C2(); | |
c1.print("World"); | |
} | |
Main.main(); | |
})(); |
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
interface I { | |
public function print(s:String):Void; | |
} | |
class C1 implements I { | |
public function new() { } | |
public function print(s) trace('C1: $s') | |
} | |
class C2 implements I { | |
public function new() { } | |
public function print(s) trace('C2: $s') | |
} | |
abstract C(I) { | |
public inline function new(flag) | |
this = flag ? new C1() : new C2() | |
public inline function print(s:String) | |
this.print(s) | |
} | |
class Main { | |
static function main() { | |
var flag = true; | |
var c = new C(flag); | |
c.print("Hello"); // C1: Hello | |
flag = false; | |
var c = new C(flag); | |
c.print("World"); // C2: World | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment