views:

306

answers:

5

Is there a way to do automatic implicit type conversion in Java? For example, say I have two types, 'FooSet' and 'BarSet' which both are representations of a Set. It is easy to convert between the types, such that I have written two utility methods:

/** Given a BarSet, returns a FooSet */
public FooSet barTOfoo(BarSet input) { /* ... */ }

/** Given a FooSet, returns a BarSet */
public BarSet fooTObar(FooSet input) { /* ... */ }

Now say there's a method like this that I want to call:

public void doSomething(FooSet data) {
    /* .. */
}

But all I have is a BarSet myBarSet...it means extra typing, like:

doSomething(barTOfoo(myBarSet));

Is there a way to tell the compiler that certain types can automatically be cast to other types? I know this is possible in C++ with overloading, but I can't find a way in Java. I want to just be able to type:

doSomething(myBarSet);

And the compiler knows to automatically call barTOfoo()

+2  A: 

The answer is short: It's possible with overloading in C++ but there is no way to do that in Java.

Martin
+1  A: 

You could overload your methods, something like this:

public void doSomething(FooSet data) {
    /* .. */
}

public void doSomething(BarSet data) {
    doSomething(barTOfoo(data));
}
masher
doSomething is a method I cannot modify, it's in an external library. But I guess writing my own overloaded methods which call the external method is an option.
davr
@davr yes, I saw that edit after I wrote my answer. I suppose that another layer in between your code and the library could be a bit of a nuisance, but it depends on how much of a nuisance barTOfoo and fooTObar is... It will also allow you to change how the library code is implemented if it's functionality changes.
masher
A: 

Overloading works in the opposite way, you declare two methods on the receiver object:

public void doSomething(FooSet data)
{
    /* .. */
}

public void doSomething(BarSet data)
{
    doSomething(barToFoo(data));
}

then thanks to Dynamic Binding (wikipedia) the right method is choosen at run-time.

Of course overloading in Java is scoped at object-level (since calls are restricted to instances or class declarations), but it works in the same way.

In your case you can try by extending the class also if it's in an external library, since it's java you should be able to do it, and add the method or by using reflection (java tutorial) to add the method dynamically.

Jack
doSomething is a method I cannot modify, it's in an external library. But I guess writing my own overloaded methods which call the external method is an option.
davr
It is not an example of Dynamic Binding by any means. It is just overloading, it happens at compile time based on declared types of variables.
vava
my fault, dynamic binding is only applied to choose methods of derived objects..
Jack
A: 

Java was created with visibility in mind. Every programmer should be able read just one line and understand what is happening there. This is why it doesn't have operator overloading, this is why it doesn't have any kind of automatic custom type conversion. So, unless you write your own wrappers around one of the library that will accept types from other library and explicitly convert them, you are out of luck there.

vava
I guess that explains why you have to use String.equals instead of == all the time...so dumb.
davr
@davr, exactly, they wanted you to spot right on is it references or actual content is being compared.
vava
A: 

You could do both together:

(1) Write wrapper methods which do conversion on the back-scenes.

(2) If your objects have proper hashCode overriden method, the wrapper methods may manage a very simple and fast cache, which you can build yourself with simple Map implementation and probably synchronization (if you use the objects concurrently at all).

That will get you rid of both converting between the two types all the time and probably of performance concerns. Even if you go against caching, I would still recommend (as other posters said) to use wrapping methods. That at least saves you lots of unnecessary typing.

Good luck.

dimitko