Making a little library

Hello everybody :smiley:
I’m trying to make myself a little collection of classes that are custom to my needs. I started making a couple like Vector and Point. The problem is I want to be able to use any data type that I want, double, float, int etc. but I want to make one class that can do any of them. the problem would be the getters and setters as I would have to define what type I will return. Also I don’t want to allocate memory for things that I won’t be using in the instance of the class as it’s pretty much useless. So my question is is there a dynamic way to get around this problem without something like the code below ?


class Point{
  int nx,ny;
  double dx,dy;

  Point(int x, int y){
    this.nx = x;
    this.ny = y
  }

  Point(double x, double y){
    this.dx = x;
    this.dy = y
  }
}

Thank you in advance :slight_smile:

If your class is immutable then you could parameterise it using a generic type along these lines:

public class Point<T extends Number> {
	private T x, y;
	
	public Point( T x, T y ) {
		this.x = x;
		this.y = y;
	}
}

However this will quickly break down if you need your class to do anything vaguely useful such as addition (e.g. try using a += operator in that class!)

Check out the Point2D classes in the java.awt.geom package which does something similar.

Haha, not in java :smiley:

Here is a scala example of how to create a generic Vec2:


And don’t worry, It’s the same performance as if you’d write a Vec2 for each numeric type (that is: byte, char, short, int, float and double, or only float and double if you decide to use Fractional).

Yeah, it’s not trivial, but it’s possible in scala :slight_smile:

In java: No… not really. Try to find a code precompiler for java maybe, but in the end that’s what scala is.

Man that’s a shame. I will have to make int and double version of everything :frowning:
Thank you for your replies though. You illuminated me :smiley:

I think multiple vector types are overrated. I never needed anything else than floats and ints for vectors.

It shouldn’t be too tough to make a method for all of them. Just type one up and then CnP and adjust for each. =P

It’s not tough as coding goes. But it certainly is more elegant if I could do what I originally wanted to do. I’ll have to live with it thought :emo:

Other alternative is to write the class in c(++) where this is trivial then jni it it. But if you’re going to go to this much trouble then you might as well use scala.

With Java, it’s essentially cut and paste code, or using code generation with templates the way most primitive collection libraries do it.


public class Point<T extends Number> {
   private T x, y;
   
   public Point( T x, T y ) {
      this.x = x;
      this.y = y;
   }
}

Isn’t another problem with the generics approach is that now you are dealing with Float, Integer, and Double rather than their primitive counterparts?

Not to side track the discussion, but I have toyed with the idea of separate semantically meaningful vector classes (such as units of measure). Ideally you would get a compile time error if you tried to add a Velocity to an Acceleration or something like that.

I haven’t thought of a way to do it in Java that reads nicely and has good performance, and isn’t a hassle. Phantom types get you a part of the way there but type erasure is a pain. Scala’s value classes looks to be a nice approach.

If you write the class in scala, you may want to add the @specialized annotation to the type parameter, which basically creates instances for primitive types.


class Vec2[@specialized T](val x: T, val y: T)(implicit num: Fractional[T]) {
  // ... etc
}

Truth is, I don’t know how compatible it is with typeclasses though, and I don’t have scala on this box, so try it out?

Just write a little more code and save yourself the hassle of unexpected bugs and incompatibilities.

Thanks! I had almost given up on what those carrots meant in java! Then I just randomly find this!