Skip to content

Instantly share code, notes, and snippets.

@bamboo
Last active December 22, 2015 06:38
Show Gist options
  • Save bamboo/6432054 to your computer and use it in GitHub Desktop.
Save bamboo/6432054 to your computer and use it in GitHub Desktop.
HACK: Declaring generic UnityScript types
import Boo.Lang.Compiler
import Boo.Lang.Compiler.Ast
import Boo.Lang.PatternMatching
class GenericAttribute(AbstractAstAttribute):
_parameters as ArrayLiteralExpression
def constructor(parameters as ArrayLiteralExpression):
_parameters = parameters
override def Apply(node as Node):
match node:
case t=TypeDefinition():
t.GenericParameters.AddRange(GenericParameters)
case m=Method():
m.GenericParameters.AddRange(GenericParameters)
GenericParameters:
get:
for e as ReferenceExpression in _parameters.Items:
yield GenericParameterDeclaration.Lift(e)
// UnityScript version of the meta attribute
import Boo.Lang.Compiler;
import Boo.Lang.Compiler.Ast;
import Boo.Lang.PatternMatching;
import System.Linq;
class GenericAttribute extends AbstractAstAttribute {
var _parameters: ArrayLiteralExpression;
function GenericAttribute(parameters: ArrayLiteralExpression) {
_parameters = parameters;
}
override function Apply(node: Node) {
var t = node as TypeDefinition;
if (t) {
t.GenericParameters.AddRange(GenericParameters());
return;
}
var m = node as Method;
if (m) {
m.GenericParameters.AddRange(GenericParameters());
return;
}
}
function GenericParameters() {
return _parameters.Items.Cast.<ReferenceExpression>().Select(function(e) GenericParameterDeclaration.Lift(e));
}
}
@generic([a, b])
class Pair {
public var first: a;
public var second: b;
override function ToString() {
return String.Format("Pair({0}, {1})", first, second);
}
}
@generic([a, b])
static function pair(fst: a, snd: b) {
var result = new Pair.<a, b>();
result.first = fst;
result.second = snd;
return result;
}
static function Test() {
return pair("ltuae", 42);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment