digitalmars.D.learn - Array of Algebraic argument syntax
- Kasra Sadeghi (14/14) Sep 22 2020 Hi everyone!
- =?UTF-8?Q?Ali_=c3=87ehreli?= (15/23) Sep 22 2020 That should be 'alias' instead of 'class':
- Kasra Sadeghi (2/8) Sep 22 2020 Thanks! Wish there was a less redundant syntax for the arrays.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (15/26) Sep 22 2020 Do you really need to write literal Value arrays? If not, you would=20
Hi everyone!
What's the syntax for passing an array of Algebraics?
definition:
  class None {}
  class Value = Algebraic!(int, double, string, None);
  void printValue(Value[] values) {
    foreach(value; values) {
      value.writeln;
    }
  }
usage attempts:
  printValue([4.5]);
  printValue(Value[4.5]);
  printValue(Value[](4.5));
 Sep 22 2020
On 9/22/20 2:30 PM, Kasra Sadeghi wrote:
 Hi everyone!
=20
 What's the syntax for passing an array of Algebraics?
=20
 definition:
=20
  =C2=A0class None {}
  =C2=A0class Value =3D Algebraic!(int, double, string, None);
That should be 'alias' instead of 'class':
import std.variant;
import std.stdio;
class None {}
alias Value =3D Algebraic!(int, double, string, None);
void printValue(Value[] values) {
   foreach(value; values) {
     value.writeln;
   }
}
void main() {
   printValue([Value(4.5), Value("hello"), Value(42)]);
}
Ali
 Sep 22 2020
On Tuesday, 22 September 2020 at 21:36:48 UTC, Ali Çehreli wrote:
 ...
 alias Value = Algebraic!(int, double, string, None);
 ...
 void main() {
   printValue([Value(4.5), Value("hello"), Value(42)]);
 }
Thanks! Wish there was a less redundant syntax for the arrays.
 Sep 22 2020
On 9/22/20 2:53 PM, Kasra Sadeghi wrote:On Tuesday, 22 September 2020 at 21:36:48 UTC, Ali =C3=87ehreli wrote:Do you really need to write literal Value arrays? If not, you would=20 build a Value[] at runtime without seeing the syntax above. Still, here is a function template that provides better syntax: Value[] valueArray(Args...)(Args args) { Value[] result; foreach (arg; args) { result ~=3D Value(arg); } return result; } void main() { printValue(valueArray(4.5, "hello", 42)); } Ali... alias Value =3D Algebraic!(int, double, string, None); ... void main() { =C2=A0 printValue([Value(4.5), Value("hello"), Value(42)]); }=20 Thanks! Wish there was a less redundant syntax for the arrays. =20 =20
 Sep 22 2020








 
  
  
  =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com>
 =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com>