Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

FxMazter

[java] Type problems?

This topic is 5229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I am wondering how I could accomplish this in java:
interface Type {
	void setValue(Object value);
	Object getValue();
	Object getTypeId();
}

class Int implements Type {
	int m_nInt;
	
	public Int() {
		m_nInt = 0;
	}
	
	public Int(int nInt) {
		m_nInt = nInt;
	}
	
	public void setValue(Object value) {
		//m_nInt = (int)value;

	}
	
	public Object getValue() {
		//...

	}
	
	public Object getTypeId() {
		//...

	}
}
Now what I want is to have a common interface for all available types that I create. But the problem is that I can't let the getValue() return types like int,float etc... since they don't inherit from the Object class. I did something similar in c++, but then I didn't use any interfaces, but templates instead. Now this sollution with the interface is flawed, since it doesn't work for any type... This is what I want to be able to do: Type type = new Int(10); if(type instanceof Int) int test = type.getValue(); It's actually for a scripting engine I want to make, where I want to allow syntax like this: Int tmpVar = 10; Now I parse this line and check if "Int" is a registered type, and if it is, I retrieve the TypeCreator that corresponds to the "Int" id in a Hashtable. Then the TypeCreator object is supposed to have a function that returns a new object. something like this:
interface TypeCreator {
	//I want this to be able to return any type - int, String, float etc...

	Object createTypeObject();
	// This is supposed to: return new int; //return new String; //etc...

	// But unfortunately this is not possible... 

}
Then I use the retrieved TypeCreator and create a new object, which I together with the variable name "tmpVar" insert to a Hashtable that contains variables the scriptfile has access to... So, does anyone have any clue on how to accomplish this? Would be very greatful [edited by - FxMazter on February 28, 2004 1:55:35 PM] [edited by - FxMazter on February 28, 2004 2:11:20 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I think the piece you are missing is that there are classes made to wrap all of the basic types, check out the api:
java.lang.Integer
java.lang.Float
java.lang.Double
java.lang.Short
java.lang.Long
java.lang.Character
etc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
But why are you even doing this in the first place? This seems like a really bad design for a java program. Can you explain what you''re trying to do with this system, so that we possibly offer a better alternative?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
(This is the first AP)
In response to 2nd AP, he pretty thoroughly described what he''s trying to do. I don''t think this is bad program design at all. There are TONS of interfaces in the API that generically use Object as paramaters and returns, because that way they can be flexible, and rely on the programmer to keep track of casting. One word of caution to the O.P. if this needs to be fast, both casting and the keyword instanceof are relatively slow.

Share this post


Link to post
Share on other sites
Well, thank you guys for reading this. Since the AP2 mentioned that it's slow with all the casts and all, I realized that the way I was going was not the one to go. Since I'm making a script engine, it needs to be in acceptable speed.

Anyway, I have changed the design. Now it works like this:

1. I read a Script file and send it to the ScriptEngine for parsing.

2. The ScriptEngine reads the file and checks if for the syntax that I have specified...

3. The problem I was having was along the parsing, I wasn't entirely sure how to make the parsing system. For eg. The problem with the type syntax I was having:

//I want syntax like this to be possible in my Script file
Int myVar = 10;

At first I thought of having it like this:
1. Check if "Int" is a registered type, In the Hashtable containing the allowed typed.
2. If it is there then get the TypeCreator object that is associated with that type. Then use TypeCreator.create() to create an object of the specified type - "Int".
3. Assign this object the value comming after the assignment operator "=", also registered as an operator in my ScriptEngine.
4. Register the created object together with the variable name - "myVar" in a Hashtable containing created variables...

Anyway, at last I managed to have it working all right with some typecasting , but as AP2 mentioned it would be better without the typecasting.

So I now changed the method of using the TypeCreator to storing a TypeParsing object instead. In the HashTable with all the registered types, I now have a TypeParsing object that has a parseThisType(sName,sAssignment) functions. Now instead of creating an object I send the data to this function, which creates the object of a specified type and assigns it a value parsed from sAssignment - "= 10;". Then stores it in a Hashtable containing registered variables. This way there is no need for typecasting, but instead there is some more coding each time adding a new type to the system... which probabilly is not such a big cost

Thx anyway guys!






[edited by - FxMazter on February 28, 2004 7:46:03 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
(This is the first AP)
In response to 2nd AP, he pretty thoroughly described what he''s trying to do. I don''t think this is bad program design at all. There are TONS of interfaces in the API that generically use Object as paramaters and returns, because that way they can be flexible, and rely on the programmer to keep track of casting. One word of caution to the O.P. if this needs to be fast, both casting and the keyword instanceof are relatively slow.


Type casting on primitives are slow because they are actually converted to the new type. Casting on objects is not as slow because all it checks is the inheritance tree to see if the type is allowed, but it does not actually transmute the object.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
Actually with version 1.5 of java you will be able to use generics, java''s version of templates, plus the auto boxing/unboxing of primitives which allows for code like:

Integer i1 = 3;
int i2 = i1;


You can get the beta version from java.sun.com rigth now

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!