template among(values...)
{
uint among(Value)(Value value)
{
switch (value) {
foreach (i, v; values) {
case v:
return i + 1;
}
default:
return 0;
}
}
}
void main() {
int a, b, c;
2.among!(a, b, c);
}
Comment #1 by hsteoh — 2014-06-26T15:11:45Z
Still happens on git HEAD.
Comment #2 by safety0ff.bugz — 2015-04-19T05:04:19Z
*** This issue has been marked as a duplicate of issue 7392 ***
Comment #3 by schuetzm — 2015-04-21T07:31:39Z
Is it really a duplicate? The code in the other bug is clearly invalid, but this one should work IMO.
Comment #4 by mk — 2015-04-21T15:20:28Z
(In reply to Marc Schütz from comment #3)
> Is it really a duplicate? The code in the other bug is clearly invalid, but
> this one should work IMO.
In this example 'v' is a runtime variable and as such it cannot be a case variable.
If a,b,c were enums, it would compile.
But I don't understand why uint a,b,c gives error message
Error: variable v cannot be read at compile time
but int a,b,c does not. So maybe it's a different bug.
Comment #5 by schuetzm — 2015-04-22T10:41:11Z
(In reply to Martin Krejcirik from comment #4)
> In this example 'v' is a runtime variable and as such it cannot be a case
> variable.
No, the foreach iterates over `values`, which is a tuple. It should therefore get unrolled, and `v` should become a constant.
Comment #6 by mk — 2015-04-22T11:44:32Z
(In reply to Marc Schütz from comment #5)
> No, the foreach iterates over `values`, which is a tuple. It should
> therefore get unrolled, and `v` should become a constant.
It cannot become a constant if a,b,c are not constants. It could still work if foreach is unrolled at compile-time, but that would be an enhancement request, I think.
Comment #7 by schuetzm — 2015-04-22T17:37:24Z
(In reply to Martin Krejcirik from comment #6)
> (In reply to Marc Schütz from comment #5)
> > No, the foreach iterates over `values`, which is a tuple. It should
> > therefore get unrolled, and `v` should become a constant.
>
> It cannot become a constant if a,b,c are not constants. It could still work
> if foreach is unrolled at compile-time, but that would be an enhancement
> request, I think.
foreach over a tuple _is_ unrolled, that's already implemented behaviour. But you're right that `values` could indeed refer to global or even local vars, didn't think of that.