An optional/maybe type with range semantics

aliak something at something.com
Mon Feb 26 20:04:14 UTC 2018


On Monday, 26 February 2018 at 16:02:58 UTC, Dukc wrote:
>
> I kinda start to see the idea... Granted, nullable is in a way 
> a range that can hold exactly one or exactly zero elements. Not 
> a bad idea at all.

Aye, ranges do not need nullability indeed. Optional doesn't need 
to adhere to the range spec of course, but it does provide some 
conveniences. There's a d-forum thread here on it actually: 
https://forum.dlang.org/thread/l87ivq$263r$1@digitalmars.com

There's a link in there on scala's optional type which is a fun 
read.

Here's a bit on the current "brokenness" nature of java's 
optional: 
https://developer.atlassian.com/blog/2015/08/optional-broken/

I like how it specifies intent (on the c++ optional): 
https://www.fluentcpp.com/2016/11/24/clearer-interfaces-with-optionalt/

Boost has a fun write-up on the motivation and design behind its 
optional type (which was incorporated in to the c++ standard): 
http://www.boost.org/doc/libs/1_66_0/libs/optional/doc/html/index.html

Meta: Is this your stuff btw? -> 
https://github.com/skirino/d-option :) me thinks I may have 
gotten some inspiration from you if so, so thanks!

>
> But shouldn't it store the value internally as a pointer, not 
> as an array, to save a bit space? When empty, it would point to 
> null.

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.


More information about the Digitalmars-d-announce mailing list