On Monday, 30 April 2018 at 21:11:07 UTC, Gerald wrote:
So I'm curious, what's the consensus on auto?

For local variables, it's not an unalloyed good, but it is good. When I use Java, everything is explicit, and sometimes that's nice. In D, I think I overuse `auto` for local variables. Sometimes I find myself switching it out for an explicit type, just because I want the compiler to check that the right-hand side is of the type I expect (eg with numeric conversion rules).

For function return types, though? The consensus among Phobos devs is it's a "here be dragons" sign. It's a sign that there could be breaking changes to your code without warning if you use that return type in a nontrivial way. Of course, this isn't made clear anywhere, and they don't actually make these changes all that often.

But even for that purpose, it's broken. Like your example was basically:

RBRange!(RBNode!long*) divide(RedBlackTree!long tree, long pivot, bool first)
{
  if (first) return tree.upperBound(pivot);
  return tree.lowerBound(pivot);
}

If the range type were public, your code would work. If subsequently the Phobos devs needed to change the range type, they could provide an alias, and your code would still work. If they needed to change upperBound to return a different type than lowerBound, there's nothing they can do about it; your code is broken.

But since the return type for both functions is `auto`, they can tell you you were wrong to write that code and they were right to make it stop working.

Reply via email to