Bug 11615 – Syntax to indicate auto return type is const

Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-27T01:28:42Z
Last change time
2020-03-21T03:56:34Z
Assigned to
No Owner
Creator
Joseph Rushton Wakeling

Comments

Comment #0 by joseph.wakeling — 2013-11-27T01:28:42Z
This enhancement request is a result of the following discussion on D.learn: http://forum.dlang.org/thread/[email protected] Consider a struct or class which contains a method that allows one to access internal data, e.g.: struct Foo { int[] _arr = [0, 1, 2, 3, 4]; auto arr() @property { return _arr; } } The problem with this is that what is returned by the .arr property allows write access to the internal array _arr. This can be solved by making the return type declaration more explicit: const(int)[] arr() @property { return _arr; } However, it would be nice to be able to indicate and obtain such non-writeable return types with the use of auto. The syntax const(auto) seems nice here. The fundamental goal would be simply: "You can't write to this data via this handle." The aim is to simplify generic programming situations by allowing the programmer to specifically protect against any accidental opening up of private data to external write access. Note that applying the regular const keyword won't work here, because this implies a const _method_ (which is a stricter requirement than just a const return type).
Comment #1 by andrej.mitrovich — 2014-04-19T13:35:16Z
Hmm.. interesting problem. Kenji what are your thoughts?
Comment #2 by b2.temp — 2020-02-20T10:13:40Z
I think this ER is nowadays invalid. You can use the `const` STC alone, which is like the `const auto` you wanted. struct Foo { int[] _arr = [0, 1, 2, 3, 4]; const arr() @property { return _arr; } pragma(msg, typeof(arr())); // const(int[]) }