An optional/maybe type with range semantics

Dukc ajieskola at gmail.com
Tue Feb 27 11:58:34 UTC 2018


On Monday, 26 February 2018 at 20:04:14 UTC, aliak wrote:
> Guess I could do a pointer and call new when i need to store a 
> value instead. Or maybe it's better to do it like above and 
> store as value type with default value and a boolean at the 
> site. Not sure.

You do not need a separate boolean field. If someone assigns 
"none" or calls popFront() just assign the pointer to null. When 
someone queries empty, you check whether the pointer points to 
null.  The default value is easy too: typeof(element).init.

That's the superior design, because it does not need any excess 
memory for the empty flag.




More information about the Digitalmars-d-announce mailing list