www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what operator(s) should I overload to prevent this?

reply drug <drug2004 bk.ru> writes:
I refactored `MyStructure` added own implementation of malloced array 
based on pureRealloc/pureFree instead of dynamic array I used before and 
now I have error:
Error: cannot implicitly convert expression get(msg.getData()) of type 
const(MyStructure) to MyStructure.

What operators should I overload to let this conversion? I've tryed 
opCast and it fails in other call sites, also I've tried opAssign - it 
didn't works, what worked is wrapping in ctor - MyStruct(const_var). But 
using ctor is not convenient.
Oct 16 2017
next sibling parent reply Biotronic <simen.kjaras gmail.com> writes:
On Monday, 16 October 2017 at 12:00:13 UTC, drug wrote:
 I refactored `MyStructure` added own implementation of malloced 
 array based on pureRealloc/pureFree instead of dynamic array I 
 used before and now I have error:
 Error: cannot implicitly convert expression get(msg.getData()) 
 of type const(MyStructure) to MyStructure.

 What operators should I overload to let this conversion? I've 
 tryed opCast and it fails in other call sites, also I've tried 
 opAssign - it didn't works, what worked is wrapping in ctor - 
 MyStruct(const_var). But using ctor is not convenient.
The reason this doesn't work is you have some sort of non-immutable pointer, reference or array in your struct. I'm not sure why this worked before, since I have no idea what your code looks like. Hence, I can only give limited advice. Notably, the reasons this gives you a warning is because it would be unsafe to do a simple cast. You might have turned immutable(T)[] into T[] (where T is int, byte, char, what-have-you). If this is the case, consider why this happened, and try to turn it back into immutable(T)[]. The same goes for immutable(T)*. Remember that an alias can make something immutable, so it might not be immediately obvious. If you're fine with being unsafe, and can't find a solution like the above, consider this: struct MyStruct { MyStruct getUnsafe() const { return cast(MyStruct)this; } alias getUnsafe this; } -- Biotronic
Oct 16 2017
parent drug <drug2004 bk.ru> writes:
16.10.2017 15:18, Biotronic пишет:
 On Monday, 16 October 2017 at 12:00:13 UTC, drug wrote:
 I refactored `MyStructure` added own implementation of malloced array 
 based on pureRealloc/pureFree instead of dynamic array I used before 
 and now I have error:
 Error: cannot implicitly convert expression get(msg.getData()) of type 
 const(MyStructure) to MyStructure.

 What operators should I overload to let this conversion? I've tryed 
 opCast and it fails in other call sites, also I've tried opAssign - it 
 didn't works, what worked is wrapping in ctor - MyStruct(const_var). 
 But using ctor is not convenient.
The reason this doesn't work is you have some sort of non-immutable pointer, reference or array in your struct. I'm not sure why this worked before, since I have no idea what your code looks like. Hence, I can only give limited advice. Notably, the reasons this gives you a warning is because it would be unsafe to do a simple cast. You might have turned immutable(T)[] into T[] (where T is int, byte, char, what-have-you). If this is the case, consider why this happened, and try to turn it back into immutable(T)[]. The same goes for immutable(T)*. Remember that an alias can make something immutable, so it might not be immediately obvious. If you're fine with being unsafe, and can't find a solution like the above, consider this: struct MyStruct {     MyStruct getUnsafe() const {         return cast(MyStruct)this;     }     alias getUnsafe this; } --   Biotronic
Hmm, I started to write reduced case and probably found the reason - I've changed slice to const data by slice to mutable data. Will investigate further. Thank you for your answer!
Oct 16 2017
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 10/16/17 8:00 AM, drug wrote:
 I refactored `MyStructure` added own implementation of malloced array 
 based on pureRealloc/pureFree instead of dynamic array I used before and 
 now I have error:
 Error: cannot implicitly convert expression get(msg.getData()) of type 
 const(MyStructure) to MyStructure.
 
 What operators should I overload to let this conversion? I've tryed 
 opCast and it fails in other call sites, also I've tried opAssign - it 
 didn't works, what worked is wrapping in ctor - MyStruct(const_var). But 
 using ctor is not convenient.
 
The issue is: int[] -> const(int)[]. This is an implicit cast from a mutable array of mutable data to a mutable array of const data. There is no way to "hook" this properly. What you need is: MyArray!int -> MyArray!(const(int)) But D doesn't do this implicitly. It can't, as you could implement MyArray!(const(int)) to be completely different from MyArray!int. If you define opCast(T : MyArray!(const(T))), then it requires an explicit cast, you need an implicit one. You could use alias this, but I don't think it's as smooth. Complicating things further, you have template functions like this: foo(T)(const(T)[]) Where D is smart enough to pull out the T there. I don't think it works with MyArray. -Steve
Oct 16 2017