The Artima Developer Community
Sponsored Link

Weblogs Forum
Implicit Casting, Good or Evil?

6 replies on 1 page. Most recent reply: Oct 17, 2005 8:11 AM by Paul de Vrieze

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Implicit Casting, Good or Evil? (View in Weblogs)
Posted: Sep 12, 2005 7:38 AM
Reply to this message Reply
Currently Heron is not allowing implicit casting to function arguments. I am trying to decide whether this is a good thing.
In C++ you can write the following:
struct A {
 A(int n) : m(n) { }
 int m; 

void f(A x) {

void g() {
  int n = 42;
This is handy, a temporary is created which gets passed to f. In a trivial example like this there is really no confusion. When there are multiple overloads of functions, then things get increasingly confusing. In non-trivial software it is very common for even experienced C++ programmers like myself, to get caught by implicit conversion bugs.

Currently Heron prevents implicit casting through constructors. It makes the code slightly more verbose, but I wonder if this is a worthwhile price to pay for increased safety and maintainability.

Vesa Karvonen

Posts: 116
Nickname: vkarvone
Registered: Jun, 2004

Re: Implicit Casting, Good or Evil? Posted: Sep 12, 2005 8:48 AM
Reply to this message Reply
I don't want to write a longer argument on this, but based on what I've been reading between the lines from various sources (too many to even remember) it seems that implicit conversions are a bad idea (you're bound to get nasty surprises).

The jury is still out on overloading, but it is clear that overload resolution needs to be as straightforward to reason about as possible (e.g. Koenig lookup causes nasty surprises even for the experts). Essentially, you should seek to limit the effect of adding new overloads or otherwise the addition of new overloads can cause nasty surprises (ambiquity, far away changes).

I would recommend taking a look at G'Caml

and Haskell's type classes.

Tanton Gibbs

Posts: 20
Nickname: tanton
Registered: Aug, 2005

Re: Implicit Casting, Good or Evil? Posted: Sep 12, 2005 11:26 AM
Reply to this message Reply
I agree. I think Implicit Casting is evil in most cases. I actually think it is a benefit in C++ because C++ needed to be an adapter around a legacy interface, C. Implicit casts helped to make that adapter more user friendly, and therefore was a good thing. However, in a new system, without legacy concerns, there should be no need for implicit casting and only a minimal need for explicit casting. Concepts should be the tool of choice in order to obviate the most common causes of casting.

Andre Wesseling

Posts: 1
Nickname: andrewe
Registered: Sep, 2005

Re: Implicit Casting, Good or Evil? Posted: Sep 13, 2005 4:37 AM
Reply to this message Reply
C++ defaults to implicit conversions and allows the user to mark constructors with the 'explicit' keyword to prevent such conversions. You could make explicit conversions the default, but at the same time you could introduce an 'implicit' keyword that allows users to have the convenience of automatic conversions where desired.

Harrison Ainsworth

Posts: 57
Nickname: hxa7241
Registered: Apr, 2005

implicit casting/conversion -- bad Posted: Sep 13, 2005 6:53 AM
Reply to this message Reply
my vote is against implicit conversions.

and furthermore it would be good to remove them from between the primitives, like int and unsigned and float, too.

since heron primitives are not so primitive, being defined as library parts, i assume implicit conversions are at least easily removable from them. which would be good.

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

Re: Implicit Casting, Good or Evil? Posted: Sep 13, 2005 12:22 PM
Reply to this message Reply
Implicit is like magic. Magic makes for surprises and bugs. I dislike magic bloody great muggle that I am.

Avoid magic at all costs.

Paul de Vrieze

Posts: 9
Nickname: pauldv
Registered: Oct, 2005

Re: Implicit Casting, Good or Evil? Posted: Oct 17, 2005 8:11 AM
Reply to this message Reply
Actually there might be one area where implicit casting might be acceptable, and that is for literals, and when there is no ambiguity. Literal casting allows for easier definition of new types that add functionality to what existing "primitive" types allready offer. Think about arbitrary precision reals or string types.

Flat View: This topic has 6 replies on 1 page
Topic: Open Laszlo Previous Topic   Next Topic Topic: Parsing with Tree Constructors

Sponsored Links


Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use