Tangent: Virtual Methods

Published August 08, 2008
Advertisement
Quick work on virtual methods and type comparison between unbuilt and built types.
This now works:

public goose class A{	public virtual void moo(){		print "moo.\r\n";	}}public goose class B:A{	public virtual void moo(){		print "MOO!\r\n";	}}public static void main(){	local A foo = new B;	local A bar = new A; 	foo.moo();	bar.moo();}


I'm sure there's some details which need polished. Right now for example I'm pretty sure all methods are treated as virtual, so that will need to be fixed. It probably won't work exactly like traditional virtual methods, but should work more like them. There's probably also bugs with multiple inheritance and virtual methods and/or virtual dispatch where there's a 'gap' in the override sequence. Little things, mostly polish/bugfixes.

This though does not currently work:

public goose class A{	public virtual void moo(A rhs){		print "AA\r\n";	}	public virtual void moo(B rhs){		print "AB\r\n";	}}public goose class B:A{	public virtual void moo(B rhs){		print "BB\r\n";	}	public virtual void moo(A rhs){		print "BA\r\n";	}}public static void main(){	local A a;	local B b;	a.moo(a);	a.moo(b);	b.moo(a);	b.moo(b);}


b.moo(a) returns an ambiguous syntax error because there's some error while checking if A. Going to play some games, watch some football, eat some red vines and relax. I'll fix that up later tonight.


Also, while I'm thinking of it; I changed the default initializer for classes to be new rather than null. It will be more common I think and make people write out = null, which should serve as a reminder to deal with it before use. That'll be something for playtesting I think.


[edit: 3:30 hr later (30 mins of debugging)]
Just a few little changes and the multiple dispatch in source works. Again, there's probably some polish to be had; keeping types disparate, adding warnings for ambiguity, a means to select 1 method from a group manually, IDE support for seeing what is in a group, and how it interacts with X.

Sadly enough, that covers the things I was aiming to complete over the weekend. So tomorrow I'll work on targets of opportunity.

Probably either absolute path declarations or this methods

absolute path declarations are essentially operator overloads. It's adding an arbitrary member to mix into a type from a different scope. The @ symbol will be used to prefix the identifier to do that:

public class vector{    public operator vector @plus(vector lhs, vector rhs){ ... }}


Will mix-in this method definition with the global member plus (which happens to be the name of the + operator). Though it'll have access to vector's scope, and thus private members just like operator overloads seen elsewhere. It might also have some use for friend functions and similar work-arounds. I'll need to think more about the details in implementation, and the scope of this.

this methods will be a different syntax for C++'s operator(). For those not familiar with C++, it allows a class to act as a creatable method. The special declaration has access then to stored variables within the instance.

public class PlusX{    public int X = 5;    public int this(int rhs){        return(X+rhs);    }}public static void main(){    local PlusX plus5 = new PlusX;    local PlusX plus1000 = new PlusX;       plus1000.X = 1000;    print plus5 50 " " plus1000 50 " \r\n";}// 55 1050


A contrived example, but a more straightforward way to create some state to accompany event handlers (and similar problems). Code in C# tends too often to either tie one method of a big class into the event or require some (unintuitive) implicit conversions to make a small class act as a functor. Since Tangent will allow (and advocate) more emphasis on methods, quickie functor creation becomes more important.

Previous Entry I am going to hell.
0 likes 1 comments

Comments

Daerax
Good that you got multiple dispatch working. That is one of the most distinguishing parts of your language imo.

this line is confusing:

print plus5 50 " " plus1000 50 " \r\n";
August 09, 2008 01:13 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement