Checking function parameters in Phobos
Meta
jared771 at gmail.com
Wed Nov 20 10:53:43 PST 2013
On Wednesday, 20 November 2013 at 18:30:58 UTC, Dmitry Olshansky
wrote:
> 20-Nov-2013 22:01, Simen Kjærås пишет:
>> On 20.11.2013 18:45, Simen Kjærås wrote:
> [snip]
>>> May I suggest:
>>>
>>> struct Validated(alias fn, T) {
>>> private T value;
>>> @property inout
>>> T get() {
>>> return value;
>>> }
>>
>> Uh-hm. Add this:
>> alias get this;
>>
>
> And it decays to the naked type in a blink of an eye. And some
> function down the road will do the validation again...
>
>>> }
>>>
>>> Validated!(fn, T) validate(alias fn, T)(T value) {
>>> Validated!(fn, T) result;
>>> fn(value);
>>> result.value = value;
>>> return result;
>>> }
>>>
>>> void
>>> functionThatTakesSanitizedFileNames(Validated!(sanitizeFileName,
>>> string) path) {
>>> // Do stuff
>>> }
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).
More information about the Digitalmars-d
mailing list