abstraction

BlitzMax Forums/BlitzMax Beginners Area/abstraction

Yue(Posted 2015) [#1]


That offers the advantages Abstraction, my structural form of "program", that of declaring an abstract method to use then do not see much benefit.

Any ideas?


TomToad(Posted 2015) [#2]
Abstract helps when you have several different types derived from a single type. You can call the extended types Methods through the base type. Say if you had a list of enemies. You could put trolls, dragons, skeletons, etc... in the same list, but use an abstract Update method to iterate through all the enemies without the need to test what type it is first.



Yue(Posted 2015) [#3]
@TomToad
Thanks You. :)


markcw(Posted 2015) [#4]
Like TomToad says, use Abstract when you want to enforce a behaviour/function on an extended type.

I've never actually found much use for Abstract and in one case I had to actually remove it as it interfered with a function I added that created a new instance of the type, and you can't do that in an abstracted type.

The only uses it has is either when designing code to be extendible by another programmer or as a reminder you need to implement a standardized method/function - as in TomToad's example.


SidAntic(Posted 2015) [#5]
nice, I never used "abstract", but for my Tile-Map-Editor and for other future things, I will use it.

Thanks.
:---)


Yasha(Posted 2015) [#6]
it interfered with a function I added that created a new instance of the type, and you can't do that in an abstracted type.


You can deal with that by doing something like this:

Type Foo Abstract
    Function Make:Foo(a:Int, b:Int)
        Return _Foo.Make(a, b)
    End Function
    Field a:Int, b:Int
End Type

Private
Type _Foo Extends Foo
    Function Make:Foo(a:Int, b:Int)
        Local f:_Foo = New Self
        f.a = a ; f.b = b
        Return f
    End Function
End Type


Abstract is actually exactly what you want when a type has constructor functions, because that's the situation when preventing the user from calling New directly is important!


markcw(Posted 2015) [#7]
Ok I get it. Thanks!