Nice!
I managed to get this working:
class TestSubClass( Int32 value )
method value( Int32 newValue ):
&value = newValue
println( " my value is: " + value )
endClass
class AdjSubClasses<<$code>>
method init( Test2 test2 ):
forEach( sub in test2.subs ) $code
endClass
class Test2
PROPERTIES
subs = {
TestSubClass( 1 ), TestSubClass( 2 ), TestSubClass( 3 )
} : TestSubClass[]
METHODS
method init:
println( "Adjusting:" )
add1ToSubClasses
method add1ToSubClasses:
AdjSubClasses<<sub.value += 1>>( this )
endClass
Which outputs this:
my value is: 1
my value is: 2
my value is: 3
Adjusting:
my value is: 2
my value is: 3
my value is: 4
(Because more examples are always good)
I think you should note (somewhere) that the templated code is scoped only to object you're creating it with. IOW, the code $code above (in AdjSubClasses) can only ref vars in AdjSubClasses (or global). Makes sense when ya think about it, but it's not immediately obvious and some peoples (like me) might think the code is scoped to
where you defined the object with the template.
Coupla other things:
I can't pass a templated object as a param? Is there a way to do this?
class TestSubClass
PROPERTIES
value : Int32
METHODS
method init( value ):
method value( Int32 newValue ):
&value = newValue
println( "my value is: " + value )
endClass
class AdjSubClass<<$code>>()
method apply( TestSubClass subClass ): $code
endClass
class Test2
PROPERTIES
subs = {
TestSubClass( 1 ), TestSubClass( 2 ), TestSubClass( 3 )
} : TestSubClass[]
METHODS
method init:
adjustSubClasses( AdjSubClass<<subClass.value += 1>>() )
method adjustSubClasses( AdjSubClass adjuster ):
forEach( sub in subs ) adjuster.apply( sub )
endClass
The above gives me this error (which is kinda vague I might add - not that compiler errors are ever clear as vodka):
ERROR: Reference to type AdjSubClass is missing substitution values.
LINE 101: method adjustSubClasses( AdjSubClass adjuster ):
I found this causes slagc.exe to crash (using most of the code directly above):
method adjustSubClasses( AdjSubClass<<>> adjuster ):
forEach( sub in subs ) adjuster.apply( sub )
Also (my two cents), although I understand templating and generics in general and the principle that code is data (or, at least, I like to think that I do), I'm not sure the <<>> syntax is really orthogonal for this code substitution stuff. Or, maybe, it's
too orthogonal. Seems like some other delimiter would make this clearer when your dealing with code and not a $DataType. Then again, I don't know how much of a pain that would be.
Neat stuff, tho. Would definitely like to see method templating.