Generic assumeUnique

Dennis dkorpel at gmail.com
Thu May 5 12:32:07 UTC 2022


On Thursday, 5 May 2022 at 08:15:35 UTC, Per Nordlöw wrote:
> Why isn't `std.exception.assumeUnique` generically defined as
>
> ```d
> 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)`?


More information about the Digitalmars-d mailing list