Chris Pratt
Chris Pratt
@chris-pratt
10 years ago

There's no reason that you can't do
public class Box<Width,Height> {
Width width;
Height height;
}

Which would allow you to instantiate it like:

Box<Integer,Long> box = new Box<>();

But I'm guessing what you're looking for is something different??
(*Chris*)

Endi Sukaj
Endi Sukaj
@endi-sukaj
10 years ago
@chris-pratt 

This seems close to what the OP was looking for, but it still requires types, doesn't it?

P. Werner
10 years ago
@chris-pratt 

You are right I'm looking for something different.
What you've written will only make the width an integer and the height a long. What I want is something like :
Box<5, 7>

Endi Sukaj
Endi Sukaj
@endi-sukaj
10 years ago
@GG:105032262254569963811 

Can't you do it with a constructor?

P. Werner
10 years ago
@endi-sukaj 

Using the constructor is somewhat limited. The beauty of class parameters is, that you can define contracts. Let's say for example:
Box<3, 3> getACoolBox() will always return a 3 by 3 box. This is inforced by the class parameters. Ofcourse you can use the constructor to define width and height and check if the returned Box is 3x3 every time you use this method, but it's not as elegant.
What I was searching for is a solution to inforce the length of an ID without destroying the reuseability of the ID class itself. To inforce the length we would check if the parameter given to the constructor of the ID has the appropriate length. But then I need to hard code the maximal length into the class itself ( you wouldn't pass the length into the constructor that's absurd) which makes the class unreuseable. If I for instance now want to have an ID that has another length then the hard coded one. I would need to create another class.

Endi Sukaj
Endi Sukaj
@endi-sukaj
10 years ago
@GG:105032262254569963811 

Gotcha. Unfortunately I don't think it's possible to do that in Java, you may be forced to use class constructors.

P. Werner
10 years ago
@endi-sukaj 

I've found a solution for the problem myself(see options). It's kind of a dirty one but it works.

Endi Sukaj
Endi Sukaj
@endi-sukaj
10 years ago
@GG:105032262254569963811 

I just saw the solution. Seems like it works, but it's a bit complicated though. You think it's worth the trouble?

P. Werner
10 years ago
@endi-sukaj 

I don't think so unfortunately. I guess I'll hard code the length for the ID, since it will be used throughout my API and I don't want do confuse users of the API with things like this. It's also somewhat limited, since you can't just do something like Lenght8*Lenght5 an have a new type argument.

Endi Sukaj
Endi Sukaj
@endi-sukaj
10 years ago
@GG:105032262254569963811 

That seems like the only way unfortunately, sorry you couldn't get a better answer.

Endi Sukaj
Endi Sukaj
@endi-sukaj
10 years ago

Hey, thanks for contributing. Though I am not entirely sure that this is what the user who asked the question is looking for. I may be wrong though :D

http://www.slant.co/topics/5339/comments#3

Izem Lavrenti
Izem Lavrenti
@ILavrenti
10 years ago

Hey, mind giving some more details on what you are looking for? I'm not really understanding the question :)

P. Werner
10 years ago
@ILavrenti 

Yes of course.
In C++ you can have non-type parameters for classes like for example :
template<int length> class X { ... }
In Java we have generics like:
class X< T >
But the gernic arguments are always types.
The question now is, if there is a possible workaround to have something like:
class X< int length >
Which would be extremely useful. Thinking of Matrices for example :
class Matrix <int width, int height>

Izem Lavrenti
Izem Lavrenti
@ILavrenti
10 years ago
@GG:105032262254569963811 

I'm not sure that can be achieved in Java, the whole point of generics in Java is to enable types to be parameters when defining classes etc... AFAIK there's no way to use formal parameters in generics in Java. But don't take my word for it, maybe someone more knowledgeable can come in and prove me wrong :)

Endi Sukaj
Endi Sukaj
@endi-sukaj
10 years ago
@GG:105032262254569963811 

Can't you do achieve what you are trying to do with class constructors?

undefinedurl nextprice drop