comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: why Ada type casting is different from other languages? newType(value) vs. (newType)value
Date: Mon, 14 Jul 2014 08:25:42 -0700 (PDT)
Date: 2014-07-14T08:25:42-07:00	[thread overview]
Message-ID: <cbfb8648-9b91-4b02-bfc6-e9389b949f33@googlegroups.com> (raw)
In-Reply-To: <lpsoun$5b0$1@speranza.aioe.org>

On Saturday, July 12, 2014 6:55:24 PM UTC-7, Nasser M. Abbasi wrote:
> I am just wondering what is the rational of Ada choosing to use
> 
>      type(value)
> 
> vs
>
>      (type)value
>
> to do typecasting. does not type(value) appear as a function call?
> 
> as in 
> 
>       i := float(9);
> 
> vs.
> 
>       i := (float) 9;
> 
> Just wondering on this choice and its advantage over the more
> common syntax.

The only reason the latter is "more common" is because a lot of languages have based their syntax on C's, and that's the syntax Ritchie decided to use.  I don't know of any language that isn't based on C syntax that uses this syntax for a cast.  As for the languages that do use that syntax, I don't think the designers made a conscious choice to use the (type)expression syntax--i.e. they didn't look at the possibilities and decide that (type)expression was better.  Rather, they just decided at the beginning to use C syntax, copied most of the syntax, and then perhaps made a few tweaks here and there.

Ironically, C++ now allows the type(expression) syntax for type conversion.  Technically, it's not a type conversion, but rather a one-argument constructor for an object of type "type" with "expression" as the argument.  But the language definition has defined implicit one-argument constructors for primitive types, so that the effect is the same.  Sometimes.  Actually, I think the semantics are subtly different, and there are times when (type)expression is legal where type(expression) is not, and sometimes you need to use dynamic_cast<type>(expression) or static_cast<type>(expression).  In fact, I think the (type)expression syntax isn't supposed to be used any more, and static_cast<type>(expression) is preferred, at least according to some authorities.  As someone I know likes to say, "If you're not confused, you're not paying attention." 

I don't like the (type)expression syntax because I can never remember what the precedence is.  This is especially a problem for (type)object.method(args), because I have to look up whether the type conversion applies to just the object or to the result of the function call.

In general, questions such as this are meaningless, because for the most part they come down someone's style preference.

                                -- Adam

  parent reply	other threads:[~2014-07-14 15:25 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-13  1:55 why Ada type casting is different from other languages? newType(value) vs. (newType)value Nasser M. Abbasi
2014-07-13  3:01 ` Shark8
2014-07-13  6:26 ` Jeffrey Carter
2014-07-13 14:33   ` Dan'l Miller
2014-07-13  9:01 ` Niklas Holsti
2014-07-13  9:08   ` Dmitry A. Kazakov
2014-07-13 10:20     ` AdaMagica
2014-07-13 10:24     ` Georg Bauhaus
2014-07-14 23:36       ` Randy Brukardt
2014-07-15 18:42         ` G.B.
2014-07-15 19:14           ` Niklas Holsti
2014-07-15 22:47             ` Georg Bauhaus
2014-07-15 19:19           ` Jeffrey Carter
2014-07-15 23:07             ` Georg Bauhaus
2014-07-15 22:00           ` Randy Brukardt
2014-07-13 14:42 ` Dan'l Miller
2014-07-14 15:25 ` Adam Beneschan [this message]
2014-07-14 16:24   ` G.B.
2014-07-14 17:41     ` Simon Wright
2014-07-14 19:41     ` Simon Clubley
2014-07-14 20:51       ` Dan'l Miller
2014-07-14 23:23       ` Randy Brukardt
2014-07-15 19:22 ` Marius Amado-Alves
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox