How to get struct's members ?
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri May 23 06:05:26 PDT 2014
On Friday, 23 May 2014 at 08:20:05 UTC, Philippe Sigaud via
Digitalmars-d-learn wrote:
> On Fri, May 23, 2014 at 8:44 AM, monarch_dodra via
> Digitalmars-d-learn
> <digitalmars-d-learn at puremagic.com> wrote:
>> On Friday, 23 May 2014 at 01:17:18 UTC, bioinfornatics wrote:
>
>
>>> I would like to get struct's members and zip them with an
>>> action
>
>> tupleof will do what you need (mostly). However, I don't think
>> there will be
>> any way to (generically) run-time zip on the members, due to
>> probably type
>> mismatch, and memory layout. In any case, nothing trivial,
>> AFAIK.
>
> You can define a map-like (or zip-like) template to act on
> tuples as
> if they were ranges, but the resulting type will still be a
> tuple: in
> general, the members and the delegates associated with them
> will all
> have a different type.
>
> Bioinfornatics, if you know your struct members are all of the
> same
> type, you can 'cast' the tuple as an array by wrapping it in
> square
> brackets like this:
>
> [ myStruct.tupleof ]
>
> and then use the usual range algorithms.
One issue with this is that it will allocate a copy of all the
elements. This may be fine if the elements are meant for a
"read-only" operation. But it won't solve the issue if there are
any mutating operations.
Just saying.
More information about the Digitalmars-d-learn
mailing list