On Friday, 17 April 2015 at 13:27:19 UTC, Biotronic wrote:
I've been writing a lot of generic code lately that has to deal with various kinds of numbers, and have near been driven nuts by the fact there is no uniform way to get a zero or one.

Consider:

   void foo(T)(T a) {}

   foo!T(0);

Does this work with all built-in numeric types? Yes.
Does it work with T=BigInt or Complex!float? No.

Now, those are a limited set of possibilities, and one could easily enough create a template such that

   foo!BigInt(zero!BigInt);

would work. But why can't I instead, for every numeric type, simply write

   foo(BigInt.zero);
   foo(float.one);
   foo(Complex!float.zero);
   foo(Rational!BigInt.one);
   foo(Meters.zero);

?

This would also work for strong typedefs and units of measurement, where simply assigning 0 to a variable might not work (because it lacks the correct unit).

It's a very simple change, both in the compiler and Phobos, and I could have a pull request ready tomorrow.

--
  Simen

This can be implemented via a library without requiring any changes to the language. It would look like Zero!T or One!T instead. You create a value template for it.

When you are writing less generic code, you can commit to zero or one in certain types via the prefixes. 1L, 1, 1.0, 1.0f. There's nothing for short or byte, but you can do short(1), byte(1). You can also write T(1) or T(0) to get a numeric type T with the value 0 or 1. That might be better than a template, I haven't tried it.

Reply via email to