Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 07, 2017 reasoning of evaluating code after return in current block (static if return) | ||||
---|---|---|---|---|
| ||||
Hi something's been bugging me for a while, for the following code: { auto t = tuple(0,0); return true; auto v = t[5]; return false; } Why is the code following "return true" evaluated ? I know that it's the same with the C++ but I was wondering why ? My guess is for goto/labels but I find odd to evaluate it by default. Furthermore one of my issues with this is that it has a negative impact on "static if" and make it cumbersome in some situations. For instance this snippet comming from "https://github.com/uncrustify/uncrustify/issues/252" ClLinearExpression opBinary(string op) (double constant) { static if (op == "+") return new ClLinearExpression(this, 1, constant); else static if (op == "-") return new ClLinearExpression(this, 1, -constant); else static if (op == "*") return new ClLinearExpression(this, constant, 0); else static if (op == "/") return new ClLinearExpression(this, 1.0 / constant, 0); } |
May 07, 2017 Re: reasoning of evaluating code after return in current block (static if return) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bastien penavayre | On Sunday, 7 May 2017 at 22:34:14 UTC, bastien penavayre wrote: > Why is the code following "return true" evaluated ? It isn't evaluated, it is just compiled. The compile happens before it is run, so it doesn't really know if it is reachable yet. > ClLinearExpression opBinary(string op) (double constant) > { > static if (op == "+") > return new ClLinearExpression(this, 1, constant); > else > static if (op == "-") > return new ClLinearExpression(this, 1, -constant); > else > static if (op == "*") > return new ClLinearExpression(this, constant, 0); > else > static if (op == "/") > return new ClLinearExpression(this, 1.0 / constant, 0); > } I would just write it `else static if` all on one line and not indent further. Then it barely looks any different anyway. |
May 07, 2017 Re: reasoning of evaluating code after return in current block (static if return) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 7 May 2017 at 23:20:26 UTC, Adam D. Ruppe wrote:
>
> I would just write it `else static if` all on one line and not indent further. Then it barely looks any different anyway.
I just realized that I accidentally posted this while editing.
I agree with you on that this is barely different from just adding "else".
The example that I wanted to put was the following.
{
enum index = ...;
static if (I >= args.length)
return ...; //uses index
else
{
enum result = ...;
static if (!result[0])
{
static if (name.length == 0)
return ...;
else static if (result[2] >= txt.length)
return ...;
else
return ...;
}
else
{
enum next = ...;
static if (!next[0])
return ...;
else
{
...
return ...;
}
}
}
}
|
May 08, 2017 Re: reasoning of evaluating code after return in current block (static if return) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bastien penavayre | On Sunday, 7 May 2017 at 23:41:00 UTC, bastien penavayre wrote:
> On Sunday, 7 May 2017 at 23:20:26 UTC, Adam D. Ruppe wrote:
>> [...]
>
> I just realized that I accidentally posted this while editing.
> I agree with you on that this is barely different from just adding "else".
>
> [...]
compile your code with the -vcg-ast switch and look at the .cg output.
|
Copyright © 1999-2021 by the D Language Foundation