Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Making a little library  (Read 798 times)
0 Members and 1 Guest are viewing this topic.
Offline Saiqal

Senior Newbie





« Posted 2013-06-17 12:11:59 »

Hello everybody  Cheesy
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 ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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 Smiley
Offline StrideColossus
« Reply #1 - Posted 2013-06-17 12:30:49 »

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

1  
2  
3  
4  
5  
6  
7  
8  
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.
Online matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2013-06-17 12:57:48 »

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.

Haha, not in java Cheesy

Here is a scala example of how to create a generic Vec2:
http://matheusdev.tumblr.com/post/51071594017/scala-and-extending-numeric-t
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 Smiley

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

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Saiqal

Senior Newbie





« Reply #3 - Posted 2013-06-17 14:03:57 »

Man that's a shame. I will have to make int and double version of everything  Sad
Thank you for your replies though. You illuminated me  Cheesy
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2013-06-17 14:19:28 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline tyeeeee1
« Reply #5 - Posted 2013-06-17 14:46:38 »

Man that's a shame. I will have to make int and double version of everything  Sad
Thank you for your replies though. You illuminated me  Cheesy

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
Offline Saiqal

Senior Newbie





« Reply #6 - Posted 2013-06-17 15:17:33 »

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
Offline quew8

JGO Coder


Medals: 29



« Reply #7 - Posted 2013-06-17 15:31:35 »

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.
Offline actual

JGO Coder


Medals: 23



« Reply #8 - Posted 2013-06-17 16:28:53 »

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

1  
2  
3  
4  
5  
6  
7  
8  
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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #9 - Posted 2013-06-18 17:29:22 »

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.

1  
2  
3  
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?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Ninja


Medals: 55
Projects: 2
Exp: 5 years


Make code not war!


« Reply #10 - Posted 2013-06-18 19:09:43 »

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

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #11 - Posted 2013-06-18 19:11:00 »

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

1  
2  
3  
4  
5  
6  
7  
8  
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.


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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!