[Cs-dev] (buggy?) variable initialization with if-then
Date | 2013-05-01 11:46 |
From | Ben Hackbarth |
Subject | [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
hello! in the csd pasted below, an i-rate variable is initialized in an if-then block and then printed. however, in the case of this example, the variable isn't initialized since the conditional does not evaluate true. strangely, csound (5 and 6) does not complain, and assigns the variable a value of zero. if seems to me that either the user shouldn't be allowed to initialize variables inside conditionals OR csound should exit with an error in this case. i much prefer the latter.thanks! -- ben
<CsoundSynthesizer> <CsOptions> -n </CsOptions> <CsInstruments> sr = 44100 ksmps = 32 nchnls = 2 0dbfs = 1 instr 1 iBool = 1 if (iBool == 0) then iTest = 20 else iOther = 10 endif print iTest endin </CsInstruments> <CsScore> i 1 0 1 e </CsScore> </CsoundSynthesizer> |
Date | 2013-05-01 12:30 |
From | Victor Lazzarini |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
The variable is not initialised at any point. The code iTest = 0 (assignment at i-time, not initialisation), just does not run if iBool is 0. The variable just holds whatever garbage it contains. You can see this by running the following code: |
Date | 2013-05-01 12:39 |
From | Ben Hackbarth |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
i see. is this seen as a feature of csound coding? if not, why not throw an error similar to those thrown when a user asks for a variable that does not exist? it seems to me only a point of confusion. in my experience it makes programming/debugging lots of if-then blocks difficult as one might have variables of the same name in different conditional blocks and then accidentally use previously stored values. at least, i am running into those kinds of problems at the moment. -- ben
On Wed, May 1, 2013 at 1:30 PM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote: The variable is not initialised at any point. The code iTest = 0 (assignment at i-time, not initialisation), just does not run if iBool is 0. The |
Date | 2013-05-01 13:09 |
From | Victor Lazzarini |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
I guess there are two issues here, one that is kind of semantic/syntactical and the other to do with backward compatibility. The first thing is that the parser will not know in all cases whether a variable is set or not. It will only know if a constant is used (as in your case). There is no way currently to tell at run time whether a variable has been set or not, before we use it (as far as I know at least). Secondly, if we implement something that will change this behaviour, we might break existing code and that is a no-go zone. (This is my opinion, of course. I might be wrong about it breaking existing code) Victor On 1 May 2013, at 12:39, Ben Hackbarth wrote:
Dr Victor Lazzarini Senior Lecturer Dept. of Music NUI Maynooth Ireland tel.: +353 1 708 3545 Victor dot Lazzarini AT nuim dot ie |
Date | 2013-05-01 13:33 |
From | Victor Lazzarini |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
Note that this is the case in other languages as well. In C: #include <stdio.h> int main(){ int a, b, c; scanf("%d", &a); if(a) b = 10; else c = 0; printf("%d\n", c); return 0; } the compiler will not tell you that c can be used uninitialized, even if you compile this with -Wuninitialized. Victor On 1 May 2013, at 13:09, Victor Lazzarini wrote:
Dr Victor Lazzarini Senior Lecturer Dept. of Music NUI Maynooth Ireland tel.: +353 1 708 3545 Victor dot Lazzarini AT nuim dot ie |
Date | 2013-05-01 13:39 |
From | Victor Lazzarini |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
In performance, however, both branches seem to be initialised, but it still does not reveal the bug. On 1 May 2013, at 13:33, Victor Lazzarini wrote:
Dr Victor Lazzarini Senior Lecturer Dept. of Music NUI Maynooth Ireland tel.: +353 1 708 3545 Victor dot Lazzarini AT nuim dot ie |
Date | 2013-05-01 21:23 |
From | Ben Hackbarth |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
agreed, but there are other languages where this is not the case, like in python: b=1 since, afaik, you cannot create variables in csound without also assigning their value (unlike C), i had assumed that asking for a variable that was not explicitly inited should result in an error. i did not think that retrieving garbage was even possible. thanks victor. -- ben
On Wed, May 1, 2013 at 2:39 PM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote:
|
Date | 2013-05-01 21:41 |
From | Victor Lazzarini |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
That is because of the interpreted nature of Python. Csound does not work like that, because the compilation and run times are separate. On 1 May 2013, at 21:23, Ben Hackbarth wrote:
Dr Victor Lazzarini Senior Lecturer Dept. of Music NUI Maynooth Ireland tel.: +353 1 708 3545 Victor dot Lazzarini AT nuim dot ie |
Date | 2013-05-01 21:51 |
From | Justin Smith |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
This is as much an issue of semantics as the practicality of implementation. ML family languages have a separate compile stage and will complain if something is referenced in a context where it could potentially have not been initialized (because of their scoping rules - if a variable was created in the context of an if branch, every usage of that variable would need to happen inside that same branch). On Wed, May 1, 2013 at 1:41 PM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote:
|
Date | 2013-05-01 21:54 |
From | Steven Yi |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Hi Ben, There's a bit to consider in looking at the issue. First, Csound has two scopes, global or local. There are no block-level scopes. To understand how this could have happened, I think we need to look at Csound before if-then's. In that case, when there is only if-goto's, there is only a flat list and no syntax blocks. When if-then was introduced, I don't think there was attention given to scope variables, just a matter of using if-then as syntax sugar for if-goto. Other languages like python/c/c++/java/etc. all end up having variables scoped by blocks. They have issues with name shadowing, but this is all explained up front in learning these languages. The way I interpret what you're saying regarding things being an error is that you expected there to be variable scoping and there isn't at the moment. I think it makes sense to have scopes. On the other hand, older pieces may break due to requiring an additional variable initialization outside of the blocks to get it to be scoped. Also, because variable definition and usage are one in the same with Csound, you couldn't do shadowing of vars (for better or for worse). steven On Wed, May 1, 2013 at 9:23 PM, Ben Hackbarth |
Date | 2013-05-01 22:29 |
From | Ben Hackbarth |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Attachments | None None |
hi steven, when i say that i _expect_ an error, what i mean is that i _want_ an error. as i rarely write good code the first time, i am counting on csound's complaints to correct my mistakes. the lack of errors makes it much harder to program efficiently.thanks for your comments -- you're correct in that the scoping of variables is precisely the issue. -- ben
On Wed, May 1, 2013 at 10:54 PM, Steven Yi <stevenyi@gmail.com> wrote: Hi Ben, |
Date | 2013-05-08 23:46 |
From | Steven Yi |
Subject | Re: [Cs-dev] (buggy?) variable initialization with if-then |
Hi Ben, Just following up on this email, I think we'll have to file looking into variable scoping and blocks post-6.0. It's a large enough change that I don't think it can be scheduled in. At least for the time being, we're following the same semantics as is found in 5.x and are thus backwards compatible. I'm guessing that the only possible solution moving forward is to adopt a backwards compatibility flag, some kind of compiler pragma, or somewhere in-between. Ideally, by default we could turn on scoped vars, and have the option to turn it off if running older projects. If it's a real legacy project that just isn't going to get updated, there'll be the possibility to run it. Otherwise the scoped behavior could be on and things may break, but at least it should be easy enough to update. Having scoped vars does promote better programming practices I think in this scenario. Other than that, I don't really see another way around it. Thanks! steven On Wed, May 1, 2013 at 10:29 PM, Ben Hackbarth |