Tangent: Delegates

Published August 19, 2008
Advertisement
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:

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
0 likes 4 comments

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
Telastyn
Quote: Original post by Daerax
Are you going to allow local function definitions too?


Clarify?

August 19, 2008 07:36 PM
Daerax
Like x.DrainA = delegate(x,y,...,z){}
August 20, 2008 07:08 AM
Telastyn
Yup. Currently I'm looking towards a mildly more verbose form of C# 3.0 lambda syntax:


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
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement