Difference between revisions of "DIP34"
Timotheecour (talk | contribs) |
Timotheecour (talk | contribs) (→DIP34: Static array literals (STILL EDITING -- don't read yet)) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 24: | Line 24: | ||
|- | |- | ||
|Links: | |Links: | ||
+ | | [http://forum.dlang.org/post/ydyqguthbfxlnuxtqklx@forum.dlang.org] [https://github.com/Dgame/dmd/tree/static_array_literals implementation in progress] | ||
| | | | ||
|} | |} | ||
Line 37: | Line 38: | ||
== Description == | == Description == | ||
Currently, array literals such as | Currently, array literals such as | ||
+ | <syntaxhighlight lang="d"> | ||
auto x=[1,2,3]; | auto x=[1,2,3]; | ||
+ | </syntaxhighlight> | ||
make x dynamic. To get a static array one needs to write: | make x dynamic. To get a static array one needs to write: | ||
+ | <syntaxhighlight lang="d"> | ||
int[3] x=[1,2,3]; | int[3] x=[1,2,3]; | ||
+ | </syntaxhighlight> | ||
which is inconvenient for many reasons: | which is inconvenient for many reasons: | ||
− | * DRY | + | * it's not DRY (need to explicitly write 3 as the length and specify the type int) |
− | int) | + | * there's no easy way to pass a static array literal to a function accepting a static array; for example it requires: |
− | * no easy way to pass a static array | + | <syntaxhighlight lang="d"> |
− | array; for example it requires: | ||
int[3] x=[1,2,3]; | int[3] x=[1,2,3]; | ||
fun(x); | fun(x); | ||
+ | </syntaxhighlight> | ||
− | + | Instead we propose the syntax: | |
− | + | <syntaxhighlight lang="d"> | |
auto x=[1,2,3]S; | auto x=[1,2,3]S; | ||
− | where S stands for static | + | </syntaxhighlight> |
− | More generally the compiler should translate [x1,...,xn]S to: typeof(x1)[n] | + | where S stands for static. More generally the compiler should translate |
+ | <syntaxhighlight lang="d"> | ||
+ | [x1,...,xn]S to: typeof(x1)[n] | ||
+ | </syntaxhighlight> | ||
Advantages: | Advantages: | ||
− | * static array | + | * static array literals becomes as convenient as dynamic ones |
− | * no confusion possible for the compiler; I believe this syntax doesn't clash | + | * no confusion possible for the compiler; I believe this syntax doesn't clash with existing syntax. |
− | with existing syntax. | + | * In our previous example, no need to write an intermediate x: we can just write |
− | * In our previous example, no need to write an intermediate x: we can just | + | <syntaxhighlight lang="d"> |
− | |||
fun([1,2,3]S); | fun([1,2,3]S); | ||
− | |||
fun([1.0,2,3]S); //for example, if static array of doubles requested | fun([1.0,2,3]S); //for example, if static array of doubles requested | ||
+ | </syntaxhighlight> | ||
* this would also prevent the common workaround hacks of the form: | * this would also prevent the common workaround hacks of the form: | ||
+ | <syntaxhighlight lang="d"> | ||
void fun(T...)(T x){} which accept fun(1,2,3): one could just write: | void fun(T...)(T x){} which accept fun(1,2,3): one could just write: | ||
void fun(T,uint N)(in T[N]x){} or void fun(T,uint N)(T[N]x){} | void fun(T,uint N)(in T[N]x){} or void fun(T,uint N)(T[N]x){} | ||
+ | </syntaxhighlight> | ||
− | * this could prevent inefficient intermediate code as reported in Issue 2356 | + | * this could prevent inefficient intermediate code as reported in Issue 2356 and related, as it would be clear from "S" that a static is requested. |
− | and related, as it would be clear from "S" that a static is requested. | ||
* this could be used in expressions as well: auto x=[1,2,3]S+[4,5,6]S; | * this could be used in expressions as well: auto x=[1,2,3]S+[4,5,6]S; | ||
− | This should be simpler than a previous request I've seen for int[$]x=[1,2,3]; | + | This should be simpler than a previous request I've seen for int[$]x=[1,2,3]; which still requires one to write the type explicitly. |
− | which still requires one to write the type explicitly. | ||
== Copyright == | == Copyright == |
Latest revision as of 18:28, 11 December 2013
Contents
DIP34: Static array literals (STILL EDITING -- don't read yet)
Title: | Static array literals | |
---|---|---|
DIP: | 34 | |
Version: | 1 | |
Status: | Draft | |
Created: | 2013-04-06 | |
Last Modified: | 2013-04-06 | |
Author: | Timothee Cour | |
Links: | [1] implementation in progress |
Abstract
This is a proposal for introducing static array literals, as follows:
auto x=[1,2,3]S;
static assert(is(typeof(x)==int[3]));
The particular choice of 'S' can be discussed.
Description
Currently, array literals such as
auto x=[1,2,3];
make x dynamic. To get a static array one needs to write:
int[3] x=[1,2,3];
which is inconvenient for many reasons:
* it's not DRY (need to explicitly write 3 as the length and specify the type int) * there's no easy way to pass a static array literal to a function accepting a static array; for example it requires:
int[3] x=[1,2,3];
fun(x);
Instead we propose the syntax:
auto x=[1,2,3]S;
where S stands for static. More generally the compiler should translate
[x1,...,xn]S to: typeof(x1)[n]
Advantages:
* static array literals becomes as convenient as dynamic ones * no confusion possible for the compiler; I believe this syntax doesn't clash with existing syntax. * In our previous example, no need to write an intermediate x: we can just write
fun([1,2,3]S);
fun([1.0,2,3]S); //for example, if static array of doubles requested
- this would also prevent the common workaround hacks of the form:
void fun(T...)(T x){} which accept fun(1,2,3): one could just write:
void fun(T,uint N)(in T[N]x){} or void fun(T,uint N)(T[N]x){}
- this could prevent inefficient intermediate code as reported in Issue 2356 and related, as it would be clear from "S" that a static is requested.
- this could be used in expressions as well: auto x=[1,2,3]S+[4,5,6]S;
This should be simpler than a previous request I've seen for int[$]x=[1,2,3]; which still requires one to write the type explicitly.
Copyright
This document has been placed in the Public Domain.