Why does this compile?

BlitzMax Forums/BlitzMax Programming/Why does this compile?

Czar Flavius(Posted 2011) [#1]
Members is a list. Look at the highlighted line. It compiled just fine and caused a hidden bug!

	Method _add_unit:TLink(unit:TUnitObject)
		_number_units :+ 1
		Assert Not _members.Contains(unit)
		Local link:TLink _members.AddLast(unit) ****
		Local num = get_number_units()
		If _formation.get_number_units() < num
			_formation.set(num)
		End If
		_formation.populate(_members)
		Return link
	End Method



Jesse(Posted 2011) [#2]
shouldn't that line have an equal right after the link declaration?
it looks like it's returning a null Tlink.

Last edited 2011


Czar Flavius(Posted 2011) [#3]
Yes it should, but it didn't and it compiled so I did not detect the problem >.>


Jesse(Posted 2011) [#4]
I don't know if you knew that you don't need to have a semicolon to separate two completeley different instructions on a single line. in most cases the semicolon is optional. in your original example it's the same as having this:
Local link:Tlink
_members.Addlast(unit)


you can actually write a whole program in a single line of code with out any semicolons.


Oddball(Posted 2011) [#5]
I'd expect the compiler to bug out for that too. The semicolon should be compulsory, especially when using SuperStrict. This is a bug if you ask me. Although knowing Mark's lax views on code formatting it might be intentional.


Czar Flavius(Posted 2011) [#6]
The semi-colon should definately be compulsory when there are multiple statements on the same line.


GfK(Posted 2011) [#7]
Its not a bug, its how it works. Its no different to, for example:
Local a:Int = 1
If a = 1 Print "Hello"
...the likes of which I've seen on this forum countless times but nobody complains about that being unexpected behaviour. Forcing folks to format their code in a specific way is a really bad idea (although personally, I wouldn't write code formatted like this in a fit, but plenty do).


ziggy(Posted 2011) [#8]
I would say it is a sort of "implementation detail" of the compiler. The documentation says you have to use a semi colon to put 2 sentences in the same line. The compiler does not complain, ok, but this does not mean this is supported or recommended.


Oddball(Posted 2011) [#9]
GfK wrote:
...the likes of which I've seen on this forum countless times but nobody complains about that being unexpected behaviour. Forcing folks to format their code in a specific way is a really bad idea (although personally, I wouldn't write code formatted like this in a fit, but plenty do).
Absolutely agree that you should be able to choose sloppy coding if you wish to, but that is the whole point of having optional Strict and SuperStirct modes. I still say it should fail in SuperStrict at the very least.


Fry Crayola(Posted 2011) [#10]
Gfk:
Its not a bug, its how it works. Its no different to, for example:

Local a:Int = 1
If a = 1 Print "Hello"

...the likes of which I've seen on this forum countless times but nobody complains about that being unexpected behaviour.


Well, there's a subtle difference there, in that keeping all the code on one line in an if statement tells the compiler not to look for an End If in order to exit the condition.

For such a statement, a carriage return makes a world of difference to the compiler.


Czar Flavius(Posted 2011) [#11]
That's ok because the Then keyword in an if statement is optional, and you expect an if statement to have two things (condition, expression). You can tell where one begins and the other ends. I don't know of any real-world use for the kind of statement that I wrote by accident.