Thread overview
[Issue 3572] New: declaring pure function with void return type should be compile time error
Dec 04, 2009
Michal Minich
Dec 04, 2009
Don
Dec 04, 2009
David Simcha
Dec 05, 2009
Don
Feb 18, 2010
David Simcha
Jan 05, 2012
Trass3r
Feb 03, 2013
Andrej Mitrovic
Feb 04, 2013
Kenji Hara
Mar 06, 2013
yebblies
December 04, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3572

           Summary: declaring pure function with void return type should
                    be compile time error
           Product: D
           Version: 2.036
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: michal.minich@gmail.com


--- Comment #0 from Michal Minich <michal.minich@gmail.com> 2009-12-04 02:37:33 PST ---
In D specification is written: "Pure functions are functions that produce the same result for the same arguments. To that end, a pure function has parameters that are all immutable or are implicitly convertible to immutable

void function cannot produce any result, thus they are meaningless as pure functions. Also when all parameters are implicitly immutable, there is no possibility to modify "out" parameters. So the only way for function to produce result, is by returning it (and void has no value).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 04, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3572


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

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


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2009-12-04 12:01:56 PST ---
(In reply to comment #0)
> In D specification is written: "Pure functions are functions that produce the same result for the same arguments. To that end, a pure function has parameters that are all immutable or are implicitly convertible to immutable
> 
> void function cannot produce any result, thus they are meaningless as pure functions. Also when all parameters are implicitly immutable, there is no possibility to modify "out" parameters. So the only way for function to produce result, is by returning it (and void has no value).

I think the restriction on 'out' parameters will be removed. I don't think
there's any reason for it.
A void pure function that doesn't have any 'out' parameters does seem to be
pretty useless (you can use it as a metaprogramming test that anything inside
it is pure, but that's about all I can think of).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 04, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3572


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha@yahoo.com


--- Comment #2 from David Simcha <dsimcha@yahoo.com> 2009-12-04 15:52:01 PST ---
?????  Why would you allow out parameters in a pure function?  This seems reasonable for simple value types (ints, floats, etc.), but when you start passing objects in, you start allowing the modification of whole object subgraphs from pure functions.  This makes no sense.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 05, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3572



--- Comment #3 from Don <clugdbug@yahoo.com.au> 2009-12-04 18:28:11 PST ---
(In reply to comment #2)
> ?????  Why would you allow out parameters in a pure function?  This seems reasonable for simple value types (ints, floats, etc.), but when you start passing objects in, you start allowing the modification of whole object subgraphs from pure functions.  This makes no sense.

Why doesn't it make sense? Aren't you thinking of inout parameters? A pure
function  void foo(out A a); ought to be exactly the same as A foo(); together
with an assignment. I'm not seeing anything impure in that.
BTW, it works fine in CTFE. (CTFE isn't quite the same concept as pure, but
it's close).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3572



--- Comment #4 from David Simcha <dsimcha@yahoo.com> 2010-02-17 18:38:36 PST ---
(In reply to comment #3)
> Why doesn't it make sense? Aren't you thinking of inout parameters? A pure
> function  void foo(out A a); ought to be exactly the same as A foo(); together
> with an assignment. I'm not seeing anything impure in that.
> BTW, it works fine in CTFE. (CTFE isn't quite the same concept as pure, but
> it's close).

You're right, I did get confused between out and ref.  Allowing out parameters in pure functions makes sense.  I tend to forget how out parameters work because I almost never use them.  I almost always just return a tuple or a struct.

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


Trass3r <mrmocool@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid, diagnostic
                 CC|                            |mrmocool@gmx.de
           Platform|x86                         |All
         OS/Version|Windows                     |All
           Severity|enhancement                 |normal


--- Comment #5 from Trass3r <mrmocool@gmx.de> 2012-01-05 14:19:40 PST ---
Also you shouldn't be able to throw away the result of a pure function. http://d.puremagic.com/issues/show_bug.cgi?id=7235

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 03, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3572


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-03 15:53:14 PST ---
(In reply to comment #3)
> (In reply to comment #2)
> > ?????  Why would you allow out parameters in a pure function?  This seems reasonable for simple value types (ints, floats, etc.), but when you start passing objects in, you start allowing the modification of whole object subgraphs from pure functions.  This makes no sense.
> 
> Why doesn't it make sense? Aren't you thinking of inout parameters? A pure
> function  void foo(out A a); ought to be exactly the same as A foo(); together
> with an assignment. I'm not seeing anything impure in that.
> BTW, it works fine in CTFE. (CTFE isn't quite the same concept as pure, but
> it's close).

Should we close this report then?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 04, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3572


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #7 from Kenji Hara <k.hara.pg@gmail.com> 2013-02-03 18:50:45 PST ---
In current, we can declare a pure function which has "weak" purity.

pure void foo(int* p, out string s, ref int[] arr);

All results are returned through its parameters.
So, returning void itself has no problem in pure functions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3572


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |yebblies@gmail.com
            Version|2.036                       |D2
         Resolution|INVALID                     |
           Severity|normal                      |enhancement


--- Comment #8 from yebblies <yebblies@gmail.com> 2013-03-06 21:46:46 EST ---
(In reply to comment #7)
> In current, we can declare a pure function which has "weak" purity.
> 
> pure void foo(int* p, out string s, ref int[] arr);
> 
> All results are returned through its parameters.
> So, returning void itself has no problem in pure functions.

This would still be meaningful for strongly-pure or const-pure functions.

eg.
pure void foo(string x)
By definition calling foo does no observable work.

While this might be WONTFIX or LATER like issue 3882, it is not invalid.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------