polymorphing

BlitzMax Forums/BlitzMax Beginners Area/polymorphing

Jesse(Posted 2006) [#1]
This is an extract of a program I am working on:
Type a
	Field z%
End Type
Type b Extends a
	Field z% = 2
End Type
Type c Extends a
	Field z% = 3
End Type
Local me:a = New c
Print me.z


I am trying to display the field z% with the value of 3 but I get 0. I am assuming it accessing the field z in the a type. can anybody tell me what I am doing wrong. Or is this a compiler bug?


Dreamora(Posted 2006) [#2]
Your error is:

Field z is defined in a, you can't redefine it.

So what you need to do is using the default constructor to set the value z

Type a
	Field z%
End Type
Type b Extends a
	Method New()
		z = 2
	End Method
End Type
Type c Extends a
	Method New()
		z = 3
	End Method
End Type
Local me:a = New c
Print me.z



Jesse(Posted 2006) [#3]
so why isn't it giving me any erros. and why can't I access them like I access methods. I can clearly define the same method on the parent and on the child and I am able to access them respectively. I thought that polymorphing was also for fields not only for methods.
I had thought about your answer myself but I wanted to make shure. Is that normal for all languages or Just for Bmax?
I started learning OOP here with bmax about a year ago so don't really comprehend the syntax too well.


Perturbatio(Posted 2006) [#4]
surely you want to have me as type C since it's a C you're creating?


Dreamora(Posted 2006) [#5]
Methods allow overriding as it makes sense there as the implementation can change.

A field is a field, its implementation can't change so no use why overriding should work.

But thats exactly why the default constructor exist, to initialize extended types correctly.



And I think this is how all OO (beside C++ perhaps which just uses 20 year old design ideas) work. A property that is defined by a extended class can't be "overdefined" again unless the language support undefine / redefine (Eiffel for example).

BM does not support redefinition or any selection / redefine / undefine. It only supports overloading of an implementation (you can't redefine the declaration sheme of a function / method as well, the types must be the same for any overriden version)


H&K(Posted 2006) [#6]
If you are going to use fields of the same name, then you have to make sure that the object is cast to the right type.

In your example you have cast your type C to an A, so it trys to use the field in A. If you recast it back to a C, or make Me a C in the first place, you should get the value you expect


Jesse(Posted 2006) [#7]
@ perturbatio: no, Me is supposed to be a parameter to a function that handles types b and c like this:
Type a
	Field z%
End Type
Type b Extends a
	Field z% = 2
End Type
Type c Extends a
	Field z% = 3
End Type


function handleit(it:a)
 print it.z
end function

Local me:c = New c
handleit(me)
you:b = new b
handleit(you)



tonyg(Posted 2006) [#8]
In that case you're 'upcasting' 'me' and 'you' to object 'a' so it will use the 'z' from type 'a'.
I don't really know this kind of stuff but I think you have to use...
Function handleit(it:Object)
		If b(it) Print b(it).z
		If c(it) Print c(it).z
end Function

There's been a few posts on this but I can' remember the right terminology to sugges a search.
<edit> Apologies if I've missed the boat.


Dreamora(Posted 2006) [#9]
Thats totally morronic.
The following source works and that although this is definitely no expected behavior nor anything near usefull / desired. It totally defeats the use of inheritance if dublicated field names are seperated instead of overridden

Type a
	Field z%
End Type
Type b Extends a
	Field z% = 2
End Type
Type c Extends b
	Field z% = 3
End Type
Local me:a = New c
handleit(me)
End

Function handleit(it:a)
 Print it.z
 If b(it)	Print "B: " + b(it).z
 If c(it)	Print "C: " + c(it).z
End Function


EDIT: This would already be the second type based compiler error on 1.22 hopefully the last one (the other is calling functions from type instances which then have a self)


Jesse(Posted 2006) [#10]
I guess I am going Dreamora's way is simpler and does what I want.
I am not out of the woods with this topic but I think I understand it better.


Jesse(Posted 2006) [#11]
this is the silly code I was working on. not yet fixed or complete. it is a math drill program for my second grade daughter to better her skills. just use your own font :



HrdNutz(Posted 2006) [#12]
lol, guys, your Polymorph doesnt work cuz ur sorceress doesnt have enough mana to cast it, lol.


Jesse(Posted 2006) [#13]
WORD!