4

I’ve come across this blog article: Implicit Conversion Operators are Bad. The article discourages the use of implicit conversion with reference types. The article describes problems caused by implicit conversions of reference types, which boils down to the following

[...] main question of this post: why are implicit conversion operators bad on reference types? [...] By adding an implicit conversion operator to a reference type, you have now introduced value semantics to assignments – but only from one type to another.
[emphasis original]

My question, however, is about value types. In the beginning, the article has got this statement:

Implicit conversion operators are incredibly important to the language, but only for value types.
[emphasis original]

The article doesn’t go into details on how to use implicit conversion with value types. That is what I’m wondering about. Here's my present thinking:

  • The conversion operator is on a value type, and it returns a value type. Implicit - okay.
  • The conversion operator is on a reference type, and it returns a value type. Implicit – okay.
  • The conversion operator is on a value type, and it returns a reference type. (That would be a somewhat bizarre, but would it be wrong?) Implicit – not okay.
  • The conversion operator is on a reference type, and it returns a reference type. Implicit – not okay.

In a more compact form:

  • If it returns a value type, an implicit conversion operator is okay on both value type and reference type.
  • If it returns a reference type, implicit conversion is not okay.

Is this correct? Is this complete?

p.s. For the most part, I work mostly with C and C++, where I have fine control over things like const, pointers, references. Of course, that comes at a risk of contracting nasty pointer-related bugs.

3
  • If it returns a value type, an implicit conversion operator is okay on both value type and reference type. By the author's reasoning, a reference type should never have an implicit conversion. An implicit conversion from a reference type to a value type still changes the semantics of assignment in the exact same way that an implicit conversion from a reference type to another reference type does.
    – Doval
    Oct 15, 2014 at 19:23
  • What does "conversion" mean? See Eric Lippert's blog post on Representation and Identity to find out.
    – rwong
    Dec 14, 2014 at 22:45
  • I am working with a 3d-party library that misuses inplicit conversions badly: there are conversions from int to their Duration class, from string to their XPath, etc. The bad thing is that they also use a lot of overloaded methods like do(int), do(string), do int(int, XPath),... and it is a nightmare when I want to call specific overload.
    – ne2dmar
    Jan 9, 2015 at 8:14

2 Answers 2

1

The short answer is that implicit operators are mostly about math.

All other things being equal, method calls are easier to read, easier to maintain, and easier to reason about, for non-math operations. Operators work with math because they are well understood; everyone knows what an addition operation is and what operator represents it. And when it's not being used in a math context (i.e. string concatenation), it still makes perfect sense semantically.

In the absence of math operations (i.e. reference types), operators work best when they are part of the language proper. The Lambda Expression operator in C# => is part of the language specification, is well-understood, and covers a broad variety of use cases. But it doesn't mean the same thing in other languages, like the + operator usually does.

3
  • How are math operators like + related to implicit conversions?
    – svick
    Oct 18, 2014 at 12:32
  • 1
    @svick: You can get away with implicit conversions with math operators because those kinds of conversions will be universally understood by most people. Sorry I didn't make that clearer. Oct 18, 2014 at 16:03
  • Disagree. While math is the most commonly used area, it has other uses. Jan 9, 2015 at 0:08
0

If you stick to immutable reference types, they have the same value semantics as value types, and the restrictions disappear. I have never used conversion operators for mutable reference types, as I only use them in cases where value semantics makes more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.