UDAs - Restrict to User Defined Types?

deadalnix deadalnix at gmail.com
Sat Nov 10 07:46:15 PST 2012


Le 09/11/2012 07:53, Nick Sabalausky a écrit :
> On Thu, 08 Nov 2012 21:24:49 -0800
> Jonathan M Davis<jmdavisProg at gmx.com>  wrote:
>
>> On Thursday, November 08, 2012 21:10:55 Walter Bright wrote:
>>> Many algorithms (at least the ones in Phobos do) already do a check
>>> to ensure the inputs are the correct kind of range. I don't think
>>> you'll get very far trying to use a range that isn't a range.
>>>
>>> Of course, you can always still have bugs in your range
>>> implementation.
>>
>> Given that a range requires a very specific set of functions, I find
>> it highly unlikely that anything which isn't a range will qualify as
>> one. It's far more likely that you screw up and a range isn't the
>> right kind of range because one of the functions wasn't quite right.
>>
>> There is some danger in a type being incorrectly used with a function
>> when that function requires and tests for only one function, or maybe
>> when it requires two functions. But I would expect that as more is
>> required by a template constraint, it very quickly becomes the case
>> that there's no way that any type would ever pass it with similarly
>> named functions that didn't do the same thing as what they were
>> expected to do. It's just too unlikely that the exact same set of
>> function names would be used for different things, especially as that
>> list grows. And given that ranges are a core part of D's standard
>> library, I don't think that there's much excuse for having a type
>> that has the range functions but isn't supposed to be a range. So, I
>> really don't see this as a problem.
>>
>
> Looking at one set of interfaces in isolation, sure the chances might
> be low. (Just like the chances of name collisions when hygeine is
> lacking, and yet we thankfully have a real module system, instead of C's
> clumsy "Well, it should usually work ok!" garbage.) But it's a terrible
> precedent. Scale things up, use ducks as common practice, and all of a
> sudden you're right back into the same old land of "no-hygeine". Bad,
> sloppy, lazy precedent. AND the presumed benefit of the duckness is
> minimal at best. Just not a good design, it makes all the wrong
> tradeoffs.
>

UDA is a really good way to express that intent.


More information about the Digitalmars-d mailing list