New operators opStaticIndex and friends

H. S. Teoh hsteoh at quickfur.ath.cx
Tue May 14 18:07:06 UTC 2019


On Tue, May 14, 2019 at 05:27:25PM +0000, angel via Digitalmars-d wrote:
[...]
> You say:
> int i = s[0]; // rewrites to s.opStaticIndex!(0), as 0 can be
> int j = s[i]; // rewrites to s.opIndex(i), as i cannot be read at
> compile-time
> 
> But actually, in this example, s[i] CAN be figured out at
> compile-time.  What is even more problematic is that the ability of
> the compiler to figure out the value of 'i' and s[i] at compile-time
> might depend on the optimization level, thus you cannot be sure
> whether s.opStaticIndex!(1) or s.opIndex(i) will be called.
[...]

I think you're being misled by the ambiguous term "compile-time", which
can refer to very different things. See:

	https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time

In D, indexing with a variable is not considered "compile-time" in the
sense of AST manipulation (see above article for definition), although
it *can* be evaluated in CTFE.  AIUI, opStaticIndex is intended to be
used at the AST manipulation phase, since the usual "runtime" array
indexing is already supported in CTFE.

This has nothing to do with optimization levels, which happens in
codegen, long past any meaningful usage of compile-time computation as
far as AST manipulation or CTFE is concerned.


T

-- 
Too many people have open minds but closed eyes.


More information about the Digitalmars-d mailing list