Proposal: Object/?? Destruction
Q. Schroll
qs.il.paperinik at gmail.com
Sat Oct 14 22:20:46 UTC 2017
I've thought about tuples and stuff for a while. For tuples, I'll
use [brackets]. Reasons follow.
Homogeneous tuples are repetitions of some single type. We have
them today in form of static arrays. We could allow
"inhomogeneous arrays" and call them tuples. T[n] is then an
alias for [T, T, .., T] with n repititions. In place of a type
[T, S] means Tuple!(T, S) and in place of an Object [t, s] means
tuple(t, s). Note that D's grammar allows disambiguate types and
objects by syntax.
A tuple implicitly converts to some other, if the pointwise types
do. Bracket literals constitute a separate type that exists in
the compiler only. We have that already to make
int[2] a = [ 1, 2 ];
not allocate on the heap, but
int[] a = [ 1, 2 ];
does. So at first, [ 1, 2.0 ] is of type [int, double]. If you
assign it to a double[2], because int -> double, the conversion
is no problem. The thing that changes, is when you ask for
typeof([ 1, 2.0 ]) directly. Of course,
auto tup = [ 1, 2.0 ];
will homogenize the tuple to double[] similar to how it does
today. Declaration-decomposition can be done as
auto [a, b] = f(x);
(non-exlusive) or
[auto a, auto b] = f(x);
The first one is shorter, the latter one let's you do
[int a, auto b] = f(x);
So auto [x1, .. xn] is just a shorthand for [auto x1, .. auto xn].
Assignment-decomposition is the same with the types/auto missing.
Swap can be done with
[a, b] = [b, a];
From the type system, if a tuple literal has only lvalues inside,
it is an lvalue, too.
Note that there must be some way to handle side effects
correctly. The problem is already known from normal assignment.
1-tuples are in a natural way included. int[1] is today different
from int. When we have first-class tuples in D, we should not
distinguish static arrays from homogeneous tuples.
Therefore, and because of brackets, you can distinguish f(1, 2)
from f([1, 2]). I find the syntax (1,) for 1-tuples weird.
Parenthesis are used only for operator precedence and function
calls. They should not be used for tuples -- the 1-tuple case and
f(1, 2) vs f((1, 2)) prove that. Parenthesis are a tool of
syntax, not semantics. You can never omit brackets in D.
Maybe you can use some of that as input for your DIP.
More information about the Digitalmars-d
mailing list