On Tuesday, 7 May 2013 at 10:33:58 UTC, Dicebot wrote:
On Tuesday, 7 May 2013 at 06:41:25 UTC, Meta wrote:
I don't really understand why either of these error messages
are occurring. The first is just incomprehensible, and the
second seems like it should work. In this case, rhs is fully
accessible at compile time in the expression (a + b), so why
does the compiler complain?
1) typeof don't work on types. Period. It is often inconvenient
in generic code (I'd love it typeof(T) to be T for simplicity)
but it is how it is done now. In your case pragma(msg, T) will
be correct one.
2) template parameter must be known at compile time. "rhs" is a
plain function parameter, so compiler can't be sure it is known
at compile time, so it is an error. Same goes for n. In
general, only enum's and template parameters can be assumed to
be known at compile-time. Your code seems to wrongly mix plain
variables with template code all over.
If you can explain for behavior/API you are trying to achieve,
most likely I'll be able to provide a more idiomatic D solution.
This is a little test to see if I can wrap types in structs and
do some checking at compile time. For example, statically
verifying in opAdd that neither number being added is negative. I
added in the pragma to see what T's type is because I had the
example compiling before, but two S structs added together was
giving a result of void, and I was trying to figure out why that
was happening.