Page 1 of 2
Default Variable Assignment
Posted: Mon Jun 15, 2020 11:53 am
by David Barker
I've decided to post this on the forum as it's a new feature of the compiler that I know a number of users have requested in the past. From 2.2.3.3 onward you can now declare and initialise variables at the same time. Some examples include:
Code: Select all
// Strings...
Include "string.bas"
Dim message As String = "hello" + " " + "world"
Dim strLen As Byte = Length("12345")
// Arrays...
dim myArray() as byte = (1,2,3,4,5)
// Other Variables...
Const
a = 10,
b = 20,
c = 30
Dim value As Word = a * (b + c) - 10
As shown in the string length example above, you can have function calls in your initialisation code. For example:
Code: Select all
Function multiply(a,b As Byte) As Word
result = a * b
End Function
Dim value As Word = multiply(2,6)
Most of the time, it's likely to be simple but useful stuff like:
Code: Select all
dim index as byte = 0
dim loopRange as byte = 100
and so on...
Re: Default Variable Assignment
Posted: Mon Jun 15, 2020 12:09 pm
by bitfogav
This is a great addition to the compiler, It will save having to search through the code to find the default values..
Thank you for adding this David.
Re: Default Variable Assignment
Posted: Mon Jun 15, 2020 2:22 pm
by Jerry Messina
...you can have function calls in your initialisation code.
Cool!
Re: Default Variable Assignment
Posted: Mon Jun 15, 2020 3:54 pm
by bitfogav
Sorry for my question, but I am just trying to understand how does the new compiler deal with variable assignments now for example the code below that was declared at the start of the prog without any assignment?.
If the variable "x" has not been initialised, the compiler used to show up a warning message, but this doesn't seem to be the case anymore.
So I was just wondering what the compiler will set the default variable too?.
Re: Default Variable Assignment
Posted: Mon Jun 15, 2020 4:09 pm
by Jerry Messina
gives me "variable x might not have been initialized"
If you don't define an initializer then the variable doesn't get assigned anything, just as before.
Until you use 'x' on the RHS of an expression it doesn't matter.
Re: Default Variable Assignment
Posted: Tue Jun 16, 2020 7:13 am
by David Barker
Just to be clear
will generate a hint "x is never used". As Jerry points out, it's not an issue unless you use "x" on the right hand side of an expression.
will generate a warning that "x has not been initialised" and
will generate the same error as above (x not initialised)
The hint and warning mechanism has not changed between versions. If you are not seeing hints or messages in similar use cases, you should really post an example bit of code...
Re: Default Variable Assignment
Posted: Tue Jun 16, 2020 12:29 pm
by Jerry Messina
... and just to be REALLY clear, my use of "RHS" was NOT how the Urban Dictionary defines it!
https://www.urbandictionary.com/define.php?term=RHS
Re: Default Variable Assignment
Posted: Tue Jun 16, 2020 12:49 pm
by bitfogav
haha Jerry...
David Barker wrote:The hint and warning mechanism has not changed between versions. If you are not seeing hints or messages in similar use cases, you should really post an example bit of code...
Thanks for your replies guys. I think it was just my mistake and I was having a moment!
I was doing something like the following and thinking how does the compiler know what "x" is
Code: Select all
dim x as byte
while true
inc(x)
wend
Re: Default Variable Assignment
Posted: Tue Jun 16, 2020 1:08 pm
by David Barker
It's a fair question. In the example you have given x could be anything. It most likely state would be "0" *but* then again it might not. It's easy to miss an initialisation in the code. But the warning should catch them...
> ... and just to be REALLY clear, my use of "RHS" was NOT how the Urban Dictionary defines it!
Well I never...and how did you discover that new meaning exactly
Re: Default Variable Assignment
Posted: Tue Jun 16, 2020 1:08 pm
by octal
@David, Jerry
Looks like Firewing is highly influencing SF future
bitfogav wrote: ↑Tue Jun 16, 2020 12:49 pm
I was doing something like the following and thinking how does the compiler know what "x" is
The compiler simply looks at the pixels forming X letter on the screen and check if the matrix matches with predefined const arrays values. So to avoid any problem with the future updates of the compiler, just make sure you use always the same colors when coding on your computer.
Re: Default Variable Assignment
Posted: Tue Jun 16, 2020 1:22 pm
by bitfogav
octal wrote:just make sure you use always the same colors when coding on your computer.
Ok Ok.. good tip Octal...
- colorssf.png (9.62 KiB) Viewed 7840 times
Re: Default Variable Assignment
Posted: Tue Jun 16, 2020 1:50 pm
by Jerry Messina
@octal - good to see you made it back. Guess you cleared up your browser issue, whatever that was.
@David - Well I never...and how did you discover that new meaning exactly
I just happened to use that there "Oogle" thingamabob for "RHS" and that was one of the first few that popped up.
Never a dull moment when asking Mr Internet for answers, that's for sure!
Re: Default Variable Assignment
Posted: Mon Jun 22, 2020 1:40 am
by bitfogav
I am trying to assign the following, is this supported with the new variable assignment? they all show a compiler error ", expected".
OR
OR
Re: Default Variable Assignment
Posted: Mon Jun 22, 2020 6:49 am
by David Barker
Damn. I will get that fixed...
Re: Default Variable Assignment
Posted: Mon Jun 22, 2020 4:26 pm
by David Barker
The problem has been fixed. We will post an update very soon. Note that your second case (boolean = 1) will not work. A boolean can only be true or false. Apologies again for this faux pas...