April 05

On Monday, 5 April 2021 at 21:43:30 UTC, Paul Backus wrote:

>

No, because it's not my package, it's Ali Akhtarzada's.

Is there a place for it in Phobos?

April 05

On Monday, 5 April 2021 at 22:21:39 UTC, Per Nordlöw wrote:

>

On Monday, 5 April 2021 at 21:43:30 UTC, Paul Backus wrote:

>

No, because it's not my package, it's Ali Akhtarzada's.

Is there a place for it in Phobos?

I think there's a place for something like it in Phobos. Whether that's Optional itself, an improved version of std.typecons.Nullable, or something else entirely is an open question. And of course, Atila has the final say.

April 06

On Friday, 26 March 2021 at 15:12:04 UTC, Paul Backus wrote:

>

On Friday, 26 March 2021 at 13:38:01 UTC, vitoroak wrote:

>

I think one reason is that unlike Rust, D doesn't have a safe way to return Optional!(ref T) so we need front and empty so Ranges can return the items by reference.

I believe with DIP 1000 it should be possible to return something like Optional!(Ref!T), where Ref!T is a safe wrapper around a T*.

When I experimented with Ref improving on Final for pointers (Ref!T is Final!(T*)), I found it near useless. The only thing you cannot do with a Ref!T but with a T* is reassigning it and pointer arithmetic. But @safe already protects you against accidental pointer arithmetic. Accidental assignment also isn't really an issue in D. I'm all in for final as a type constructor (meaning head-const), but it probably won't lift its weight.

>

Of course the ideal would be to make ref itself into a type qualifier (ref(T)).

If you look at all the exceptions C++ has for its reference type constructor, you'll immediately see why Walter created ref as a storage class and not a type constructor. C++ reference types in many cases don't compose; e.g. vector<int&> isn't a thing.
If you really think about it, you don't need ref as a type constructor. Although allowing ref local variables wouldn't be harmful, I guess.

April 06

On Tuesday, 6 April 2021 at 01:47:47 UTC, Q. Schroll wrote:

>

On Friday, 26 March 2021 at 15:12:04 UTC, Paul Backus wrote:

>

Of course the ideal would be to make ref itself into a type qualifier (ref(T)).

If you look at all the exceptions C++ has for its reference type constructor, you'll immediately see why Walter created ref as a storage class and not a type constructor. C++ reference types in many cases don't compose; e.g. vector<int&> isn't a thing.
If you really think about it, you don't need ref as a type constructor. Although allowing ref local variables wouldn't be harmful, I guess.

Here's what the generic identity function currently looks like in D:

auto ref T identity(T)(auto ref T arg)
{
    import core.lifetime: forward;
    return forward!arg;
}

Here's what the generic identity function would look like if ref were a type qualifier:

T identity(T)(T arg)
{
    return arg;
}

I rest my case.

April 06

On Tuesday, 6 April 2021 at 02:14:17 UTC, Paul Backus wrote:

>

Here's what the generic identity function would look like if ref were a type qualifier:

T identity(T)(T arg)
{
    return arg;
}

I rest my case.

So what was the motive for not making it a type qualifier in D?

And what are the obstacles for making it that?

April 06

On Tuesday, 6 April 2021 at 20:34:08 UTC, Per Nordlöw wrote:

>

So what was the motive for not making it a type qualifier in D?

And what are the obstacles for making it that?

See https://forum.dlang.org/post/mailman.7903.1554072555.29801.digitalmars-d@puremagic.com

April 06

On Tuesday, 6 April 2021 at 02:14:17 UTC, Paul Backus wrote:

>
auto ref T identity(T)(auto ref T arg)
{
    import core.lifetime: forward;
    return forward!arg;
}

When is the call to forward needed in this case?

April 06

On Tuesday, 6 April 2021 at 20:47:31 UTC, Per Nordlöw wrote:

>

On Tuesday, 6 April 2021 at 02:14:17 UTC, Paul Backus wrote:

>
auto ref T identity(T)(auto ref T arg)
{
    import core.lifetime: forward;
    return forward!arg;
}

When is the call to forward needed in this case?

For non-copyable types. It's actually needed in both cases--we would need DIP 1040 (or something similar) to get rid of it.

April 06

On Tuesday, 6 April 2021 at 21:09:13 UTC, Paul Backus wrote:

>

For non-copyable types. It's actually needed in both cases--we would need DIP 1040 (or something similar) to get rid of it.

So let's help Walter getting DIP-1040 accepted then. :)

What else is forward needed for? The doc says

"Forwards function arguments while keeping out, ref, and lazy on
the parameters."

Why can't the compiler do that for us?

April 07
On 2021-04-05 22:01, Per Nordlöw wrote:

> Interesting. Can you show some pseudocode of this? Can those algorithms be implemented as non-member functions?

Here's an article about idiomatic Scala and optional types [1]. I think it applies to D as well.

[1] https://www.originate.com/thinking/idiomatic-scala-your-options-do-not-match

-- 
/Jacob Carlborg
1 2 3 4 5 6