www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - uniform initialization in D (as in C++11): i{...}

reply Timothee Cour via Digitalmars-d <digitalmars-d puremagic.com> writes:
what's D's answer for C++11's uniform initialization [1] which allows DRY code?

Could we have this:
----
struct A{
  int a;
  int b;
}

A fun(A a, int b) {
  if(b==1) return i{0,1};
  else if(b==2) return i{2,3};
  else return fun(i{3,4}, 1);
}
----

As for which syntax to use, that's an orthogonal question, but here I
used i{} since {} (from C++11) is already used by delegates (with
tuples also being discussed at some point, which didn't pan out bc
someone mentioned it was ambiguous in some case; see my next email
proposal below though [2])

----
{} // delegate (existing syntax)
q{...} // comment (existing syntax)
i{...} // uniform intialization (proposed syntax)
t{...} // tuple(a,b) (proposed syntax)
T{...} // TypeTuple!(a,b) (proposed syntax)
----

[1] http://programmers.stackexchange.com/questions/133688/is-c11-uniform-initialization-a-replacement-for-the-old-style-syntax
[2] EMAIL:proposed syntax for tuple: t{} and TypeTuple: T{}
Apr 04 2016
next sibling parent reply Jin <nin-jin ya.ru> writes:
On Tuesday, 5 April 2016 at 05:39:25 UTC, Timothee Cour wrote:
 what's D's answer for C++11's uniform initialization [1] which 
 allows DRY code?

 Could we have this:
 ----
 struct A{
   int a;
   int b;
 }

 A fun(A a, int b) {
   if(b==1) return i{0,1};
   else if(b==2) return i{2,3};
   else return fun(i{3,4}, 1);
 }
 ----
A fun(A a, int b) { if(b==1) return [0,1]; else if(b==2) return [a:2,b:3]; else return fun([3,4], 1); }
Apr 05 2016
parent ixid <adamsibson hotmail.com> writes:
On Tuesday, 5 April 2016 at 09:22:12 UTC, Jin wrote:
 On Tuesday, 5 April 2016 at 05:39:25 UTC, Timothee Cour wrote:
 what's D's answer for C++11's uniform initialization [1] which 
 allows DRY code?

 Could we have this:
 ----
 struct A{
   int a;
   int b;
 }

 A fun(A a, int b) {
   if(b==1) return i{0,1};
   else if(b==2) return i{2,3};
   else return fun(i{3,4}, 1);
 }
 ----
A fun(A a, int b) { if(b==1) return [0,1]; else if(b==2) return [a:2,b:3]; else return fun([3,4], 1); }
Square brackets look better than curly brackets which create a lot of visual noise, especially when mixed with other brackets. It also feels neat to treat tuples like an array of mixed types. Can't we be more aggressive about reclaiming the comma operator for tuples?
Apr 05 2016
prev sibling next sibling parent QAston <qaston gmail.com> writes:
On Tuesday, 5 April 2016 at 05:39:25 UTC, Timothee Cour wrote:
 As for which syntax to use, that's an orthogonal question, but 
 here I used i{} since {} (from C++11) is already used by 
 delegates (with tuples also being discussed at some point, 
 which didn't pan out bc someone mentioned it was ambiguous in 
 some case; see my next email proposal below though [2])

 ----
 {} // delegate (existing syntax)
 q{...} // comment (existing syntax)
 i{...} // uniform intialization (proposed syntax)
 t{...} // tuple(a,b) (proposed syntax)
 T{...} // TypeTuple!(a,b) (proposed syntax)
 ----

 [1] 
 http://programmers.stackexchange.com/questions/133688/is-c11-uniform-initialization-a-replacement-for-the-old-style-syntax
 [2] EMAIL:proposed syntax for tuple: t{} and TypeTuple: T{}
D doesn't have initialization syntax problems that C++ has, so it doesn't need "uniformization" (it isn't really an uniformization, which is hillarious) of the syntax.
Apr 05 2016
prev sibling next sibling parent Alex Parrill <initrd.gz gmail.com> writes:
On Tuesday, 5 April 2016 at 05:39:25 UTC, Timothee Cour wrote:
 q{...} // comment (existing syntax)
That is syntax for a string literal, not a comment (though unlike other string literals, the contents must be valid D tokens and editors usually do not highlight them as strings).
Apr 05 2016
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 05.04.2016 07:39, Timothee Cour via Digitalmars-d wrote:
 what's D's answer for C++11's uniform initialization [1] which allows DRY code?
If it's just about DRY, D is in quite good shape. A fun(A a, int b) { alias i=typeof(return); if(b==1) return i(0,1); else if(b==2) return i(2,3); else return fun(i(3,4), 1); }
 Could we have this:
 ----
 struct A{
    int a;
    int b;
 }

 A fun(A a, int b) {
    if(b==1) return i{0,1};
    else if(b==2) return i{2,3};
    else return fun(i{3,4}, 1);
 }
 ----

 As for which syntax to use, that's an orthogonal question, but here I
 used i{} since {} (from C++11) is already used by delegates (with
 tuples also being discussed at some point, which didn't pan out bc
 someone mentioned it was ambiguous in some case; see my next email
 proposal below though [2])

 ----
 {} // delegate (existing syntax)
 q{...} // comment (existing syntax)
 i{...} // uniform intialization (proposed syntax)
 t{...} // tuple(a,b) (proposed syntax)
 T{...} // TypeTuple!(a,b) (proposed syntax)
 ----

 [1] http://programmers.stackexchange.com/questions/133688/is-c11-uniform-initialization-a-replacement-for-the-old-style-syntax
 [2] EMAIL:proposed syntax for tuple: t{} and TypeTuple: T{}
Well, the following is allowed: struct A{ int a; int b; } void main(){ A x={1,2}; A y={b:2,a:1}; assert(x==y); } It would be funny if the syntax for such literals was different in initialization position and in other places. (Even more strange than the fact that now, the expression syntax for initialization differs from the usual expression syntax. {} is not always a delegate literal. E.g. auto x={}; actually fails to compile.)
Apr 05 2016
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 05.04.2016 21:24, Timon Gehr wrote:
 On 05.04.2016 07:39, Timothee Cour via Digitalmars-d wrote:
 what's D's answer for C++11's uniform initialization [1] which allows
 DRY code?
If it's just about DRY...
NVM, I see those points have been brought up already (the thread was split into two).
Apr 05 2016