Implicit conversion to mutable if no indirections?

Loara loara at noreply.com
Wed Sep 7 16:36:24 UTC 2022


On Tuesday, 6 September 2022 at 17:48:53 UTC, Quirin Schroll 
wrote:
> This one isn’t even vaguely a container of `T`.

Exactly, what you say is valid only for containers not for 
generic templated objects. It's better to provide a custom method 
that apply the needed conversion:

```d
struct myContainer(T){
   ...
   const(myContainer!(Unconst!T)) shareConst() const{
     //do stuff
   }
}
```

> I’m not entirely sure what you want to tell me. In the cases of 
> containers, it’s probably even possible to a large degree to 
> use introspection to provide a wide-ranging `clone`. The 
> relevant part is that when you clone, you have to be explicit.

I don't think that could be possible to implement such feature 
without heavy drawbacks, it's smarter to implement custom 
conversion methods for each templated object since "cloning an 
object" may assume different meaning depending of its type. For 
example in [this project](https://github.com/Loara/dclone) 
cloning an object means "create a new object that doesn't share 
any indirection with the original one", however this condition is 
often too restrictive for containers since you don't want to 
clone each stored object.

An unique solution that works for every object doesn't exists, 
it's better that each programmer implements the preferred 
strategy in his code.




More information about the Digitalmars-d mailing list