const challenge

Kris foo at bar.com
Thu Jan 31 20:55:57 PST 2008


Steve - some D2 questions for ya:

# char[] other (const(char)[] input, char[] output = null);
#
# auto result = other (niftyStringFunction(x, y));

no nesting/chaining of results permitted? That would be a problem :)

I find the current const approach to exhibit various opposing tensions, so 
I'm not exactly a 'fan' at this time. Taking an example related to the 
discussion, and if I understand correctly, the variations on Unique!(T) are 
little more than attempts to hide a cast() operation under a sugary coating, 
right? A need for such sugar in the language syntax would surely tend to 
indicate a notable flaw?

As far as the example api-style goes, here's hoping that this will work 
correctly in D2:

# char[] other (in char[] input, char[] output = null);

where 'in' indicates only that the input will not be modified by the callee. 
By doing so, the following should be clean:

# char[] nested (in char[] input, char[] output = null);
#
# auto result = nested (other (x, y));

Given that the input cannot be modified, you should also be able to pass a 
const or invariant string as an 'in' parameter; right?

# auto result = other ("foo");

If that's the case, then ok.

Assuming all is kosher so far, sliced 'in' arguments should also be accepted 
in the same fashion:

# auto result = other ("foo"[0..2]);

right?

Additionally, the optional output should always be good for use with slices 
also:

# char[32] buf;
#
# auto result = other ("foo", buf[0..8]);

yes?

- Kris


"Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message 
news:fnu30k$2fnm$1 at digitalmars.com...
> "Janice Caron" wrote
>> OK, here's an example, borrowed a bit from Tango.
>>
>>    char[] niftyStringFunction(char[] outBuffer, char[] input)
>>    {
>>        /*...*/
>>    }
>>
>> The idea is, if (outBuffer == null) then the function allocates memory
>> and returns it; whereas, if (outBuffer != null) then the result is
>> writen into outBuffer, and the return value is a slice of outBuffer.
>> (The idea is to minimize allocations).
>>
>> Converting this to D2 is problematic, because you want to return a
>> string, but string is invariant, so if you do that, you won't be able
>> to pass the same buffer back into other, similarly nifty string
>> functions.
>>
>
> char[] niftyStringFunction(char[] outBuffer, const(char)[] input)
>
> The function contract says, "I may write to outBuffer, I won't write to 
> input.  You can write to the result if you wish".
>
> How is this not what you want?
>
> -Steve
> 





More information about the Digitalmars-d mailing list