December 11, 2013
On 12/11/13 6:25 AM, qznc wrote:
> On Tuesday, 10 December 2013 at 18:15:58 UTC, Andrei Alexandrescu wrote:
>> On 12/10/13 9:40 AM, JR wrote:
>>> On Tuesday, 10 December 2013 at 17:28:26 UTC, Andrei Alexandrescu wrote:
>>>> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a
>>>> collection of exactly one value, but not a type for "a value of type T
>>>> or nothing at all".
>>>
>>> Is this not what Nullable!T is?
>>
>> Nullable is not a range.
>
> Nullable!T is a type for "a value of type T or nothing at all". It also
> provides a range interface if the T value is a range.
>
> Where would you use an explicit zero or one element range?

See the URL I posted in the opening of this thread.

Andrei

July 01, 2014
On 2013-12-10 17:28:26 +0000, Andrei Alexandrescu said:

> I talked to a programmer who knows Scala (among others) and he mentioned the usefulness of the Option type - a zero or one element collection (range in D terminology). Here's an article discussing it: http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html 
> 
> 
> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a collection of exactly one value, but not a type for "a value of type T or nothing at all". Should we follow Scala's example and add it?
> 
> 
> Andrei

Did anything ever come of this?  If there's nothing in Phobos yet, I can start working on it and submit a PR.   If we can get this formalized and into the good usage, it seems like we would not need a .? operator.

-Shammah

July 01, 2014
On Tuesday, 1 July 2014 at 11:37:15 UTC, Shammah Chancellor wrote:
> On 2013-12-10 17:28:26 +0000, Andrei Alexandrescu said:
>
>> I talked to a programmer who knows Scala (among others) and he mentioned the usefulness of the Option type - a zero or one element collection (range in D terminology). Here's an article discussing it: http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html
>> 
>> 
>> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a collection of exactly one value, but not a type for "a value of type T or nothing at all". Should we follow Scala's example and add it?
>> 
>> 
>> Andrei
>
> Did anything ever come of this?  If there's nothing in Phobos yet, I can start working on it and submit a PR.   If we can get this formalized and into the good usage, it seems like we would not need a .? operator.
>
> -Shammah

I'd strongly vote against an Option type, because we already have
Nullable (although I would much more like the nomenclature of
Option). Nullable is trivial to convert into a range, the
function of which could simply be provided in Phobos.

Actually, like bearophile pointed out, it's not that Option is a
range, but that ranges and Option are monads, e.g. mappable and
flattenable. Being mappable and flattenable is no unique trait of
ranges, that's where the mixup comes from I think.

Of course, we could just say that "range" is D lingo for "monad",
but that will be kind of strange in the long run...
July 01, 2014
On Tuesday, 10 December 2013 at 17:28:26 UTC, Andrei Alexandrescu wrote:
> I talked to a programmer who knows Scala (among others) and he mentioned the usefulness of the Option type - a zero or one element collection (range in D terminology). Here's an article discussing it: http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html
>
> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a collection of exactly one value, but not a type for "a value of type T or nothing at all". Should we follow Scala's example and add it?
>
>
> Andrei

I am strongly in favour of an option type and the range behaviour. You can express things like chain(optionalFoo, optionalBar, optionalBaz) and get a sequece of only non-optional values out of it. It's one of those sublte things where you might ask, "What's the point?" Once you use it a lot you can express some nice things with it.

I'm also strongly in favour of Option/Maybe types in general, as a replacement for null. null is one of my pet hates, and things are much nicer when the lack of a value is represented in the type system. (T or Option!T)
July 01, 2014
On Tuesday, 1 July 2014 at 11:37:15 UTC, Shammah Chancellor wrote:
> On 2013-12-10 17:28:26 +0000, Andrei Alexandrescu said:
>
>> I talked to a programmer who knows Scala (among others) and he mentioned the usefulness of the Option type - a zero or one element collection (range in D terminology). Here's an article discussing it: http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html
>> 
>> 
>> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a collection of exactly one value, but not a type for "a value of type T or nothing at all". Should we follow Scala's example and add it?
>> 
>> 
>> Andrei
>
> Did anything ever come of this?  If there's nothing in Phobos yet, I can start working on it and submit a PR.   If we can get this formalized and into the good usage, it seems like we would not need a .? operator.
>
> -Shammah

