Checking function parameters in Phobos

Meta jared771 at gmail.com
Wed Nov 20 23:57:17 PST 2013


On Thursday, 21 November 2013 at 07:36:38 UTC, Jacob Carlborg 
wrote:
> On 2013-11-20 19:53, Meta wrote:
>
>> Yes. It is very important not to allow direct access to the 
>> underlying
>> value. This is important for ensuring that it is not put in an 
>> invalid
>> state. This is a mistake that was made with 
>> std.typecons.Nullable,
>> making it useless for anything other than giving a 
>> non-nullable type a
>> null state (which, in fairness, is probably all that it was 
>> originally
>> intended for).
>
> In that case all string functionality needs to be provided 
> inside the Validated struct. In addition to that we loose the 
> beauty of UFCS, at least for functions expecting plain "string".

This is tricky business. Unfortunately, having the wrapper be 
able to degrade to its base type is at odds with providing 
compiler-enforced guarantees. We can't allow direct access to the 
underlying string, because the user could purposely or 
inadvertently put it in an invalid state. On the other hand, 
these opaque wrapper types can no longer be transparently 
substituted into existing code. One solution is copying the 
validated string to do arbitrary operations on, leaving the 
original validated string unchanged.

auto validatedString = validate!isValidUTF(someString);
//Doesn't work; Validated!string does not expose the string 
interface
//auto invalidString = validatedString.map!(c => c - 
cast(char)int.max);
//Also doesn't work
//validatedString ~= cast(char)0xFFFF
auto validatedCopy = validatedString.duplicate();
//Do bad things with validatedCopy. validatedString remains 
unchanged and valid


More information about the Digitalmars-d mailing list