Interfaces

BlitzMax Forums/Brucey's Modules/Interfaces

Brucey(Posted 2015) [#1]
The latest bcc and brl.mod for NG now support Interfaces.

Interfaces can be created using the following :
Interface ImyInterface
  Method myMethod()
End Interface

Type TmyType Implements ImyInterface
  Method myMethod()
    ' implement here
  End Method
End Type


Interfaces can be extend other interfaces :
Interface ImyOtherInterface Extends ImyInterface
  Method anotherMethod()
End Interface


Types can implement multiple interfaces :
Type TmyBigType Implements ImyInterface, IanotherInterface
  Method myMethod()
  End Method

  Method moreMethod()
  End Method
End Type


Abstract types which implement an interface can elect not to implement those methods. Its concrete subclass however, will need to implement any methods in the hierarchy that haven't.

The preferred naming convention is to prefix interface names with I (uppercase i), much in the same way that types are generally prefixed with T, but of course you can name your stuff however you like ;-)

[technical] The implementation does away with three "reserved" function pointers that were attached to each Type, replacing them with some vtable stuff.

If you have any problems with it, please let me know. Breakable examples are always useful in helping to solve issues!

:o)


Brucey(Posted 2015) [#2]
I should add that this implementation is more in the style of Java Interfaces, rather than Monkey's which is lacking in several areas.
I assume the "broken" implementation in Monkey is down to some of the targets it is trying to support, whereas in BlitzMax we just don't care about stuff like that and implement things as they should be ;-)


Silver_Knee(Posted 2015) [#3]
Christmas is coming early this year. This must make it into the official release and replace TMap and TList with interfaces.

Also I'd love to create a IHashable interface that will have a hashCode:Int() method and somehow force String to implement it, so we can have a THashMap for BlitzMax.


Brucey(Posted 2015) [#4]
I've now added reflection support for interfaces. There's an example here : https://github.com/bmx-ng/brl.mod/issues/22#issuecomment-120739956


markcw(Posted 2015) [#5]
Excuse my ignorance Bruce... but what are the benefit of Interfaces over what we have in Bmx already? Do they make wrapping C++ any easier?

Also, I had an issue recently, where I added a NewObject function to TEntity where it was instantiating a new TEntity but because there was abstract methods CopyEntity/Update in TEntity the compiler refused since the whole Type was abstracted. I just commented out the abstracts. I had to add NewObject to TEntity. So the point is why have abstract at all, if all it does is create compiler warnings?

Great work by the looks of things though.


Brucey(Posted 2015) [#6]
Think of an interface like a behaviour. If you want your object/type to take on the behaviour of something completely unrelated, you only need to implement that interface, rather than try to work out how you would create some special super Type that hashes different, but unrelated, methods together...

Here's a Java example : https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

And there's some more interface specific stuff here :
https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

Although the text is all about Java, it applies equally enough to the BlitzMax implementation - unlike Monkey, which is like a rather cut-down version of interfaces.


Brucey(Posted 2015) [#7]
Do they make wrapping C++ any easier?

Yes, in theory I could rewrite a chunk of wxMax to reflect the same structure of wxWidgets - there some control interfaces that I've had to merge into some Types, which makes things a bit unwieldy - and prone to errors of duplicated code.


markcw(Posted 2015) [#8]
Ok thanks. I'm not sure I'm advanced enough to ever need these.


Brucey(Posted 2015) [#9]
The accepted answer to this question explains the concept rather well : http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface


Armitage 1982(Posted 2015) [#10]
Hi Brucey,
Was passing by.

Nice addition! So you are in charge with the evolution of BlitzMax :)


GuntiNDDS(Posted 2015) [#11]
This is excellent news. I just don't know which of your modules I need to download to get this. Do you have a link?