I have a simple implementation that I've been working on off and on. It's kind of neat representing it as a range as you get all the range algorithms for free. One problem I ran into is that my first instinct is to use std.algorithm.map as the monadic bind operation, but it produces an option wrapped in a MapResult, which is no good. You could just implement map as a member function of Option!T, but it's not transparent, and it's more difficult to make it lazy.
July 01, 2014
On Tuesday, 1 July 2014 at 14:28:10 UTC, Meta wrote:
> On Tuesday, 1 July 2014 at 11:37:15 UTC, Shammah Chancellor wrote:
>> On 2013-12-10 17:28:26 +0000, Andrei Alexandrescu said:
>>
>>> I talked to a programmer who knows Scala (among others) and he mentioned the usefulness of the Option type - a zero or one element collection (range in D terminology). Here's an article discussing it: http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html
>>> 
>>> 
>>> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a collection of exactly one value, but not a type for "a value of type T or nothing at all". Should we follow Scala's example and add it?
>>> 
>>> 
>>> Andrei
>>
>> Did anything ever come of this?  If there's nothing in Phobos yet, I can start working on it and submit a PR.   If we can get this formalized and into the good usage, it seems like we would not need a .? operator.
>>
>> -Shammah
>
> I have a simple implementation that I've been working on off and on. It's kind of neat representing it as a range as you get all the range algorithms for free. One problem I ran into is that my first instinct is to use std.algorithm.map as the monadic bind operation, but it produces an option wrapped in a MapResult, which is no good. You could just implement map as a member function of Option!T, but it's not transparent, and it's more difficult to make it lazy.

std.algorithm.map corresponds to the arrow map (fmap) of the functor underlying the monad, it's not the bind operator. That's why you'll get the nesting, what you really want is flatMap, a map followed by a flatten operation (Phobos neither seems to include flatMap, nor an equivalent of flatten, just chain). The bind operator would correspond to flatMap.
July 01, 2014
Sebastian Graf:

> (Phobos neither seems to include flatMap,

I'd like a fast flatMap in Phobos.


> nor an equivalent of flatten, just chain).

joiner?
http://dlang.org/library/std/algorithm/joiner.html

Bye,
bearophile
July 01, 2014
On Tuesday, 1 July 2014 at 15:09:08 UTC, bearophile wrote:
>
> joiner?
> http://dlang.org/library/std/algorithm/joiner.html
>
> Bye,
> bearophile

Indeed.
July 01, 2014
On 12/10/2013 12:40 PM, JR wrote:
> On Tuesday, 10 December 2013 at 17:28:26 UTC, Andrei Alexandrescu wrote:
>> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a
>> collection of exactly one value, but not a type for "a value of type T
>> or nothing at all".
>
> Is this not what Nullable!T is?

Nullable lacks compile-time enforcement that you handle the "does not exist" possibility. It turns it into a run-time issue.

July 01, 2014
On Tuesday, 1 July 2014 at 16:49:38 UTC, Nick Sabalausky wrote:
> On 12/10/2013 12:40 PM, JR wrote:
>> On Tuesday, 10 December 2013 at 17:28:26 UTC, Andrei Alexandrescu wrote:
>>> We have only(x) (http://dlang.org/phobos/std_range.html#.only) to be a
>>> collection of exactly one value, but not a type for "a value of type T
>>> or nothing at all".
>>
>> Is this not what Nullable!T is?
>
> Nullable lacks compile-time enforcement that you handle the "does not exist" possibility. It turns it into a run-time issue.

I'd say, if we include an Option type, then that type should wrap a Nullable rather than a range. Conversion to a range can still happen through a function, and Option could enforce proper pattern matching or ifHasValue!projection.orElse(nullValue); goo.