public class foo{ public delegate operator int op(int lhs, int rhs); public delegate void DrainA(a instance); public void doIt(){ print 100 op 100 " \r\n"; }}public static void bar(){ print "BAR!\r\n"; }public goose class a{}public goose class b:a{}public static void AorB(a A){ print "A\r\n"; }public static void AorB(b B){ print "B\r\n"; }// plus and minus are the built-in names for + and - respectively.public static void main(){ local foo x = new foo; x.op = plus; print x.op(2,2) " \r\n"; x.doIt(); x.op = minus; print 2 x.op 2 " \r\n"; x.doIt(); //x.op = bar; // error. x.DrainA = AorB; local a A = new a; local a B = new b; x.DrainA(A); x.DrainA(B);}// 4// 200// 0// 0// A// B
Had a little bit of time and worked a bit on delegates tonight. Turns out they weren't terribly difficult due to some good planning on my part, and because a lot of the stuff was already there. I still need to make normal methods not assignable, and stuff might break in corner cases, and I need yet to make the default delegate initializer be a no-op, and I need to make instance binding work so a instance/instance-method pair can be pushed into a delegate, but the 'simple' stuff works:
Previous Entry
Paying the Piper.
Next Entry
Tangent: Lambda Expressions
Comments
Daerax
Cool. I like how you can go x.op = plus. Thats a good idea. Are you going to allow local function definitions too?
August 19, 2008 07:22 PM
Quote: Original post by Daerax
Are you going to allow local function definitions too?
Clarify?
August 19, 2008 07:36 PM
Yup. Currently I'm looking towards a mildly more verbose form of C# 3.0 lambda syntax:
Tangent usually won't be able to do the type inference that the normal lambdas do, and will usually require a return type to disambiguate the block. So I'm going to require the return type be specified to the right of the arrow, functional style.
All the infrastructure is there because I planned around including them, I just need to implement the syntax and a small class to store/reuse the already existing local state object.
SomeDelegate = (int x, int y) => int{x+y;};
Tangent usually won't be able to do the type inference that the normal lambdas do, and will usually require a return type to disambiguate the block. So I'm going to require the return type be specified to the right of the arrow, functional style.
All the infrastructure is there because I planned around including them, I just need to implement the syntax and a small class to store/reuse the already existing local state object.
August 20, 2008 08:44 AM
Advertisement
Latest Entries
And so it goes...
1787 views
Tangent, now with usable error messages!
1929 views
Back to work!
1859 views
Tangent: Haitus
1807 views
Tangent: Syntax reworking
1593 views
Tangent: Adverbs
1456 views
Tangent: Pattern Matching, Part 1
1668 views
Tangent: Pattern Matching, Part 1
1520 views
Tangent: Generic Method Bodies, Part 2
1681 views
Tangent: Generic Method Bodies, Part 1
1528 views
Advertisement