May 03, 2009 Re: Throwable, Exception, and Error | ||||
|---|---|---|---|---|
| ||||
On Sun, May 3, 2009 at 4:36 PM, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote: > catch(RangeError e) eh that's supposed to be Exception | ||||
May 03, 2009 Re: Throwable, Exception, and Error | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> On Sun, May 3, 2009 at 4:14 PM, Denis Koroskin <2korden@gmail.com> wrote:
>> PHP allows function definition like this:
>>
>> void foo(Args args, string fileName = __FILE__, int line = __LINE__)
>> {
>> // do stuff
>> }
>
> Hidden feature: D2 allows it too, at least with templates.
>
> void Throw(T : Throwable, string file = __FILE__, int line = __LINE__)(T ex)
> {
> ex.file = file;
> ex.line = line;
> throw ex;
> }
>
> void foobar()
> {
> Throw(new Exception("o hai"));
> }
>
> void main()
> {
> try
> foobar();
> catch(RangeError e)
> writefln("(%s: %d) %s", e.file, e.line, e.msg);
> }
>
> I did find a sort of bug when trying a slightly different
> implementation of this though - any kind of explicit instantiation of
> the Throw template causes the __FILE__ and __LINE__ to be evaluated in
> the scope of the template rather than at the call site.
That's intentional and is used in enforce() and a couple other places in Phobos.
Andrei
| |||
May 03, 2009 Re: Throwable, Exception, and Error | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sun, May 3, 2009 at 5:07 PM, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote: >> I did find a sort of bug when trying a slightly different implementation of this though - any kind of explicit instantiation of the Throw template causes the __FILE__ and __LINE__ to be evaluated in the scope of the template rather than at the call site. > > That's intentional and is used in enforce() and a couple other places in > Phobos. That's what I figured. It wasn't until I looked at the source to std.contracts that I found out about it, though, and I doubt many other people know about it. The only mention about it in the changelog is for 2.013, where it says "Changed __FILE__ and __LINE__ so they work as parameter default initializers" but that doesn't.. well, doesn't say a lot :P In any case, I doubt the bug is intentional. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply