Bug 16745 – Add template helper for creating static arrays with the size inferred

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-11-23T21:56:08Z
Last change time
2018-06-30T19:18:17Z
Assigned to
No Owner
Creator
Jonathan M Davis

Comments

Comment #0 by issues.dlang — 2016-11-23T21:56:08Z
There is currently no way in the language or in Phobos to create a static array where the size is inferred. If you do something like int[5] sa = [1, 2, 3, 4, 5]; you have to explicitly give the length, and if the number of arguments ever changes, you need to explicitly change the length. There are advantages to that, but it can also be annoying to have to type the length rather than have it be inferred. And auto won't do it, because it would infer the type to be a dynamic array rather than a static one. As I understand it, at one point there was a PR for dmd which would have added a syntax such as int[$] = [1, 2, 3, 4, 5]; to implement this functionality, but it was rejected. However, it should be simple enough to create a template helper which does the same job and add it to Phobos.
Comment #1 by john.loughran.colvin — 2016-12-08T01:38:44Z
Comment #2 by github-bugzilla — 2018-06-30T19:18:16Z
Commit pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/e191a7d6a48757c9696004fce90c30d738adb901 Fixes Issue 16745 - Add template helper for creating static arrays with the size inferred