jaylib icon indicating copy to clipboard operation
jaylib copied to clipboard

better constructors

Open electronstudio opened this issue 5 years ago • 7 comments

Javacpp creates noarg constructors and expects you use the fluent methods to initialize, e.g.: new Vector3().x(16).y(8).z(16)

It would be nicer for Java programmers if we could make a constructor so you could do: new Vector3(16,8,16)

I know how to do this as a helper method, but I don't know how to insert it as a constructor into the generated Raylib.java source.

electronstudio avatar Apr 03 '20 08:04 electronstudio

I have created Jaylib.Vector3 which inherits from Raylib.Vector3 but has additional constructors. I suggest creating similiar subclasses of the autogenerated classes.

electronstudio avatar Apr 09 '20 13:04 electronstudio

Using subclasses can get confusing, because the library functions dont return the subclasses. Might be better to create helper functions and get rid of the subclasses?

electronstudio avatar Jul 02 '21 14:07 electronstudio

Alternative would be to create subclasses for everything, and create wrapper around the library functions that use the subclasses. Result would be ideal but it's work that would have to be updated every time Raylib is updated, and the goal of this project is to automate things so there is no work.

electronstudio avatar Jul 02 '21 14:07 electronstudio

JavaCPP does not generate constructors. The recommended JavaCPP way is like this:

 var vec = new Vector3().x(1).y(2).z(3);

I made some sub classes in Jaylib.java. To use those:

import com.raylib.Jaylib.Vector3;
...
var vec = new Vector3(1, 2, 3);

I made some constructor functions in Jaylib.java. To use those:

import static com.raylib.Jaylib.Vector3;

var vec = Vector3(1, 2, 3);

I'm undecided which of these methods is cleanest but I recommend the first because it's autogenerated by JavaCPP.

electronstudio avatar Jul 03 '21 23:07 electronstudio

I'm thinking the helpers are not actually a good idea, because they make it easy to think you're dealing with Java objects, when actually these are chunks of memory allocated on the native heap. The GC behaviour is different and the JVM won't be able to do boxing, escape analyse, etc, optimizations on them. The unusual syntax helps to remind me of that.

electronstudio avatar Nov 19 '21 07:11 electronstudio