2

What happened to me while programming in Java:

String str

// want to call something(), but signature does not match
something(Foo foo)

// but I have this conversion function
Foo fooFrom(String)

// Obviously I am about to create another method overload.. sigh
something(String s) {
    something(fooFrom(s));
}

But then I thought of the possibility of a "automatic type conversion" which just uses my defined conversion function fooFrom everytime a string is passed in where a Foo object is excepted.

My search brought me to the wikipedia page about type conversion with this Eiffel example:

class STRING_8
    …
create
    make_from_cil
    …
convert
    make_from_cil ({SYSTEM_STRING})
    to_cil: {SYSTEM_STRING}
    …

The methods after convert are called automatically if a STRING_8 is used as a SYSTEM_STRING and vice-versa. Somehow surprising for me I could not find any other language supporting this.

So my question: are there any other languages supporting this feature? If not, are there any reasons for that, since it seems quite useful to me? Further I think it would not be difficult to implement it as a language add-on.

phant0m
  • 16,595
  • 5
  • 50
  • 82
Cwt
  • 8,206
  • 3
  • 32
  • 27

4 Answers4

1

There is one minor point that may make the things a bit more complicated. At the moment Eiffel has a rule that conversion can be applied only when the source of reattachment is attached to an object, i.e. is not Void (not null in Java/C#).

Let's look at the original example:

something (str);

Suppose that str is null. Do we get a NullPointerException / InvalidArgumentException, because the code is transformed into

something (fooFrom (str));

and fooFrom does not expect null? Or is the compiler smart enough to transform this into

if (str == null)
    something (null);
else
    something (fooFrom (str));

?

The current Eiffel standard makes sure that such issues simply do not happen and str is not null if conversion is involved. However many other languages like Java or C# do not guarantee that and the additional complexity may be not worth the effort for them.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
0

I believe that Eiffel is not the only language to support conversion routines, but I would say that it might be one of the very few that integrate this very nicely with the rest of the language definition.

In .NET, for example, you have both op_Explicit and op_Implicit routines that can be used for conversion for languages that support them. And I believe C# does.

Manu

Emmanuel Stapf
  • 213
  • 1
  • 7
0

Type coercion (implicit conversion) is a curse and a blessing--handy in some case, but it can also backfire.

For instance, Javascript has many weird coercion rules, that can leads to bug when coercings string to number, etc.

Scala has something called "implicit" which achieves something similar (at least to me) to what you describe in Eiffel. With little surprise, they can lead to certain gotchas. But they can be also very handy, see for instance the article Pimp My Library.

Community
  • 1
  • 1
ewernli
  • 38,045
  • 5
  • 92
  • 123
0

C++ has copy constructors and assignment operator.

cyco130
  • 4,654
  • 25
  • 34