• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Casting?

 
ian bert
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im building a generic Variable object for use in a parsed language system.
To make it versatile, the Variable object has a Data object that contains the actual variable data (no way right?).
The problem being that when I want to get the data, I am using a method, getData(), that returns an Object; meaning that I have to cast it later where I want to do things with that data.

Being somewhat new to OOP, I cant figure out a way to dynamically cast the object Data either at the class level, or the return level of getData().

I assume something like this is possible?

here is the Variable class in all its glory, with the important parts pointed out.
 
Campbell Ritchie
Sheriff
Posts: 48376
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All those tests for class look grim to me. There is a rather similar phenomenon called “dispatch code” in this thread. You can see that you have awkward code, and it is restricted; what will happen if you pass a char/Character?
Can you parametrise your data objects. so it becomes public class Variable<T>? Then you replace Object by T throughout. Have a look at the Java Tutorials; there is another section about generics somewhere else in the tutorials.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're misunderstanding casting. This bit here:
ian bert wrote:
Does absolutely nothing at all.

Casting doesn't change an object (when you're talking about reference variables). It simply allows you to change the type of a reference.

So someClass.cast(this.getData()) has no effect on your data object. All it does is return a variable of type someClass referencing the data (and will throw an exception if that's not possible). So unless you're assigning the return value to a variable of that type it's pointless.

I'd agree with Campbell - I'd be looking to use a generic class Variable<T> here.
 
Campbell Ritchie
Sheriff
Posts: 48376
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might do well to make your class immutable. Make all the fields final, get rid of the setXXX methods, initialise all the data in the constructor, return defensive copies of all data from any getXXX methods, and make the class final, too, spo as to prevent subclassing.
Also, I wouldn’t allow null values into my object if I can avoid it. Get rid of the overloaded constructor which permits nulls.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ian bert wrote:Being somewhat new to OOP, I cant figure out a way to dynamically cast the object Data either at the class level, or the return level of getData().
I assume something like this is possible?

Possibly, with reflection, but I'd advise strongly against it.

I guess my question back to you is: How do you plan to use it?

At the end of the day you have a Variable that contains an Object (its data) of some unknown type, so no matter how much fancy code you put into your Variable class, anything that uses it is still going to have to determine for itself what it contains (Ugh).

You could mitigate it somewhat by adding a generic type to the Variable class definition, but that only helps if the client knows what to expect.

I think it's time to back up and explain exactly what you want this "variable" for.

Winston

[EDIT] PS: For type checking, you're usually better off using instanceof, rather than getClass(). For one thing, it's quicker; for another, it handles nulls.
 
Campbell Ritchie
Sheriff
Posts: 48376
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For once, I shall agree and say instanceof is better. It tests whether you can safely cast something to a particular class, so it picks up subclass instances too.

I still think making that data class parametrised is a better solution. If you get the wrong type, the compiler will tell you rather than letting it suffer Exceptions at run-time.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I still think making that data class parametrised is a better solution. If you get the wrong type, the compiler will tell you rather than letting it suffer Exceptions at run-time.

I agree in general, but in ian's case, we still have no idea what he intends to do with the "data" he gets from Variable.getData().

If it's anything that's type-dependent, then anything that uses it is still going to need either (a) reflection, or (b) some form of dispatch to actually use the data, unless you want to get into something like Strategy matrixes for each use (again: Ugh).

Winston
 
ian bert
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of the Variable object is to serve as a variable in a parsed programming system.
Previous incarnations of the system used string arrays to keep the declared variables, and a system of parseBlah() to get numbers and such. Such a method was extremely tedious and felt the brunt of my programming fury, instead of the more important areas, like the math functions.

I just messed around with Campbells idea to parametrise the class to Variable<T>, which solved a lot of problems but seems to create new ones, mainly the fact that the type, T, must be stated explicitly.
Perferably I'd like a way to do this psudo code


like I said, Variable will be used in its own system, and needs to be extremely dynamic (or at least the system itself does)

Thanks for the help
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ian bert wrote:The point of the Variable object is to serve as a variable in a parsed programming system.

Not quite sure what that means, but whatever.

Previous incarnations of the system used string arrays to keep the declared variables, and a system of parseBlah() to get numbers and such. Such a method was extremely tedious and felt the brunt of my programming fury, instead of the more important areas, like the math functions.

I just messed around with Campbells idea to parametrise the class to Variable<T>, which solved a lot of problems but seems to create new ones, mainly the fact that the type, T, must be stated explicitly.

What's the problem with that? Surely when you create your Variable, you know what type you want to put in it?

Perferably I'd like a way to do this psudo code

I don't think you really need that. See above.

like I said, Variable will be used in its own system, and needs to be extremely dynamic (or at least the system itself does)

And it's that part that really bothers me. I'm in agreement with Campbell about the generic type, but I don't see how it helps you when you come to use it. You're still likely to end up with some kind of method that takes a Variable<?>, and have to divine what type it contains.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic