« | Main | »

Pure native interfaces

By Jewe | October 29, 2012

About the purpose of pure native interfaces.

It is possible to declare interfaces using the native modifier keyword. This will make the interface pure native, meaning script classes can not implement this interface. Here is an example:

// declare an interface which can only be implemented by native types:
native interface NoiseGenerator
    method float Random(const float x);

The basic idea of interfaces in JewelScript was to make them fully transparent. Meaning, it should not matter whether the implementing class was native or implemented in byte-code. And when using the JewelScript API to call an interface method, this is actually the case.

However, I have noticed that this transparency is not always what you want, and it is not always feasible to use the JewelScript API to call a method of your native class. Sometimes, you just want to get a pointer to your native object and make a direct call to it’s methods.

While you always had that option, there was a potential problem: Script programmers had no knowledge whether an interface was intended only for native classes or not. So a script programmer could end up trying to pass an object implemented in script to a function expecting only native objects – which of course would fail.

By making your interface pure native, you can make script programmers aware, that this interface is not meant to be implemented in script code, and the compiler can ensure that they cannot pass a script object to a function that only works with native objects.

Topics: docs | Comments Off on Pure native interfaces

Comments are closed.