Type fruit is extended to citrus then lemon which?

BlitzMax Forums/BlitzMax Beginners Area/Type fruit is extended to citrus then lemon which?

Burl(Posted 2012) [#1]
How do I figure out what the object is?


Type fruit
end type

type citrus extends fruit
end type

type lemon extends citrus
end type

Functon WhichIsIt:int(f:fruit)
'
' so I want to know if the input variable f is citrus or a specific lemon?
' is there a kindof() function that will tell me or do I
' have to write a ThisIsA method in each type?
'
end function

Thanks
yours
Burl

Last edited 2012

Last edited 2012


Yasha(Posted 2012) [#2]
You can do this (i.e. try to downcast):

Local l:Lemon = Lemon(f)
Local c:Citrus = Citrus(f)


If f is not a Lemon, "l" will be null. If it is not a Citrus, "c" will be null. In this example you'd have to check them in that order though, since if "f" is a Lemon, it must also be a Citrus.

In general though, as far as possible you should try to avoid writing code that needs to downcast values; ideally code should only operate on values whose type it knows (you do have to do this sometimes in BlitzMax, but not often, probably not as often as you think).

Last edited 2012


Burl(Posted 2012) [#3]
Thanks Yasha. That helps but what i really want to know is, this:

function IsSameKind:boolean(f:fruit,g:fruit)

so if they are both lemons or both fruits etc it returns true.

Thanks for your first answer too.

Obviously they are both objects and both fruits, but if one is a fruit and the other a lemon I want a false. As a last point, I don't want to use Lemon in my code since fruit may get extended to something else again after the code is writen.

yours
Burl

Last edited 2012

Last edited 2012


matibee(Posted 2012) [#4]


I'll accept any critisism Yasha, my OOP-foo is failing me in my old age.

Also, doesn't it make sense that "citrus" should also be an abstract type? You can't make a fruit that is only a citrus, so you shouldn't be able to instantiate one.


matibee(Posted 2012) [#5]
An improved version with another fruit group (berries) and another fruit (strawberry).
the citrus and berry group types can no longer be instantiated (ie "New citrus" isn't allowed because it's an abstract type.)



Last edited 2012


N(Posted 2012) [#6]
.

Last edited 2012


N(Posted 2012) [#7]
.

Last edited 2012


H&K(Posted 2012) [#8]
Type Lemon

Field ImA:string = "Lemon"
etc

If Global Fields override, even better.

Last edited 2012


Burl(Posted 2012) [#9]
Thanks for all the help.

yours
Burl