Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
April 17, 2017 Empty Result | ||||
---|---|---|---|---|
| ||||
Attachments:
| I find myself in need of constructing an empty Result object. I tried takeNone!Result, but obviously the type Result doesn't appear to exist. Anyone any ideas?
--
Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
|
April 17, 2017 Re: Empty Result | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | On 04/17/2017 11:30 AM, Russel Winder via Digitalmars-d-learn wrote: > I find myself in need of constructing an empty Result object. I tried > takeNone!Result, but obviously the type Result doesn't appear to exist. > Anyone any ideas? > (Not a complete solution; just sharing your pain.) I had the same problem here: http://ddili.org/ders/d.en/fibers.html#ix_fibers.Generator,%20std.concurrency The solution I could use there is less than ideal and may not apply in all cases. I used typeof and was able to satisfy it with an empty delegate: /* Returns an InputRange to the nodes of the tree. The * returned range is empty if the tree has no elements (i.e. * if 'root' is 'null'). */ auto byNode(const(Tree) tree) { alias RangeType = typeof(byNode(tree.root)); return (tree.root ? byNode(tree.root) : new RangeType(() {})); // ← Empty range } Ali |
April 17, 2017 Re: Empty Result | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Mon, Apr 17, 2017 at 12:05:19PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: [...] > auto byNode(const(Tree) tree) { > alias RangeType = typeof(byNode(tree.root)); Could this possibly be simplified to: alias RangeType = typeof(return); ? Or does that cause a circular dependency that makes the compilation fail? > return (tree.root > ? byNode(tree.root) > : new RangeType(() {})); // ← Empty range > } [...] T -- WINDOWS = Will Install Needless Data On Whole System -- CompuMan |
April 17, 2017 Re: Empty Result | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 04/17/2017 12:33 PM, H. S. Teoh via Digitalmars-d-learn wrote:
> On Mon, Apr 17, 2017 at 12:05:19PM -0700, Ali Çehreli via Digitalmars-d-learn wrote:
> [...]
>> auto byNode(const(Tree) tree) {
>> alias RangeType = typeof(byNode(tree.root));
>
> Could this possibly be simplified to:
>
> alias RangeType = typeof(return);
>
> ?
Thank you. That's much better but only if I restructure the code to use an if statement:
auto byNode(const(Tree) tree) {
if (tree.root) {
return byNode(tree.root);
}
else {
alias RangeType = typeof(return);
return new RangeType(() {});
}
}
That works because the return type of the function is the first return statement. (My original code had only one return and that was the problem.)
Ali
|
April 18, 2017 Re: Empty Result | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli Attachments:
| On Mon, 2017-04-17 at 15:10 -0700, Ali Çehreli via Digitalmars-d-learn wrote: > alias RangeType = typeof(return); > return new RangeType(() {}); It appears the answer to my question is: return typeof(return)(); Thanks to Ali and T for setting on the right path. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
Copyright © 1999-2021 by the D Language Foundation