Bug 22768 – lazy does not cache its results

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-02-13T12:30:27Z
Last change time
2022-02-13T13:12:36Z
Assigned to
No Owner
Creator
Temtaime

Comments

Comment #0 by temtaime — 2022-02-13T12:30:27Z
import std; void foo(lazy string s) { s.writeln; s.writeln; } void main() { foo(() { 1.writeln; return `abc`; }()); } This simple program outputs: 1 abc 1 abc It should output instead: 1 abc abc
Comment #1 by stanislav.blinov — 2022-02-13T13:12:36Z
The spec does not mandate such behavior: https://dlang.org/spec/function.html#lazy-params The compiler *could* cache results if resulting delegate was strongly pure (https://dlang.org/spec/function.html#pure-functions #17), but even that is left up to the implementation, and `lazy` fails to infer attributes anyway.