Default Variable Assignment

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

User avatar
David Barker
Swordfish Developer
Posts: 1180
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Default Variable Assignment

Post by David Barker » Mon Jun 15, 2020 11:53 am

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...

bitfogav
Registered User
Registered User
Posts: 148
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Mon Jun 15, 2020 12:09 pm

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. :D

Jerry Messina
Swordfish Developer
Posts: 1173
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Mon Jun 15, 2020 2:22 pm

...you can have function calls in your initialisation code.
Cool!

bitfogav
Registered User
Registered User
Posts: 148
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Mon Jun 15, 2020 3:54 pm

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?.

Code: Select all

Dim x As Byte
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?.

Jerry Messina
Swordfish Developer
Posts: 1173
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Mon Jun 15, 2020 4:09 pm

Code: Select all

dim x as byte
dim y as byte

y = x
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.

User avatar
David Barker
Swordfish Developer
Posts: 1180
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Default Variable Assignment

Post by David Barker » Tue Jun 16, 2020 7:13 am

Just to be clear

Code: Select all

dim x as byte
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.

Code: Select all

dim x as byte
dim y as byte
y = x
will generate a warning that "x has not been initialised" and

Code: Select all

dim x as byte
dim y as byte = x
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...

Jerry Messina
Swordfish Developer
Posts: 1173
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Tue Jun 16, 2020 12:29 pm

... 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

bitfogav
Registered User
Registered User
Posts: 148
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Tue Jun 16, 2020 12:49 pm

Jerry Messina wrote: ... 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
:lol: 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! :lol: I was doing something like the following and thinking how does the compiler know what "x" is :shock:

Code: Select all

dim x as byte
while true
   inc(x)
wend

User avatar
David Barker
Swordfish Developer
Posts: 1180
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Default Variable Assignment

Post by David Barker » Tue Jun 16, 2020 1:08 pm

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 :D

User avatar
octal
Registered User
Registered User
Posts: 581
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Re: Default Variable Assignment

Post by octal » Tue Jun 16, 2020 1:08 pm

@David, Jerry
Looks like Firewing is highly influencing SF future :P
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 :shock:
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.

bitfogav
Registered User
Registered User
Posts: 148
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Tue Jun 16, 2020 1:22 pm

octal wrote:just make sure you use always the same colors when coding on your computer.
Ok Ok.. good tip Octal... :lol:

colorssf.png
colorssf.png (9.62 KiB) Viewed 187 times

Jerry Messina
Swordfish Developer
Posts: 1173
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Tue Jun 16, 2020 1:50 pm

@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!

bitfogav
Registered User
Registered User
Posts: 148
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Mon Jun 22, 2020 1:40 am

I am trying to assign the following, is this supported with the new variable assignment? they all show a compiler error ", expected".

Code: Select all

dim SLEEP_ON As bit = 1
OR

Code: Select all

dim SLEEP_ON As Boolean = 1
OR

Code: Select all

dim SLEEP_OFF As Boolean = false

User avatar
David Barker
Swordfish Developer
Posts: 1180
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Default Variable Assignment

Post by David Barker » Mon Jun 22, 2020 6:49 am

Damn. I will get that fixed...

User avatar
David Barker
Swordfish Developer
Posts: 1180
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Default Variable Assignment

Post by David Barker » Mon Jun 22, 2020 4:26 pm

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...

Post Reply