Verbose checking of range category

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Dec 11 12:48:54 PST 2010


And to further hijack the topic, a hypothetical piece of code with
constraint blocks might look something like this:

http://dpaste.org/xB2N/

On 12/11/10, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> Why not just add an extra string parameter to constraint functions
> that will show a custom error message? This kind of works (although
> you still get standard errors):
>
> import std.conv;
> import std.stdio;
>
> alias int cool;
>
> bool isCool(T, string X)()
> {
>     static if (is(T : cool))
>     {
>         return true;
>     }
>     else
>     {
>         pragma(msg, "Constraint failure: " ~ X);
>         return false;
>     }
> }
>
> void foo(X)(X x) if (isCool!(X, "foo(X) argument must be cool!")())
> {
> }
>
> void main()
> {
>     cool myvar;
>     double myvar2;
>     foo!()(myvar);
>     foo!()(myvar2);
> }
>
> On 12/11/10, Robert Jacques <sandford at jhu.edu> wrote:
>> On Sat, 11 Dec 2010 12:15:31 -0500, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>> [snip]
>>> This program will generate a valid executable, but will also print
>>> during compilation:
>>>
>>> Type int is not a random access range because:
>>>    no empty property
>>>    no front property
>>>    no popFront method
>>>    no indexing
>>>    no slicing
>>>
>>> When a programmer has an odd issue with a range check, turning
>>> verboseness of checks could help.
>>>
>>> What do you think?
>>>
>>>
>>> Andrei
>>
>> An issue with this is that failed template type checks are both extremely
>> common and expected in template code: i.e. static
>> if(isRandomAccessRange!T) {...}. So, you'll get a lot of spurious error
>> messages slowly. Add in parallel builds, and the last error message won't
>> even be the one you're looking for. So, while I think the concept is
>> usefully, I'd view this implementation as an intermediate stepping stone
>> at best (and a distraction from you fixing other bugs at worse). I'd
>> recommend, as an alternative, to have specific debug versions of the
>> checks, i.e. isRandomAccessRangeDebug, as a way to avoid false positives.
>>
>


More information about the Digitalmars-d mailing list