Class extension (dot syntax for new methods)

BlitzMax Forums/BlitzMax Programming/Class extension (dot syntax for new methods)

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

tong,

As to your second question, honestly I'm not sure why this kind of casting is not allowed. It has something to do with the way that objects are implemented internally in BMax. For instance, this code will not run:

SuperStrict
Type TBAMap Extends TMap
	Method Encode:String()
		Local Result :String
		Local K:String
		For K = EachIn Keys()
			Result = Result + String(K) + "=" + String(ValueForKey(K))
		Next
		Return Result 
	EndMethod
EndType

Local map:TMap = New TMap  ' Change to map:TMap = New TBAMap and it will work
map.Insert("test", "test value")
Notify TBAMap(map).Encode()


It gives the runtime error "Unhandled Exception:Attempt to access field or method of Null object".

Some languages do allow casting in this direction as long as there are no fields added by the subclass, and even sometimes then. This isn't really what I would want, as I believe method extension provide much more flexibility.

It'd be much better to be able to call .Encode() directly instead of always having to cast to TBAMap() first, even if that worked.