www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Generic assumeUnique

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Why isn't `std.exception.assumeUnique` generically defined as

```d
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
parent reply Dennis <dkorpel gmail.com> writes:
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)`?
May 05 2022
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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.
May 05 2022