Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 January 2021 at 21:57:43 UTC, sighoya wrote: On Saturday, 9 January 2021 at 20:20:38 UTC, Q. Schroll wrote: That's not what I mean. You copy the reference. That's not what referencing meant. Derived d = new Derived(); Base* bp = &d; // fails const(Base) cbp = &d; // compil

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread sighoya via Digitalmars-d-learn
On Saturday, 9 January 2021 at 20:20:38 UTC, Q. Schroll wrote: That's not what I mean. You copy the reference. That's not what referencing meant. Derived d = new Derived(); Base* bp = &d; // fails const(Base) cbp = &d; // compiles. Generally, allowing covariant assignment for the Ptr, i

Re: How can I do lazy variable initialization?

2021-01-09 Thread Jack via Digitalmars-d-learn
On Saturday, 9 January 2021 at 20:39:26 UTC, Ali Çehreli wrote: On 1/9/21 12:35 PM, Ali Çehreli wrote: > alias lightLoadOperation = memoize!heavyLoadOperation; > >const l = lightLoadOperation(); Well, that doesn't work the way you want but this does: if (args.length == 1) { writefln!

Re: How can I do lazy variable initialization?

2021-01-09 Thread Ali Çehreli via Digitalmars-d-learn
Explicit with a lambda: import std; int heavyLoadOperation() { writeln("Expensive!"); return uniform(0, 10); } void main(string[] args) { const l = { bool inited = false; static int i; if (!inited) { i = heavyLoadOperation(); } return i; }(); if (args.length

Re: How can I do lazy variable initialization?

2021-01-09 Thread Ali Çehreli via Digitalmars-d-learn
On 1/9/21 12:35 PM, Ali Çehreli wrote: > alias lightLoadOperation = memoize!heavyLoadOperation; > >const l = lightLoadOperation(); Well, that doesn't work the way you want but this does: if (args.length == 1) { writefln!"Using lazy variable: %s %s"(lightLoadOperation(), lightLoadOper

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 January 2021 at 20:00:35 UTC, Jacob Carlborg wrote: On 2021-01-09 19:16, Q. Schroll wrote: Say I have a class hierarchy like this:   class Base { }   class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense t

How can I do lazy variable initialization?

2021-01-09 Thread Jack via Digitalmars-d-learn
In c# we can do something like this: static Lazy lazy = new Lazy (() => heavyLoadOperation()); static T value { get { return lazy.Value; } } and heavyLoadOperation() is only called when variable "value" is actually used. How can I do something like this in D? I've tried

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Jacob Carlborg via Digitalmars-d-learn
On 2021-01-09 19:16, Q. Schroll wrote: Say I have a class hierarchy like this:   class Base { }   class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense to me. It can: Base b = new Derived(); One can replace Base by a

dirEntries: How get "." and ".."?

2021-01-09 Thread kdevel via Digitalmars-d-learn
Why does dirEntries in std/file.d do that (POSIX version)?: for (dirent* fdata; (fdata = readdir(_stack[$-1].h)) != null; ) { // Skip "." and ".." if (core.stdc.string.strcmp(&fdata.d_name[0], ".") && core.stdc.string.

[Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
Say I have a class hierarchy like this: class Base { } class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense to me. One can replace Base by a @system delegate type (SysDG) and Derived by a @safe delegate type (SafeDG)