www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing array to ctor

reply OP <OP_member pathlink.com> writes:
Hello all,

assuming I have the following class:
(BTW, any easier way to write the ctor?)

class SomeClass
{
public:
this(int SomeValue_, int[] SomeArray_)
{
SomeValue = SomeValue_;
SomeArray = SomeArray_;
}
private:
int SomeValue;
int[] SomeArray;
}

How can I call the constructor in a *convenient* way? I don't like this clumsy
syntax:

void main()
{
static int[] dummy = [1,2];
SomeClass SomeObject = new SomeClass(12, dummy);
}

I'd prefer to use something like:

void main()
{
SomeClass SomeObject = new SomeClass(12, [1, 2]);
}

(No, it doesn't compile, only to show what I have in mind.) Any chance to do it
this way?

Thanks, OP
Apr 19 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <d43h2m$17ci$1 digitaldaemon.com>, OP says...
Hello all,

assuming I have the following class:
(BTW, any easier way to write the ctor?)

class SomeClass
{
public:
this(int SomeValue_, int[] SomeArray_)
{
SomeValue = SomeValue_;
SomeArray = SomeArray_;
}
private:
int SomeValue;
int[] SomeArray;
}

How can I call the constructor in a *convenient* way? I don't like this clumsy
syntax:

void main()
{
static int[] dummy = [1,2];
SomeClass SomeObject = new SomeClass(12, dummy);
}

I'd prefer to use something like:

void main()
{
SomeClass SomeObject = new SomeClass(12, [1, 2]);
}

(No, it doesn't compile, only to show what I have in mind.) Any chance to do it
this way?

Thanks, OP
Sadly, this isn't supported in D (yet). The typical workaround is to use a shim like this one: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.learn/130 This way, you can create an array like this: SomeClass SomeObject = new SomeClass(12, makeArray!(int)(1, 2)); -EricAnderton at yahoo
Apr 19 2005
parent OP <OP_member pathlink.com> writes:
In article <d43lcm$1bh7$1 digitaldaemon.com>, pragma says...

Sadly, this isn't supported in D (yet).  The typical workaround is to use a shim
like this one:

http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.learn/130

This way, you can create an array like this:

SomeClass SomeObject = new SomeClass(12, makeArray!(int)(1, 2));
Thank you! OP
Apr 19 2005