July 23, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #10 from Kenji Hara <k.hara.pg@gmail.com> 2011-07-23 08:28:08 PDT --- (In reply to comment #7) > One simple syntactic matter is that we can't extend the existing syntax to work with specified types. That means the user must use "auto" but cannot specify the types of the variables defined. > > To allow that in the future, we need to move the paren before the auto: > > (auto i, j) = tuple(1.2, "a"); > > Then the syntax can be later extended to: > > (double i, string j) = tuple(1.2, "a"); > > Anyway, we should wait for Walter to weigh in. Thanks Kenji for this work. How about this syntax? auto (i, j) = tuple(1.2, "a"); (double, string) (i, j) = tuple(1.2, "a"); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 23, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #11 from Kenji Hara <k.hara.pg@gmail.com> 2011-07-23 08:33:10 PDT --- Oops, I forgot to paste experimental patch link. https://github.com/9rnsr/dmd/compare/expandTuples...declarationTuple -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 23, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #12 from bearophile_hugs@eml.cc 2011-07-23 09:03:24 PDT --- (In reply to comment #10) > (In reply to comment #7) > > (auto i, j) = tuple(1.2, "a"); > > > > Then the syntax can be later extended to: > > > > (double i, string j) = tuple(1.2, "a"); > > > > Anyway, we should wait for Walter to weigh in. Thanks Kenji for this work. > > How about this syntax? > auto (i, j) = tuple(1.2, "a"); > (double, string) (i, j) = tuple(1.2, "a"); It's not ugly, but I think this looks a bit better: (double i, string j) = tuple(1.2, "a"); Also because it's closer to (more consistent with) the currently used definition syntax: alias Tuple!(double,"i", string,"j") T1; auto t1 = T1(1.2, "a"); If eventually the definition syntax too will become built-in, then you will have something like: alias (double i, string j) T2; auto t2 = T2(1.2, "a"); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 23, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #13 from bearophile_hugs@eml.cc 2011-07-23 12:04:19 PDT --- Tuple slicing and other situations create tuples with 1 items or 0 items: auto t = tuple(1.5, "foo"); auto t1 = t[0 .. 1]; auto t2 = t[0 .. 0]; In python they are written: (x,) or x, () For the 1-tuple D may do use the same syntax (but here parentheses are requires, while in Python they are optional): auto (x,) = [10]; auto (y,) = t1; What about 0-length tuples? int[0] a; () = a; () = t2; -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 23, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #14 from bearophile_hugs@eml.cc 2011-07-23 12:14:23 PDT --- See also the idea of switching on tuples, in issue 596 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 24, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #15 from Kenji Hara <k.hara.pg@gmail.com> 2011-07-24 06:41:36 PDT --- I found a conflict like follows: (double, string) (i, j) = tuple(1.2, "a"); (var, func)(var, var) = tuple(1.2, "a"); So it is bad. I clean up syntax, and update patch. https://github.com/9rnsr/dmd/compare/expandTuples...declarationTuple ---- TupleDeclaration: StorageClasses ( IdentifierList ) = Initializer ; // 1 ( ParameterList ) = Initializer ; // 2-1 ( StorageClass TupleTypeList ) = Initializer ; // 2-2 TupleTypeList: TupleType TupleType , TupleTypeList TupleType: StorageClasses BasicType Declarator BasicType Declarator StorageClasses Identifier Identifier ---- // Example of 1 auto (i, j) = tuple(10, "a"); static assert(is(typeof(i) == int)); static assert(is(typeof(j) == string)); const (x, y) = TypeTuple!(1, 2); static assert(is(typeof(x) == const(int))); static assert(is(typeof(y) == const(int))); // Example of 2-1 (int i, string j) = tuple(10, "a"); static assert(is(typeof(i) == int)); static assert(is(typeof(j) == string)); // Example of 2-2 (auto c, r) = TypeTuple!('c', "har"); static assert(is(typeof(c) == char)); static assert(is(typeof(r) == string)); (const x, auto y) = TypeTuple!(1, 2); static assert(is(typeof(x) == const(int))); static assert(is(typeof(y) == int)); (auto a1, const int[] a2) = TypeTuple!([1], [2,3]); static assert(is(typeof(a1) == int[])); static assert(is(typeof(a2) == const(int[]))); ---- TupleTypeList is similar to ForeachTypeList. http://d-programming-language.org/statement.html#ForeachStatement -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 24, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #16 from bearophile_hugs@eml.cc 2011-07-24 07:24:53 PDT --- (In reply to comment #15) > I clean up syntax, and update patch. https://github.com/9rnsr/dmd/compare/expandTuples...declarationTuple Thank you. How does it behave for: a[1] = [10]; auto (x, y) = a; How does it behave for 0-tuples and 1-tuples? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 24, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #17 from Kenji Hara <k.hara.pg@gmail.com> 2011-07-24 07:38:49 PDT --- (In reply to comment #16) > How does it behave for 0-tuples and 1-tuples? I think 1-element tuple expansion should be allowed. Now my patch's this support is incomplete. I'll improve it. // 1-tuple expansion auto (x1) = [10]; (auto x2) = tuple(10); (auto x3) = tuple(10)[0..1]; (int x4) = TypeTuple!(10); assert(x1 == 10); assert(x2 == 10); assert(x3 == 10); assert(x4 == 10); The 0-element tuple with TupleDeclaration should be error. Because they has no elements, so expansion cannot get value. //auto () = []; //(auto ) = tuple(); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 24, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #18 from bearophile_hugs@eml.cc 2011-07-24 11:14:57 PDT --- (In reply to comment #17) > > // 1-tuple expansion > auto (x1) = [10]; Are you sure you want to use that syntax? A syntax more similar to the Python one is: auto (x1,) = [10]; -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
July 24, 2011 [Issue 6365] AutoTupleDeclaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=6365 --- Comment #19 from Kenji Hara <k.hara.pg@gmail.com> 2011-07-24 11:43:28 PDT --- (In reply to comment #18) > Are you sure you want to use that syntax? Yes. The syntax is usuful for tuple expansion and non-auto binding: const (x, y) = tuple(10, 20); // x and y are now const It is more usuful for many element tuple: (const x, const y, const z, const v, const w, const m, const n) = ... ; // same as: // const (x, y, z, v, w, m, n) = ... ; > A syntax more similar to the Python one is: > > auto (x1,) = [10]; In D, Isolated comma is almost invalid except enum declaration, I think. D is not Python. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation