digitalmars.D - Should conversion of mutable return value to immutable allowed?
- =?ISO-8859-1?Q?Ali_=C7ehreli?= (17/17) Feb 24 2011 Implicit conversions to immutable in the following two functions feel
- spir (10/27) Feb 24 2011 I'm all for that. Can hardly how auto conversion in the sense mutable --...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (21/51) Feb 24 2011 It shouldn't be allowed if a reference to that char[] is left behind.
- Jesse Phillips (3/13) Feb 24 2011 Yes, dup stands for duplicate and is a property of arrays.
- Jesse Phillips (1/1) Feb 24 2011 Also there is std.exception.assumUnique()
- Simon Buerger (15/32) Feb 24 2011 Currently, the correct way to do it is to use the phobos function
- Tomek =?ISO-8859-2?B?U293afFza2k=?= (11/30) Feb 24 2011 Indeed. The returned object can be safely set to stone when its only ali...
Implicit conversions to immutable in the following two functions feel harmless. Has this been discussed before? string foo() { char[] s; return s; // Error: cannot implicitly convert expression // (s) of type char[] to string } string bar() { char[] s; return s ~ s; // Error: cannot implicitly convert expression // (s ~ s) of type char[] to string } Is there a reason why that's not possible? I am sure there must be other cases that at least I would find harmless. :) Ali
Feb 24 2011
On 02/24/2011 07:08 PM, Ali Çehreli wrote:Implicit conversions to immutable in the following two functions feel harmless. Has this been discussed before? string foo() { char[] s; return s; // Error: cannot implicitly convert expression // (s) of type char[] to string } string bar() { char[] s; return s ~ s; // Error: cannot implicitly convert expression // (s ~ s) of type char[] to string } Is there a reason why that's not possible? I am sure there must be other cases that at least I would find harmless. :) AliI'm all for that. Can hardly how auto conversion in the sense mutable --> immutable could be harmful, but may miss a meaningful point. This would esp be nice for strings, since we regularly need to use char arrays to construct textual content. Denis -- _________________ vita es estrany spir.wikidot.com
Feb 24 2011
On 02/24/2011 10:28 AM, spir wrote:On 02/24/2011 07:08 PM, Ali Çehreli wrote:It shouldn't be allowed if a reference to that char[] is left behind. Otherwise although the receiver would think that the data wouldn't change; it could be changed by that other reference. struct S { char[] s; string foo() { return s; // <-- Must not be allowed } } But when the object in question is about to go out of scope? I don't know. On the other hand, reducing some implicit behavior is also good.Implicit conversions to immutable in the following two functions feel harmless. Has this been discussed before? string foo() { char[] s; return s; // Error: cannot implicitly convert expression // (s) of type char[] to string } string bar() { char[] s; return s ~ s; // Error: cannot implicitly convert expression // (s ~ s) of type char[] to string } Is there a reason why that's not possible? I am sure there must be other cases that at least I would find harmless. :) AliI'm all for that. Can hardly how auto conversion in the sense mutable --> immutable could be harmful, but may miss a meaningful point.This would esp be nice for strings, since we regularly need to use char arrays to construct textual content. DenisI have another question: Does calling .idup copy any data below? string foo() { char[] s; return s.idup; // Is the content copied? } Ali
Feb 24 2011
Ali Çehreli Wrote:I have another question: Does calling .idup copy any data below? string foo() { char[] s; return s.idup; // Is the content copied? } AliYes, dup stands for duplicate and is a property of arrays. There was discussion for allowing immutable objects to be created and return from pure functions.
Feb 24 2011
Also there is std.exception.assumUnique()
Feb 24 2011
On 24.02.2011 19:08, Ali Çehreli wrote:Implicit conversions to immutable in the following two functions feel harmless. Has this been discussed before? string foo() { char[] s; return s; // Error: cannot implicitly convert expression // (s) of type char[] to string } string bar() { char[] s; return s ~ s; // Error: cannot implicitly convert expression // (s ~ s) of type char[] to string } Is there a reason why that's not possible? I am sure there must be other cases that at least I would find harmless. :) AliCurrently, the correct way to do it is to use the phobos function assumeUnique, like: string bar() { char[] s; return assumeUnique(s); } Note that this does only little more than casting to immutable, so you have to ensure there is no mutable reference left behind. Anyway, it might be nice if the compiler could detect some trivial cases and insert the cast appropriately. But on the other hand, the compiler will never to be able to auto-detect all cases, and so its cleaner to use assumeUnique explicitly. - Krox
Feb 24 2011
Ali =C7ehreli napisa=B3:Implicit conversions to immutable in the following two functions feel=20 harmless. Has this been discussed before? =20 string foo() { char[] s; return s; // Error: cannot implicitly convert expression // (s) of type char[] to string } =20 string bar() { char[] s; return s ~ s; // Error: cannot implicitly convert expression // (s ~ s) of type char[] to string } =20 Is there a reason why that's not possible? I am sure there must be other==20cases that at least I would find harmless. :)Indeed. The returned object can be safely set to stone when its only aliase= s to the outside world point to immutable data. Such a guarantee is express= ed in today's language by marking the function pure and all its arguments i= mmutable. The conversion is currently not allowed as the above virtue of im= mutably pure functions was discovered not too long ago. If you want it, vote up: http://d.puremagic.com/issues/show_bug.cgi?id=3D5081 --=20 Tomek
Feb 24 2011