digitalmars.D.learn - Looking to get typeof parseXML return value
- Chris Piker (24/24) Sep 06 2021 Hi D
- jfondren (35/44) Sep 06 2021 Here's a quick script:
- Chris Piker (8/9) Sep 06 2021 Hey, I like this trick!
- Paul Backus (3/10) Sep 07 2021 You can always use the `.init` value of a type (in this case,
- Chris Piker (10/17) Sep 06 2021 Though it's ususally bad form to respond to one's own question.
- JN (5/9) Sep 07 2021 I struggle with this often. Templated types that pretty much
- bauss (6/15) Sep 07 2021 I agree with this, that's why I avoid templates anywhere they're
Hi D I'm using the **dxml** library since I like it's "pull here for more data" mentality. I've come across the need to save an entity range created by the `parseXML` function as a class member so that I can tuck it away and pull more data as needed. Like almost all new users to D I'm tripping over how to save and pass around variables since nothing has an understandable type anymore and you can't use "auto" for *class member* storage types. Any ideas on how to get the return type of `parseXML` below: ``` import dxml.parser; const(char)[] _mmfile; //_mmfile initialization TYPE??? _entityRng = parseXML!(simpleXML)(_mmfile); ``` *before* calling parseXML, so that it can be a class member variable? I've tried variations on `typeof` and `.inputRangeObject` etc. with no success so far. Thanks for any advice :) All this would be so much easier if dxml just defined `Entity` at the top level of the parser module instead of burying it inside a templated struct. Then the type could just be `InputRange!Entity` which is easy to work with.
Sep 06 2021
On Tuesday, 7 September 2021 at 04:13:08 UTC, Chris Piker wrote:Any ideas on how to get the return type of `parseXML` below: ``` import dxml.parser; const(char)[] _mmfile; //_mmfile initialization TYPE??? _entityRng = parseXML!(simpleXML)(_mmfile); ``` *before* calling parseXML, so that it can be a class member variable?Here's a quick script: ```d /++ dub.sdl: dependency "dxml" version="0.4.0" +/ import dxml.parser; import std.stdio : writeln; struct SomeXML { EntityRange!(simpleXML, string) xml; } struct Again { typeof(parseXML!simpleXML("")) xml; } void main() { auto xml = parseXML!simpleXML("<x><y></y></x>"); pragma(msg, typeof(xml)); // compile time writeln(typeid(xml)); // runtime SomeXML some = SomeXML(xml); foreach (_; 0 .. 4) { writeln(some.xml.front); some.xml.popFront; } auto again = Again(xml); writeln(again.xml.front); } ``` EntityRange have two template parameters, a Config with four flags (where simpleXML has all flags set to yes) and the type of the forward range supplying characters to be parsed. So, `EntityRange!(simpleXML, string)` works as a type of those are really what you'll be using, `typeof(parseXML!simpleXML(""))` works as a type by seeing what type parseXML returns when invoked like that.
Sep 06 2021
On Tuesday, 7 September 2021 at 04:40:25 UTC, jfondren wrote:typeof(parseXML!simpleXML("")) xml;Hey, I like this trick! I was wondering what to use for the const(char)[] variable in the typeof statement. It's blindingly obvious in retrospect. Wouldn't work so well if there wasn't a literal for the input data range type. Sorry, didn't see your response before I posted mine, but thanks for the tip anyway :)
Sep 06 2021
On Tuesday, 7 September 2021 at 05:00:50 UTC, Chris Piker wrote:On Tuesday, 7 September 2021 at 04:40:25 UTC, jfondren wrote:You can always use the `.init` value of a type (in this case, `(const(char)[]).init`) as a dummy value for things like this.typeof(parseXML!simpleXML("")) xml;Hey, I like this trick! I was wondering what to use for the const(char)[] variable in the typeof statement. It's blindingly obvious in retrospect. Wouldn't work so well if there wasn't a literal for the input data range type.
Sep 07 2021
On Tuesday, 7 September 2021 at 04:13:08 UTC, Chris Piker wrote:Any ideas on how to get the return type of `parseXML` below: ``` import dxml.parser; const(char)[] _mmfile; //_mmfile initialization TYPE??? _entityRng = parseXML!(simpleXML)(_mmfile); ```Though it's ususally bad form to respond to one's own question. I hope to avoid wasting your time on this question. Just reading the source instead of trying to pull some typeof wizardry gives the answer: ``` EntityRange!(simpleXML, const(char)[]) _rEntity; _rEntity = parseXML!(simpleXML)(_data); ``` Sorry for the forum noise, carry on.
Sep 06 2021
On Tuesday, 7 September 2021 at 04:13:08 UTC, Chris Piker wrote:Like almost all new users to D I'm tripping over how to save and pass around variables since nothing has an understandable type anymore and you can't use "auto" for *class member* storage types.I struggle with this often. Templated types that pretty much require you to use auto look nice on the paper and in samples, but once you try to integrate them into a larger project, it can get messy.
Sep 07 2021
On Tuesday, 7 September 2021 at 08:27:33 UTC, JN wrote:On Tuesday, 7 September 2021 at 04:13:08 UTC, Chris Piker wrote:I agree with this, that's why I avoid templates anywhere they're not necessary and wrap everything in structs/classes with little to no template parameters. Templates gets messy soon because you often end up overengineering trivial problems.Like almost all new users to D I'm tripping over how to save and pass around variables since nothing has an understandable type anymore and you can't use "auto" for *class member* storage types.I struggle with this often. Templated types that pretty much require you to use auto look nice on the paper and in samples, but once you try to integrate them into a larger project, it can get messy.
Sep 07 2021