How to get struct's members ?
bioinfornatics via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri May 23 02:51:14 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.
>
> If that's not the case, you can create another struct, holding
> both
> the original struct and the delegates...
>
> I did some generic range-like work on tuples a few years ago.
> See for example:
>
> https://github.com/PhilippeSigaud/dranges/blob/master/tuple.d#L620
>
> Could you explain what you want with more details?
I would like to create a generic parser for simple case.
User provide a sructure to fill:
struct A {
string a;
string b
}
In another way they are a struct whicg describe how to get start
and end section.
alias predicate = bool delegate( immutable(ubyte)[] );
struct Statement
{
immutable bool delegate( immutable(ubyte)[] ) start;
immutable bool delegate( immutable(ubyte)[] ) end;
immutable bool isOptional;
@safe nothrow
this( in predicate start, in predicate end, isOptional =
false )
{
this.start = start;
this.end = end;
this.isOptional = isOptional;
}
}
Like this you could say members a start by @ and end by a newline
Statement sequenceLine = Statement(
( word ) =>
word[0] >= 'A' && word[0] <= 'z',
( word ) =>
word[0] == '\n' );
Once all section are delimited give them to eat to a parser. You
have not to write a parser for each new file format
More information about the Digitalmars-d-learn
mailing list