Bug 5753 – Disallow map() of void function

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-03-19T07:11:03Z
Last change time
2017-08-24T08:12:28Z
Keywords
accepts-invalid, bootcamp
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-03-19T07:11:03Z
This code compiles with no errors (DMD 2.052), but in my opinion it has to raise a compile-time error, because map() must statically refuse functions that return void: import std.algorithm; void foo() {} void main() { int[] data = [1, 2, 3]; map!((int i){ return foo(); })(data); }
Comment #1 by bearophile_hugs — 2011-03-19T07:12:24Z
A simpler example: import std.algorithm; void foo(int x) {} void main() { int[] data = [1, 2, 3]; map!foo(data); }
Comment #2 by pedro — 2011-04-24T12:17:15Z
Comment #3 by andrej.mitrovich — 2011-04-24T12:31:29Z
*** Issue 5804 has been marked as a duplicate of this issue. ***
Comment #4 by bearophile_hugs — 2011-04-24T12:43:54Z
For Phobos devs: see also the comment about mixing up lazy evaluation with side effects: http://d.puremagic.com/issues/show_bug.cgi?id=5804#c2
Comment #5 by peter.alexander.au — 2014-02-09T07:40:09Z
Comment #6 by github-bugzilla — 2014-02-13T02:39:32Z
Comment #7 by r.97all — 2015-12-21T09:02:34Z
It seems that void lambda is not disallowed. DMD2.069 compiles the code below, which I think must not: void f(T)(T x){} unittest { import std.algorithm : map; static assert (!__traits(compiles, [1].map!f)); static assert ( __traits(compiles, [1].map!(e => f(e)))); }
Comment #8 by razvan.nitu1305 — 2017-08-24T08:12:28Z
(In reply to Ryuichi OHORI from comment #7) > It seems that void lambda is not disallowed. DMD2.069 compiles the code > below, which I think must not: > > void f(T)(T x){} > > unittest > { > import std.algorithm : map; > static assert (!__traits(compiles, [1].map!f)); > static assert ( __traits(compiles, [1].map!(e => f(e)))); > } I cannot reproduce this on git HEAD (ubuntu 16.04 64-bit). The second static assert fails with the message "Error: static assert "Mapping function(s) must not return void: tuple(__lambda1)"". Closing as WORKSFORME