-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent uninitialized variables #452
Comments
@munificent - which option would you pick? How have you handled this in the language(s) you've designed? Would be interesting to hear some "outside thoughts" on this one. (and yeah.. the real reason for pinging you is that I do want to brag a bit about the fact that I'm on my way to writing a full Perlang-to-C++-transpiler.. 😊 🙈 Pretty cool for a Lox-derivative, huh?) |
This approach is simplest. But to make it really usable where it doesn't get in the users way, it helps to have:
This is the most usable path for a statement-oriented language that doesn't have the above feature. The name for it is "definite assignment analysis". It's a flavor of control-flow analysis. Doing it isn't rocket science, but it does increase the language complexity by a notch. |
Thanks a lot for your reply and input @munificent! 🙇 Much appreciated.
This is indeed quite useful. 👍 In the C# codebase I've started working on recently since switching to a new job some month ago, the tuple-based approach seems to be used a bit here and there. One thing that's particularly nice about it is that if you refactor a method to return "more data" than just a single type, the effort to just Thinking loudly, if I wanted to implement something like this right now I could just add some simple C++ tuple wrapper class which the Perlang
This is actually an option too, and I've actually done a significant amount of Ruby coding in the past, where this pattern is prevalent. I dunno... there's something about the "implicit" part of that I kind of dislike I think, or the fact that code like this:
...it just doesn't look so pretty, I think... maybe. 🤔
Thanks. 👍 I think I'll leave this on the backburner for a while. As the language (and hopefully compiler) settles, I'll hopefully come to some form of conclusion on this. |
The following Perlang program:
...currently both compiles (when using experimental compilation, #406) and runs.
In interpreted mode
The semantics here are better, less unexpected. We probably inherited them from Lox in this case.
In compiled mode
Quite frankly, this is pretty horrible. 😬
Looking at the intermediate C++ code, this isn't very surprising in fact. It's a completely uninitialized (stack) pointer, what do we expect from it? In fact, we're quite lucky to even get garbage printed in this case, it could equally well have caused a SIGSEGV/core dump. 🙂
Going forward
We should probably do one of the following:
if
/else
blocks, for example.The text was updated successfully, but these errors were encountered: