www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Empty Result

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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?

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
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
Apr 17 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Apr 17 2017
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
Apr 17 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Apr 17 2017
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 2017-04-17 at 15:10 -0700, Ali =C3=87ehreli via Digitalmars-d-learn
wrote:
 alias RangeType =3D typeof(return);
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 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
Apr 18 2017