July 22, 2012
On Sunday, July 22, 2012 23:40:16 Andrej Mitrovic wrote:
> On 7/22/12, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> > Yeah. Don't have them be template parameters unless you need to, otherwise
> > you
> > get a different template instantiation _every_ time that you call the
> > function.
> 
> I've just noticed something:
> 
> @property front(T)(T arr, string file = __FILE__, size_t line = __LINE__)
>     if (isArray!T)
> {
>     enforce(arr.length, safeFmt("%s(%s): Cannot call front on empty
> array.", file, line));
>     return std.array.front(arr);
> }
> 
> This errors at compilation: Error: properties can only have zero, one, or two parameter
> 
> Do you think I should file this?

No. The error is correct. A property can't take all of those arguments, and the fact that you gave it default arguments has no effect on the type. Granted, that could be a bit annoying in this case, since then you'd be forced to using __FILE__ and __LINE__ as template arguments, but given how default arguments work, it makes no sense to allow what you're trying to do.

On the bright side though, how often do you really need this? In my experience, the main advantages of having __FILE__ and __LINE__ as default arguments is to give exceptions the proper file and line number and to help with unit testing helper functions which need to give the file and line number of their call point on test failure rather than the line number inside of them where the test failed.

By the way, I believe that the agreed upon way to handle the check that you're doing is to use an assertion, not an exception - at least as far as Phobos goes. Using enforce there is probably going to kill your performance given the fact that it can't be inlined and front is probably used often, and it makes using ranges in a nothrow context really annoying. The caller needs to check for empty first if there's any question rather than relying on front or popFront to handle it.

- Jonathan M Davis
1 2
Next ›   Last »