digitalmars.D.learn - Function accepting variadic arguments
- pineapple (16/16) Jan 20 2016 I'd like to make a constructor which takes a variable list of
- =?UTF-8?Q?Ali_=c3=87ehreli?= (32/48) Jan 20 2016 import std.stdio;
- pineapple (5/8) Jan 20 2016 Thank you!
- Daniel Kozak (15/27) Jan 20 2016 struct S
- Daniel Kozak (3/33) Jan 20 2016 isNumeric!(Args[index] should be:
- Daniel Kozak via Digitalmars-d-learn (18/37) Jan 20 2016 V Wed, 20 Jan 2016 12:38:20 +0000
- Daniel Kozak (2/22) Jan 20 2016 Type should be named Index :)
I'd like to make a constructor which takes a variable list of
arguments, of a short list of possible types, with different
handling depending on the type. I haven't been able to find any
documentation or examples that did quite what I'm trying to. Help?
A fun pseudocode example of what I'm trying to do:
this(...){
foreach(argument){
if(argument is a number){
do stuff with it;
}else if(argument is of type A){
do other stuff;
}else if(argument is of type B){
yet more stuff;
}
}
}
Jan 20 2016
On 01/20/2016 04:38 AM, pineapple wrote:
I'd like to make a constructor which takes a variable list of arguments,
of a short list of possible types, with different handling depending on
the type. I haven't been able to find any documentation or examples that
did quite what I'm trying to. Help?
A fun pseudocode example of what I'm trying to do:
this(...){
foreach(argument){
if(argument is a number){
do stuff with it;
}else if(argument is of type A){
do other stuff;
}else if(argument is of type B){
yet more stuff;
}
}
}
import std.stdio;
struct A {
double d;
}
struct B {
string s;
}
struct C {
this(T...)(T args) {
foreach (arg; args) {
static if (is (typeof(arg) == int)) {
writefln("int: %s", arg);
} else static if (is (typeof(arg) == A) ) {
writefln("A: %s", arg);
} else static if (is (typeof(arg) == B)) {
writefln("B: %s", arg);
}
}
}
}
void main() {
auto c = C(42, A(1.5), B("hello"));
}
Prints:
int: 42
A: A(1.5)
B: B("hello")
And there is another example here:
http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.tuple%20template%20parameter
Ali
Jan 20 2016
On Wednesday, 20 January 2016 at 12:56:51 UTC, Ali Çehreli wrote:And there is another example here: http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.tuple%20template%20parameter AliThank you! What's the purpose of "is" in the type checks? Also, how would I utilize isNumeric from std.traits here, in place of checking only for ints?
Jan 20 2016
On Wednesday, 20 January 2016 at 13:06:14 UTC, pineapple wrote:On Wednesday, 20 January 2016 at 12:56:51 UTC, Ali Çehreli wrote:is is use for compare typesAnd there is another example here: http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.tuple%20template%20parameter AliThank you! What's the purpose of "is" in the type checks?Also, how would I utilize isNumeric from std.traits here, in place of checking only for ints?struct S { this(Args...)(Args args) { foreach(Index, Value;args) { if (isNumeric!(Args[index])) { ... } } } }
Jan 20 2016
On Wednesday, 20 January 2016 at 13:09:13 UTC, Daniel Kozak wrote:On Wednesday, 20 January 2016 at 13:06:14 UTC, pineapple wrote:isNumeric!(Args[index] should be: isNumeric!(Args[Index]On Wednesday, 20 January 2016 at 12:56:51 UTC, Ali Çehreli wrote:is is use for compare typesAnd there is another example here: http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.tuple%20template%20parameter AliThank you! What's the purpose of "is" in the type checks?Also, how would I utilize isNumeric from std.traits here, in place of checking only for ints?struct S { this(Args...)(Args args) { foreach(Index, Value;args) { if (isNumeric!(Args[index])) { ... } } } }
Jan 20 2016
V Wed, 20 Jan 2016 12:38:20 +0000
pineapple via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
napsáno:
I'd like to make a constructor which takes a variable list of
arguments, of a short list of possible types, with different
handling depending on the type. I haven't been able to find any
documentation or examples that did quite what I'm trying to. Help?
A fun pseudocode example of what I'm trying to do:
this(...){
foreach(argument){
if(argument is a number){
do stuff with it;
}else if(argument is of type A){
do other stuff;
}else if(argument is of type B){
yet more stuff;
}
}
}
import std.stdio;
struct S
{
this(Args...)(Args args)
{
foreach(Type, Value;args)
{
writefln("Type: %s value: %s", Args[Type].stringof, Value);
}
}
}
void main()
{
auto s = S(1, "string");
}
Jan 20 2016
On Wednesday, 20 January 2016 at 13:04:23 UTC, Daniel Kozak wrote:
V Wed, 20 Jan 2016 12:38:20 +0000
pineapple via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com>
napsáno:
import std.stdio;
struct S
{
this(Args...)(Args args)
{
foreach(Type, Value;args)
{
writefln("Type: %s value: %s", Args[Type].stringof,
Value);
}
}
}
void main()
{
auto s = S(1, "string");
}
Type should be named Index :)
Jan 20 2016









Daniel Kozak <kozzi11 gmail.com> 