www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why is Exceptions c'tor this(immutable(char[]) instead of

reply "Mathias Laurenz Baumann" <anonym001 supradigital.org> writes:
Well, topic says it all. Why is the string parameter for the Exception  
class in object.di an immutable(char[])?

A const(char[]) works for both, immutable(char[]) types and char[] types.  
why this limitation?


--Marenz
Aug 26 2010
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 26 Aug 2010 16:05:00 -0400, Mathias Laurenz Baumann  
<anonym001 supradigital.org> wrote:

 Well, topic says it all. Why is the string parameter for the Exception  
 class in object.di an immutable(char[])?

 A const(char[]) works for both, immutable(char[]) types and char[]  
 types. why this limitation?

Normally, I'd agree with you, but if the exception gets a immutable(char)[], it can store it without duplication, and without worry it will change it. If you passed in a const, then you'd *have* to duplicate it. If you have a char[] or a const(char)[] that you want to pass in, call idup on it. -Steve
Aug 26 2010
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 8/26/10 13:16 PDT, Steven Schveighoffer wrote:
 On Thu, 26 Aug 2010 16:05:00 -0400, Mathias Laurenz Baumann
 <anonym001 supradigital.org> wrote:

 Well, topic says it all. Why is the string parameter for the Exception
 class in object.di an immutable(char[])?

 A const(char[]) works for both, immutable(char[]) types and char[]
 types. why this limitation?

Normally, I'd agree with you, but if the exception gets a immutable(char)[], it can store it without duplication, and without worry it will change it. If you passed in a const, then you'd *have* to duplicate it. If you have a char[] or a const(char)[] that you want to pass in, call idup on it.

Better yet: to!string(whatever). Leaves actual immutable stuff non-duplicated/ Andrei
Aug 26 2010
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 8/26/10 13:24 PDT, Mathias Laurenz Baumann wrote:
 Okay, sounds reasonable.

 What about many of the phobos functions, like for example from std.regex:

 int find(string s, RegExp pattern);

 I mean, that function won't store anything, right?

Good point. I think Regexp does actually store slices of its input string, but that artifact is not visible in the interface. I will look into it. Andrei
Aug 26 2010
prev sibling next sibling parent "Mathias Laurenz Baumann" <anonym001 supradigital.org> writes:
Okay, sounds reasonable.

What about many of the phobos functions, like for example from std.regex:

int find(string s, RegExp pattern);

I mean, that function won't store anything, right?

--Marenz


Am 26.08.2010, 22:16 Uhr, schrieb Steven Schveighoffer  
<schveiguy yahoo.com>:

 On Thu, 26 Aug 2010 16:05:00 -0400, Mathias Laurenz Baumann  
 <anonym001 supradigital.org> wrote:

 Well, topic says it all. Why is the string parameter for the Exception  
 class in object.di an immutable(char[])?

 A const(char[]) works for both, immutable(char[]) types and char[]  
 types. why this limitation?

Normally, I'd agree with you, but if the exception gets a immutable(char)[], it can store it without duplication, and without worry it will change it. If you passed in a const, then you'd *have* to duplicate it. If you have a char[] or a const(char)[] that you want to pass in, call idup on it. -Steve

Aug 26 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 26 Aug 2010 16:24:57 -0400, Mathias Laurenz Baumann  
<anonym001 supradigital.org> wrote:

 Okay, sounds reasonable.

 What about many of the phobos functions, like for example from std.regex:

 int find(string s, RegExp pattern);

 I mean, that function won't store anything, right?

This is where I normally agree with you ;) From what I understand, there are quite a few phobos modules that need some TLC. -Steve
Aug 26 2010
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 8/26/10 13:05 PDT, Mathias Laurenz Baumann wrote:
 Well, topic says it all. Why is the string parameter for the Exception
 class in object.di an immutable(char[])?

 A const(char[]) works for both, immutable(char[]) types and char[]
 types. why this limitation?


 --Marenz

The string is stored inside the Exception object. If it's immutable then there's a guarantee it will last forever. Andrei
Aug 26 2010