On Tue, 17 Jun 2014 23:59:39 +0000
bearophile via Digitalmars-d <[email protected]> wrote:

> Jonathan M Davis:
>
> > Regardless, I fully support how out works.
>
> Why? Do you often need to return .init from out arguments? Do you
> think that adding logic to make sure you have initialized the
> argument inside the function (as in Ada and C#) isn't going to
> improve the safety of your D code? Do you think that both Ada and
> C# designers have done a design mistake? What's good in an
> argument annotation that is used to return a value and that
> accepts you to not set such value, to return a default one?

I _hate_ how Java and C# operate where they give you an error if you don't
initialize or assign a value to a variable. It's very annoying, particularly
when it's not necessary, but the compiler isn't smart enough to figure that
out. With D's solution, I don't have to do that, and the cost should be
exactly the same, because the cases where the compiler can't figure out
whether the variable is assigned to or not, it just assigns it the init value
for you, and the cases where it is smart enough, it should optimize out the
extraneous assignment that out does.

I see no safety problems with out. It guarantees that you get a consistent
value for a parameter if you don't assign a value to it, and that's
_completely_ consistent without how default-initialization works in D in
general. The worst that can happen is that you don't assign a value to an out
parameter when you meant to.  And that's not unsafe. That's just a logic bug -
and one that's trivially caught with unit tests at that. And in my experience,
it's not even a bug that's likely to happen. So, I much, much prefer D's
approach of default-initialization over having the compiler scream at me. It's
perfectly safe and is just plain less annoying. And in my experience, it's
never been bug-prone.

- Jonathan M Davis

Reply via email to