Jump to page: 1 2
Thread overview
[Issue 5399] New: Return the result of a nonvoid function in a void function
Jan 02, 2011
Walter Bright
Jan 02, 2011
nfxjfg@gmail.com
Jan 02, 2011
Iain Buclaw
Jan 02, 2011
nfxjfg@gmail.com
Jun 29, 2011
yebblies
Jun 29, 2011
Don
Jun 29, 2011
yebblies
Oct 09, 2011
Walter Bright
January 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399

           Summary: Return the result of a nonvoid function in a void
                    function
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-01-01 13:49:57 PST ---
In D (rightly) you can't return a value different from void inside a void
function. But this code compiles and runs with no errors in DMD 2.051 (bug
found by Daren Scot Wilson):


int foo() { return 1; }
void main() {
    return foo();
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement


--- Comment #1 from bearophile_hugs@eml.cc 2011-01-01 23:00:02 PST ---
This isn't a bug, I was wrong. D2 specs clearly show this is working as
expected:
http://www.digitalmars.com/d/2.0/statement.html

ReturnStatement:
    return;
    return Expression ;

Expression is allowed even if the function specifies a void return type. The Expression will be evaluated, but nothing will be returned. If the Expression has no side effects, and the return type is void, then it is illegal.


Indeed, this too compiles:

void main() {
    int x;
    return x++;
}


But this is a potentially dangerous corner case in the return rules.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |INVALID


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2011-01-01 23:33:12 PST ---
It is not a dangerous corner case, it is a deliberate design choice. It is meant to facilitate writing generic code so that the same code can be generated for void and non-void return values.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg@gmail.com


--- Comment #3 from nfxjfg@gmail.com 2011-01-02 05:45:57 PST ---
(In reply to comment #2)
> It is not a dangerous corner case, it is a deliberate design choice. It is meant to facilitate writing generic code so that the same code can be generated for void and non-void return values.

Wow, D is really full of idiocy, isn't it?

If you wanted to facilitate that, you'd just allow declaring void variables and all that. I've written generic code where _that_ would have been quite useful. But returning non-void values in void functions? Garbage language feature.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399


Iain Buclaw <ibuclaw@ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw@ubuntu.com


--- Comment #4 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-01-02 06:24:38 PST ---
(In reply to comment #2)
> It is not a dangerous corner case, it is a deliberate design choice. It is meant to facilitate writing generic code so that the same code can be generated for void and non-void return values.

Just for clarification, so you allow this, but the return value is always
ignored? (ie: 0)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P5
             Status|RESOLVED                    |REOPENED
                 CC|                            |andrei@metalanguage.com
         Resolution|INVALID                     |
           Severity|enhancement                 |major


--- Comment #5 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-01-02 06:48:17 PST ---
This is a very problematic misfeature that takes no effort to remove. In particular I confirm it is of no or negative use to generic programming. Walter, please let's remove it in the next release. Thank you.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399



--- Comment #6 from nfxjfg@gmail.com 2011-01-02 07:46:29 PST ---
I think the issue is with allowing stuff like this:

Result wrapCall(alias call, Result, Args...)(Args args) {
   return call(args);
}

And then making it work even if the result of the call is void:

wrapCall(&something, void, int, int)(1, 2);

That requires that you can return a void value. Returning a void normally wouldn't make sense, but as you can see it simplifies generic programming.

Somehow it made sense in Walter's head to allow returning _anything_ from a void function. (It would make sense if void would work like Scala's Unit, but void doesn't.)

Walter, please explain.

By the way if D were really orthogonal and would follow any logic, you wouldn't have any problem with this code:

Result wrapCall(alias call, Result, Args...)(Args args) {
   try {
      return call(args);
   } catch {
      writefln("call failed!");
      return Result.init;
   }
}

This works, except when Result is void. Then you have to use static if, duplicate the core code around the actual call if that is more complicated than in the given example, and so on. (I had this in real world code.)

Sure makes a lot of sense.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399



--- Comment #7 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-01-02 09:05:29 PST ---
(In reply to comment #6)
> I think the issue is with allowing stuff like this:
> 
> Result wrapCall(alias call, Result, Args...)(Args args) {
>    return call(args);
> }
> 
> And then making it work even if the result of the call is void:
> 
> wrapCall(&something, void, int, int)(1, 2);
> 
> That requires that you can return a void value. Returning a void normally wouldn't make sense, but as you can see it simplifies generic programming.

Yes, that's a classic in C++ too. My assessment refers not to forwarding across void function, but to void functions returning non-void expressions.

To clarify: forwarding from one void function to another void function is useful. Having a void function return a non-void value should be removed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com


--- Comment #8 from yebblies <yebblies@gmail.com> 2011-06-29 18:58:29 EST ---
I'll copy what I said in issue 3746:

Without this feature, what should happen with lazy void?

void lazyFunc(lazy void a) { a; }

void main()
{
   int i;
   lazyFunc(i++);
}

lazyFunc(i++) is currently re-written to something like
lazyFunc({return i++;})
This of course is broken if returning a non-void from a void function is
disallowed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5399


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #9 from Don <clugdbug@yahoo.com.au> 2011-06-29 02:29:05 PDT ---
(In reply to comment #8)
> I'll copy what I said in issue 3746:
> 
> Without this feature, what should happen with lazy void?
> 
> void lazyFunc(lazy void a) { a; }
> 
> void main()
> {
>    int i;
>    lazyFunc(i++);
> }
> 
> lazyFunc(i++) is currently re-written to something like
> lazyFunc({return i++;})
> This of course is broken if returning a non-void from a void function is
> disallowed.

What's wrong with
lazyFunc( { i++; return;})
?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2