Thread overview | |||||
---|---|---|---|---|---|
|
February 01, 2017 Convert struct to set of fields and pass it's to constructor | ||||
---|---|---|---|---|
| ||||
Class constructor accept only set if fields like this(string login, string pass) Can I create structure, fill it, and than pass to constructor? Like this: ``` import std.stdio; struct ConnectSettings { string login; string pass; }; ConnectSettings cs; void main() { cs.login = "admin"; cs.pass = "mypass"; } ``` and than pass to constructor it's like: `... new SomeClass(cs)` I tried, but get error, that ctor accept only fields list. Can I unroll struct to it? |
February 01, 2017 Re: Convert struct to set of fields and pass it's to constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 1 February 2017 at 13:37:27 UTC, Suliman wrote:
> Class constructor accept only set if fields like
> this(string login, string pass)
>
> Can I create structure, fill it, and than pass to constructor?
>
> Like this:
> ```
> import std.stdio;
>
> struct ConnectSettings
> {
> string login;
> string pass;
> };
> ConnectSettings cs;
>
> void main()
> {
> cs.login = "admin";
> cs.pass = "mypass";
> }
> ```
>
> and than pass to constructor it's like:
>
> `... new SomeClass(cs)`
>
>
> I tried, but get error, that ctor accept only fields list. Can I unroll struct to it?
tupleof is probably what you're looking for.
ie.
new SomeClass(cs.tupleof);
That said, why not have the constructor accept the struct as a parameter?
|
February 01, 2017 Re: Convert struct to set of fields and pass it's to constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minty Fresh | On Wednesday, 1 February 2017 at 13:51:28 UTC, Minty Fresh wrote:
> On Wednesday, 1 February 2017 at 13:37:27 UTC, Suliman wrote:
>> Class constructor accept only set if fields like
>> this(string login, string pass)
>>
>> Can I create structure, fill it, and than pass to constructor?
>>
>> Like this:
>> ```
>> import std.stdio;
>>
>> struct ConnectSettings
>> {
>> string login;
>> string pass;
>> };
>> ConnectSettings cs;
>>
>> void main()
>> {
>> cs.login = "admin";
>> cs.pass = "mypass";
>> }
>> ```
>>
>> and than pass to constructor it's like:
>>
>> `... new SomeClass(cs)`
>>
>>
>> I tried, but get error, that ctor accept only fields list. Can I unroll struct to it?
>
> tupleof is probably what you're looking for.
> ie.
>
> new SomeClass(cs.tupleof);
>
> That said, why not have the constructor accept the struct as a parameter?
Because it's implemented not by me, and I do not want to touch its realization.
|
Copyright © 1999-2021 by the D Language Foundation