Using std.algorithm.map: Error: cannot implicitly convert expression of type MapResult!

Philippe Sigaud philippe.sigaud at gmail.com
Tue Dec 10 22:51:33 PST 2013


On Wed, Dec 11, 2013 at 7:26 AM, Dfr <deflexor at yandex.ru> wrote:
>
> Thank you, this clears thing to me.
>
> I currently has all stuff wrapped in Variants because it is structure parsed
> from xml (or json), and it could be for example map of Something[string],
> where Something could be string or array or another map, and also this all
> nested few levels deep. I'm new to D and maybe there is better solution
> exists how to represent such structure ?

For JSON, since the range of types is quite limited, different
solutions exists, that depend on what kind of code you prefer:

- Having a JSONNode type (a struct, or a class) with a 'type' field
(say, a string), that would store whether it's a JSON string, an array
of other JSON elements, or a map of JSON elements.
Since the range of JSON types is limited, you could have an external
enum (enum JSONType {String, Array, Map, ... }) and have you JSONNode
contain a member of this kind.
External code could then check this field to act accordingly.

- Having a hierarchy of class, with a root JSONNode (abstract, I
suppose) class, and other, derived classes (JSONArray, JSONMap, ...).
A JSONArray would contain an array of JSONNode's, and so on.

- Having a bunch of templated structs that would hold together by
template constraints, but I don't have the space here to explain it.
It's much more complicated and the only advantage I see is being able
to construct a value at compile-time.

- Using std.variant.Algebraic instead of std.variant.Variant is also an option.


For XML, that depends whether you have a DTD, a Schema or whatever,
something that limits the kind of XML files you'll receive.

- If not, then use a simple node struct, with a name (the XML node
name) and an array of nodes as children. It'll give you a generic
tree, that is quite OK to represent XML. classes or structs are OK and
are mainly a matter of taste here (other might chime in this thread
and have other arguments, I'm more a struct guy myself).
You'd have to think about how to correclty represent attributes.

That also depend of your end goal: just learning to read and
manipulate this kind of data in D? Or create a new XML value and write
it somewhere? Validation of content (when reading) or allowing the
construction only of valid values (when creating) are also interesting
subjects.

- If you have a DTD and it's fixed, then you can follow the JSON
approach: a hierarchy of XML nodes, each containing what's required by
the DTD.

- If you know you have a DTD but do not know it in advance, then it's
still possible, but not in a forum post :) You would have to first
parse the DTD, determine what kind of XML node is authorized and then
create the required D code. Just know it's perfectly feasible, though
more advanced than the other solutions.


Cheers,


Philippe


More information about the Digitalmars-d-learn mailing list