April 21, 2012 Re: Docs: Section on local variables | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Saturday, 21 April 2012 at 09:15:14 UTC, Jonathan M Davis wrote:
> init solves the larger problem of uninitialized variables being garbage and
> resulting in non-deterministic behavior. And it solves it in more places than
> the Java and C# solution does
Flow analysis catches the bug early (at compile time): the default value of null is not what you want in most cases, and you don't want null to leak into your system and cause a crash at unknown time, unknown place, whether it will be deterministic is another question. In .net the problem is worse because strings are classes there and calling member methods on a null string causes NullPointerException.
| |||
April 21, 2012 Re: Docs: Section on local variables | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kagamin | It also makes good impression to newbies: I saw already a newbie in D.learn who declared a variable, forgot to initialize it with a reference to class instance and was asking why the program crashes. | |||
April 21, 2012 Re: Docs: Section on local variables | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jakob Ovrum | On 04/21/2012 02:26 PM, Jakob Ovrum wrote:
> ...
> Even if the compiler gets it wrong once in a
> blue moon, I would happily trade that for the massive benefits it gives
> you in writing readable, maintainable code.
>
It doesn't.
| |||
April 21, 2012 Re: Docs: Section on local variables | ||||
|---|---|---|---|---|
| ||||
On Saturday, April 21, 2012 07:49:47 H. S. Teoh wrote:
> Hold on a second here, I thought the original complaint was that *unused* local variables should generate a warning? What has that got to do with .init?
Reread the original post. It was two things.
1. It's an error to use a local variable without first assigning it a value.
2. It's an error to have a local variable which is never referred to.
- Jonathan M Davis
| ||||
April 22, 2012 Re: Docs: Section on local variables | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | "Andrej Mitrovic" <andrej.mitrovich@gmail.com> wrote in message news:mailman.2024.1335025462.4860.digitalmars-d@puremagic.com... > On 4/21/12, Nick Sabalausky <SeeWebsiteToContactMe@semitwist.com> wrote: >> Yes, it's initialized to .init by default. But .init may not be what the programmer intended it to be initialized to. > > Well, we could introduce a new language feature that explicitly makes the compiler verify that you've initialized a variable before using it. This could help in cases where e.g. you have static if sections and you forget to add an else clause: > > string name = none; // 'none' would be a sentinel for the compiler > enum i = 2; > static if (i == 1) { > name = ...; > } > // forgotten else clause > if (name == ...) { } // compile-time error > > This way we don't interrupt existing code that relies on .init. Hmm, it's technically possible. But if you have to *remember* to tell the compiler to make sure you've remembered to init a value, then I think we've probably blown way past the point of diminishing returns. > Either > we introduce some new syntax for initializers, or we get customized > warning options. Personally I prefer the latter, Yea, same here. > but unfortunately > Walter went full-on Steve Jobs on compiler options for some reason. Heh heh :) > > Also take note of this case: > int[string] hash; > ... > int x = hash.get("foo", 0); > > I think this is perfectly legal code that shouldn't be worthy of a warning. But how can the compiler know not to emit a warning here? That's an interesting case. I don't know how that should be handled. | |||
April 22, 2012 Re: Docs: Section on local variables | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jakob Ovrum | "Jakob Ovrum" <jakobovrum@gmail.com> wrote in message news:tavnewhepsyaqfsyngmv@forum.dlang.org... > > I've used C# a fair bit too, and this has never even happened to me (not in Java either using Eclipse). Of course, this is just a personal anecdote, but if anyone has had problems with the CFG approach in these languages, I'd like to hear more about it. Yea, I don't think I've even seen that occur or heard of a case of it happening. Of course, I'm sure *someone* has done it at some point (What *hasn't* been done?), but it's just doesn't seem to be the natural knee-jerk reaction Walter has voiced concern over it being. I dunno, maybe it's a culture issue. One of the key characteristics of C# is "safety before convenience/performance". So maybe C# people are just more likely than others to be the "look before you leap", "meaure twice, cut once" type. Just speculating, of course. > As it stands the claim that people would be tossing in half-assed explicit initializers all the time is simply not the reality. Even if the compiler gets it wrong once in a blue moon, I would happily trade that for the massive benefits it gives you in writing readable, maintainable code. > As would I. On the rare occasion when I would get a false error from C# on that, any instinctual twinge of annoyance was quickly diffused by the thought of "Well, better to have it conservative like this than risk silent errors in other places." So if that means once in a while it's gotta say "Umm, hey, uhh, is this really correct? Yea? Ok, just making sure." then that's perfectly fine by me. > I don't think this ship has sailed for D. If it were to be implemented, you could easily transition into it slowly like @property. Well, I'll be happy if you're right about that ;) > Also, the same implementation could be used to solve the problem of statically checking when const/immutable constructors are cooked/baked, which remains an unsolved problem. I'm not sure I understand what that is. | |||
April 22, 2012 Re: Docs: Section on local variables | ||||
|---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | "H. S. Teoh" <hsteoh@quickfur.ath.cx> wrote in message news:mailman.2018.1335019794.4860.digitalmars-d@puremagic.com... > On Sat, Apr 21, 2012 at 02:14:14AM -0700, Jonathan M Davis wrote: >> >> init solves the larger problem of uninitialized variables being garbage and resulting in non-deterministic behavior. And it solves it in more places than the Java and C# solution does, because it deals with stuff like initializing all of the elements in an array when it's first allocated, which they don't AFAIK. >> >> True, init doesn't make it so that programmers always remember to initialize all of their variables, and with a default-initialized variable, you have the ambiguity of whether the programmer meant to use the default or whether they forgot to initialize the variable, but that's minor in comparison to uniitialized variables being garbage values. And since there's nothing to stop a programmer from initializing a variable with an incorrect value, the fact that a variable is directly initialized by the programmer in Java and C# doesn't necessarily solve that problem any better anyway. And then there's also the irritation of the occasional forced initialization, because the compiler's flow analysis isn't good enough to detect that it's unecessary. > [...] > > Hold on a second here, I thought the original complaint was that *unused* local variables should generate a warning? What has that got to do with .init? [...] There were two original points. Warnings on unused locals was one. The other was this which, apperently, is still in the docs from, what, almost a decade ago?: "Local Variables It is an error to use a local variable without first assigning it a value. The implementation may not always be able to detect these cases. Other language compilers sometimes issue a warning for this, but since it is always a bug, it should be an error." > > -- > "You are a very disagreeable person." "NO." That reminds me of one of mine, which I kinda liked at first, but then was never really sure the intended irony of "agreeable asshole" came across quite like it was supposed to: https://www.semitwist.com/articles/article/view/seriously-do-i-even-read-any-of-the-crap-i-write... (Ugh, the trailing dots are apperently part of the URL. Stoopid off-the-shelf CMS...) | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply