May 29, 2013
On 05/29/2013 06:25 AM, Jakob Ovrum wrote:

> I was unable to leverage std.exception.

I have looked only your short example. std.exception.ifThrown may work:

import std.typecons;
import std.exception;

alias ToThrow = Flag!"ToThrow";

// Can throw, and we want to catch
int createTheVar(ToThrow toThrow)
{
    if (toThrow == ToThrow.yes) {
        throw new Exception("bad cat!");
    }

    return 42;
}

// Can also throw, but we don't want to catch it here
int transform(int a)
{
    return a + 1;
}

int foo(ToThrow toThrow)
{
    const(int) i = createTheVar(toThrow).ifThrown!Exception(666);
    return transform(i);
}

unittest
{
    assert(foo(ToThrow.yes) == 667);
    assert(foo(ToThrow.no) == 43);
}

void main()
{}

Ali

May 29, 2013
On Wednesday, May 29, 2013 14:36:18 Jakob Ovrum wrote:
> Consider the following example:
> 
> http://dpaste.dzfl.pl/a0595ddf
> ----
> // Can throw, and we want to catch
> int createTheVar();
> 
> // Can also throw, but we don't want to catch it here
> int transform(int a);
> 
> int foo()
> {
> const(int) i;
> 
> try
> {
> i = createTheVar(); // Clearly not allowed
> }
> catch(Exception e) // For demo purposes
> {
> // Exception handling code
> }
> 
> return transform(i);
> }
> ----

Wrap the try-catch in a function.

int foo()
{
 int initI()
 {
 try
 return createTheVar();
 catch(Exception e)
 return int.init;
 }

 const int i = initI();

 return transform(i);
}

And as it's a nested function, you can even have access to foo's scope (or make it static if you don't need that).

- Jonathan M Davis
May 29, 2013
On Wednesday, May 29, 2013 15:08:24 Jonathan M Davis wrote:
> Wrap the try-catch in a function.
> 
> int foo()
> {
> int initI()
> {
> try
> return createTheVar();
> catch(Exception e)
> return int.init;
> }

Ouch, my e-mail client ate the indentation. That should be

    int initI()
    {
        try
            return createTheVar();
        catch(Exception e)
            return int.init;
    }

- Jonathan M Davis
May 29, 2013
On Wed, 29 May 2013 08:43:44 -0400, Jakob Ovrum <jakobovrum@gmail.com> wrote:

> On Wednesday, 29 May 2013 at 12:40:39 UTC, Dicebot wrote:
>> Why something like this is not usable?
>> -----------------------
>> int tmp;
>> try
>> {
>>    tmp = ...;
>> }
>> catch(..)
>> {
>> }
>> const(int) i = tmp;
>> -----------------------
>> Not really pretty but nothing crazy.
>
> const(int) i = tmp; // fails when the type has mutable indirection.

Everything mutable and immutable (and const, or inout) is implicitly convertible to const.  The above should work for any type of tmp.

Can you cite specific code that is failing?

-Steve
May 30, 2013
On Wed, 29 May 2013 15:08:24 -0400, Jonathan M Davis <jmdavisProg@gmx.com> wrote:

> Wrap the try-catch in a function.
>

I was about to suggest this.  This is the correct solution.

-Steve
May 30, 2013
Thanks for the response, everyone.

Unfortunately, it seems until we have some kind of flow analysis, using another function is the only solution after all. At least we have the option of expressing it using a standard library function, ifThrown.
1 2 3
Next ›   Last »