V2 string

Derek Parnell derek at psych.ward
Thu Jul 5 05:03:15 PDT 2007


On Thu, 05 Jul 2007 00:42:25 -0700, Walter Bright wrote:

> Derek Parnell wrote:
>> The idiom I'm using is that functions that receive text have those
>> parameters as 'string' to guard against the function inadvertantly
>> modifying that which is passed, and functions that return text return
>> 'string' to guard against calling functions inadvertantly modifying data
>> that they did not create (own).
>> 
>> This leads to constructs like ...
>> 
>>    char[] result;
>> 
>>    result = SomeTextFunc(data).dup;
> 
> If you're needing to guard against inadvertent modification, that's just 
> what const strings are for. I'm not understanding the issue here.

There is no issue. I'm not raising an issue. I'm just making some
observations about my exerience so far in moving to V2. 

I'm not surprised by the effort that I'm having. I expected it. Why?
Because I knew that most of the strings I work with are text (mutable
things) and by using the D 'string', an immutable thing, for function
signatures was going to mean I'd have to changes things to suit. 

I choose to use 'string' it safe guard myself from making stupid errors in
coding. And its working. My next pass through the application code will be
to find places where I can safely return a 'text' thing instead of a
'string' thing, which is a performance turning exercise.

>> Another commonly used idiom that I had to stop using was ...
>> 
>>    char[] text;
>>    text = getvalue();
>>    if (wrongvalue(text))
>>        text = ""; // Reset to an empty string
>> 
>> I now code ...
>> 
>>        text.length = 0; // Reset to an empty string
>> 
>> which is slightly less readable.
> 
> This should do it nicely:
> 
> 	text = null;

Not really. I want an empty text and not a non-text. Also, it doesn't fit
right with other data types - the consistency thing again.

   text = typeof(text).init; 

works better for me because I can also use this construct in templates
without problems.

But really, this thread can die now. I didn't mean to go off into weird
tangental subects.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell



More information about the Digitalmars-d mailing list