Difference between revisions of "Talk:DIP32"
(Created page with "== About $identifier == Why not require normal declaration syntax, when a variable should be declared in unpacking? Suggested in DIP: <source lang="D"> switch (tup) { ca...") |
Bearophile (talk | contribs) (Added two comments) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 37: | Line 37: | ||
{x, int y} = tup; | {x, int y} = tup; | ||
</source> | </source> | ||
+ | |||
+ | == Variable declaration in case statements == | ||
+ | |||
+ | Care must be taken when variable declaration is used in combination with fall through / '''goto case''' in case statements: | ||
+ | |||
+ | <source lang="D"> | ||
+ | switch (tup) { | ||
+ | case {1, 2}: | ||
+ | case {$, 2}: | ||
+ | goto case; | ||
+ | case {1, x}: | ||
+ | // what is x if tup == {1, 2}? | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==Tuple Slicing?== | ||
+ | |||
+ | If you want you can also add a slicing syntax for tuples: | ||
+ | |||
+ | <source lang="D">auto t1 = {10, 20.5, "foo"}; | ||
+ | auto t2 = t1[1 .. $];</source> | ||
+ | |||
+ | ==Return ref value in tuple?== | ||
+ | This is currently not possible with Phobos tuples: | ||
+ | |||
+ | <source lang="D">{ref int, int} foo(ref int x) { | ||
+ | return tuple(x, 5); | ||
+ | } | ||
+ | void main() {}</source> | ||
+ | |||
+ | ==Built-in tuples should avoid Phobos Tuple const problems== | ||
+ | |||
+ | Like this one: | ||
+ | |||
+ | <source lang="D">void main() { | ||
+ | import std.typecons: Tuple; | ||
+ | Tuple!int t1; | ||
+ | alias T = const Tuple!int; | ||
+ | T t2; | ||
+ | t1 = t2; // OK | ||
+ | t1 = T(); // Error | ||
+ | }</source> |
Latest revision as of 17:43, 8 January 2015
Contents
About $identifier
Why not require normal declaration syntax, when a variable should be declared in unpacking?
Suggested in DIP:
switch (tup) {
case {1, 2}:
case {$, 2}:
case {1, x}:
default:
}
// -----------------------------
int x = 1;
if (auto {$x, y} = coord) { ... }
My suggestion:
switch (tup) {
case {1, 2}:
case {$, 2}:
case {1, auto x}:
default:
}
// -----------------------------
int x = 1;
if ({x, auto y} = coord) { ... }
This would be more consistent with how if usually works (expression syntax => use existing variable, declaration syntax => declare variable initialized with the value of the comparison), doesn't require this special syntax which is used nowhere else in the language, and makes it obvious that a declaration takes place.
It also allows to use unpacking everywhere:
int x;
{x, int y} = tup;
Variable declaration in case statements
Care must be taken when variable declaration is used in combination with fall through / goto case in case statements:
switch (tup) {
case {1, 2}:
case {$, 2}:
goto case;
case {1, x}:
// what is x if tup == {1, 2}?
}
Tuple Slicing?
If you want you can also add a slicing syntax for tuples:
auto t1 = {10, 20.5, "foo"};
auto t2 = t1[1 .. $];
Return ref value in tuple?
This is currently not possible with Phobos tuples:
{ref int, int} foo(ref int x) {
return tuple(x, 5);
}
void main() {}
Built-in tuples should avoid Phobos Tuple const problems
Like this one:
void main() {
import std.typecons: Tuple;
Tuple!int t1;
alias T = const Tuple!int;
T t2;
t1 = t2; // OK
t1 = T(); // Error
}