Thread overview
Generic assumeUnique
May 05, 2022
Per Nordlöw
May 05, 2022
Dennis
May 05, 2022
Per Nordlöw
May 05, 2022

Why isn't std.exception.assumeUnique generically defined as

auto assumeUnique(T)(T x) @trusted {
	return cast(immutable)x;
}

instead of currently restricted to arrays at https://github.com/dlang/phobos/blob/master/std/exception.d#L905?

May 05, 2022

On Thursday, 5 May 2022 at 08:15:35 UTC, Per Nordlöw wrote:

>

Why isn't std.exception.assumeUnique generically defined as

auto assumeUnique(T)(T x) @trusted {
	return cast(immutable)x;
}

That's not a correct use of @trusted

>

instead of currently restricted to arrays at https://github.com/dlang/phobos/blob/master/std/exception.d#L905?

The documentation says:

>

Typically, assumeUnique is used to return arrays from functions that have allocated and built them.

For a struct or class, you would typically use an immutable / pure constructor, so there's less need for it. What non-array type do you want to use assumeUnique on and why don't you cast(immutable)?

May 05, 2022

On Thursday, 5 May 2022 at 12:32:07 UTC, Dennis wrote:

>

What non-array type do you want to use assumeUnique on and why don't you cast(immutable)?

http://mir-algorithm.libmir.org/mir_string_map.html

I can use cast(immutable) but that needs to be qualified as @trusted.

StringMap is implemented using three pointers fields and one length field and is a hybrid between a normal array and an associative array.