Bug 22509 – Document implicit conversion to immutable for pure factory functions

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P3
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-11-13T08:44:54Z
Last change time
2023-03-05T15:45:25Z
Keywords
spec
Assigned to
No Owner
Creator
thomas.bockman

Comments

Comment #0 by thomas.bockman — 2021-11-13T08:44:54Z
`immutable immutA = a.ptr;` should be a compile-time error, just like `immutable immutB = b.ptr;` is: ////////////////////////////////////////////////////////// module app; struct A(P) { P ptr() inout @safe { return null; } } struct B { int* ptr() inout @safe { return null; } } void main() @safe { immutable A!(int*) a; immutable immutA = a.ptr; // Accepts invalid! immutable B b; immutable immutB = b.ptr; // Error: cannot implicitly convert expression `b.ptr()` of type `int*` to `immutable(int*)` } //////////////////////////////////////////////////////////
Comment #1 by thomas.bockman — 2021-11-13T09:43:10Z
Apparently, this is a deliberate feature not yet documented in the spec. Forum discussion: https://forum.dlang.org/post/[email protected]
Comment #2 by timon.gehr — 2021-11-13T10:02:41Z
Changed component to spec.
Comment #3 by nick — 2023-03-05T15:44:26Z
> immutable immutA = a.ptr; // Accepts invalid! That is valid. A.ptr is a template so it gets attributes inferred (see below). > immutable immutB = b.ptr; // Error: cannot implicitly convert expression `b.ptr()` of type `int*` to `immutable(int*)` This error goes away if you add `pure` to B.ptr. > this is a deliberate feature not yet documented in the spec It is documented: dlang.org/spec/function.html#pure-factory-functions If you have any ideas on how the docs can be improved please let us know.
Comment #4 by nick — 2023-03-05T15:45:25Z
> A.ptr is a template I meant A.ptr is inside the A template.