www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What's the order of argument evaluation for functions

reply Jens Mueller <jens.k.mueller gmx.de> writes:
Hi,

on page 50 TDPL states
"All arguments are evaluated left to right before fun gets invoked."
That means
int i;
writeln(++i, ++i);
is supposed to print
"12".
and is valid.
But on http://www.digitalmars.com/d/2.0/expression.html
func(++i, ++i);
is defined as an error.
Is the evaluation order for function calls undefined or it is left to
right?

Jens
Apr 27 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 27 Apr 2011 16:22:45 -0400, Jens Mueller <jens.k.mueller gmx.de>  
wrote:

 Hi,

 on page 50 TDPL states
 "All arguments are evaluated left to right before fun gets invoked."
 That means
 int i;
 writeln(++i, ++i);
 is supposed to print
 "12".
 and is valid.
 But on http://www.digitalmars.com/d/2.0/expression.html
 func(++i, ++i);
 is defined as an error.
 Is the evaluation order for function calls undefined or it is left to
 right?

Both :) The answer is, the current spec says it is an error. However, TDPL is usually considered to be what the spec *will* be when D2 is completed. So expect the left to right evaluation in the future. It's likely that the compiler doesn't currently conform to TDPL. -Steve
Apr 27 2011
parent reply Don <nospam nospam.com> writes:
Jens Mueller wrote:
 Steven Schveighoffer wrote:
 On Wed, 27 Apr 2011 16:22:45 -0400, Jens Mueller
 <jens.k.mueller gmx.de> wrote:

 Hi,

 on page 50 TDPL states
 "All arguments are evaluated left to right before fun gets invoked."
 That means
 int i;
 writeln(++i, ++i);
 is supposed to print
 "12".
 and is valid.
 But on http://www.digitalmars.com/d/2.0/expression.html
 func(++i, ++i);
 is defined as an error.
 Is the evaluation order for function calls undefined or it is left to
 right?

The answer is, the current spec says it is an error. However, TDPL is usually considered to be what the spec *will* be when D2 is completed. So expect the left to right evaluation in the future. It's likely that the compiler doesn't currently conform to TDPL.

I see. I never seen it like this. So all documentation on http://www.digitalmars.com/d/2.0/ specifies how it is right now but not what it will be. And by 'usually' you mean that TDPL can be in error. So I should rely on TDPL exclusively. Even though I find the online documentation easier to search.

It's slightly more complicated. The online docs are what it will be. But, some decisions were made shortly before the publication of TDPL which are not yet reflected in the online docs. But a few decisions made after the publication of TDPL _are_ included in the online docs. Generally they are things which TDPL doesn't mention. (TDPL was intentionally silent or vague on a few things, because they hadn't been decided).
Apr 27 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Don:

 It's slightly more complicated. The online docs are what it will be.

I think few days ago Andrei has said something different about expressions with side effects (I think more like TDPL than like the site). In Ada most things are defined, and Ada compilers conformance is verified with a very large amount of official tests, so I have hope for a good result of this situation in D2 too :-) Bye, bearophile
Apr 27 2011
prev sibling next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Steven Schveighoffer wrote:
 On Wed, 27 Apr 2011 16:22:45 -0400, Jens Mueller
 <jens.k.mueller gmx.de> wrote:
 
Hi,

on page 50 TDPL states
"All arguments are evaluated left to right before fun gets invoked."
That means
int i;
writeln(++i, ++i);
is supposed to print
"12".
and is valid.
But on http://www.digitalmars.com/d/2.0/expression.html
func(++i, ++i);
is defined as an error.
Is the evaluation order for function calls undefined or it is left to
right?

Both :) The answer is, the current spec says it is an error. However, TDPL is usually considered to be what the spec *will* be when D2 is completed. So expect the left to right evaluation in the future. It's likely that the compiler doesn't currently conform to TDPL.

I see. I never seen it like this. So all documentation on http://www.digitalmars.com/d/2.0/ specifies how it is right now but not what it will be. And by 'usually' you mean that TDPL can be in error. So I should rely on TDPL exclusively. Even though I find the online documentation easier to search. Jens
Apr 27 2011
prev sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Don wrote:
 Jens Mueller wrote:
Steven Schveighoffer wrote:
On Wed, 27 Apr 2011 16:22:45 -0400, Jens Mueller
<jens.k.mueller gmx.de> wrote:

Hi,

on page 50 TDPL states
"All arguments are evaluated left to right before fun gets invoked."
That means
int i;
writeln(++i, ++i);
is supposed to print
"12".
and is valid.
But on http://www.digitalmars.com/d/2.0/expression.html
func(++i, ++i);
is defined as an error.
Is the evaluation order for function calls undefined or it is left to
right?

The answer is, the current spec says it is an error. However, TDPL is usually considered to be what the spec *will* be when D2 is completed. So expect the left to right evaluation in the future. It's likely that the compiler doesn't currently conform to TDPL.

I see. I never seen it like this. So all documentation on http://www.digitalmars.com/d/2.0/ specifies how it is right now but not what it will be. And by 'usually' you mean that TDPL can be in error. So I should rely on TDPL exclusively. Even though I find the online documentation easier to search.

It's slightly more complicated. The online docs are what it will be. But, some decisions were made shortly before the publication of TDPL which are not yet reflected in the online docs. But a few decisions made after the publication of TDPL _are_ included in the online docs. Generally they are things which TDPL doesn't mention. (TDPL was intentionally silent or vague on a few things, because they hadn't been decided).

Then the documentation should clarify that function arguments are evaluated left to right. I'm going to have an eye on the online documentation reporting mismatches to TDPL and report them in the component 'websites' on http://d.puremagic.com/issues/. Even better I can fix them myself and create a pull request for d-programming-language.org. In the above case http://d-programming-language.org/function.html and http://d-programming-language.org/expression.html should be fixed. Is d-programming-language.org going to be the only official source for those information? Will http://www.digitalmars.com/d/2.0/ be deprecated or will it always mirror d-programming-language.org. Jens
Apr 27 2011