Why doesn't this compile in 1.22?

BlitzMax Forums/BlitzMax Programming/Why doesn't this compile in 1.22?

Grey Alien(Posted 2006) [#1]
This used to compile in 1.20:
		If x < w2 Then x = w2
		ElseIf x > screenwidth - w2 Then x = screenwidth - w2


I get "error: expecting Expecting Expression but encountered ElseIf"

I can fix this easily enough by padding it out and using EndIfs but I wondered why it used to compile and now it doesn't...Was I using ElseIf incorrectly?

Here's how I can get it to work:
		If x < w2 Then
			x = w2
		Else
			If x > screenwidth - w2 Then x = screenwidth - w2
		EndIf


Thanks


Grey Alien(Posted 2006) [#2]
Oh I get it, it's all supposed to be on one line:
		If x < w2 Then x = w2 ElseIf x > screenwidth - w2 Then x = screenwidth - w2

weird...


Damien Sturdy(Posted 2006) [#3]
What's an ElseIf?

ElseIf = "Yucky" :)

The second version of your code reads much more cleanly - Perhaps mark removed Elseif? What was it doing there in the first place?


Grey Alien(Posted 2006) [#4]
Yeah I always used to write the inflated version of the code but got fed up with it and wondered if I could use shortcuts...


Grey Alien(Posted 2006) [#5]
For example, imagine how big this gets padded out:
		If Image.midhandled Then
			If x < 0-w2 Then Return 1
			ElseIf x > screenwidth + w2 Then Return 1
			If y < 0-h2 Then Return 1
			ElseIf y > screenheight + h2 Then Return 1
			Return 0 'OK		
		Else
			If x < 0-w Then Return 1
			ElseIf x > screenwidth Then Return 1
			If y < 0-h Then Return 1
			ElseIf y > screenheight Then Return 1
			Return 0 'OK
		EndIf



Azathoth(Posted 2006) [#6]
ElseIf is still supported.


Grey Alien(Posted 2006) [#7]
yeah it's in the docs.


Grey Alien(Posted 2006) [#8]
here's another one, how am I supposed to write this?!:

		If x <= 0-w2 Then del=1
		ElseIf x>=ScreenWidth+w2 Then del = 1	
		ElseIf y <= 0-h2 Then del = 1
		ElseIf y>=ScreenHeight+h2 Then del =1


If padded out and using EndIf, it would have 3(4?) levels of nesting.


Grey Alien(Posted 2006) [#9]
OK, I can write the above like this, and it compiles:
		If x <= 0-w2
			del=1
		ElseIf x>=ScreenWidth+w2
			del = 1	
		ElseIf y <= 0-h2
			del = 1
		ElseIf y>=ScreenHeight+h2
			del =1
		EndIf


This is a lot better than tons of if nesting.


fredborg(Posted 2006) [#10]
Of course that could be compacted to be more effecient, and in my opinion readable.

For instance this section:
			If x < 0-w2 Then Return 1
			ElseIf x > screenwidth + w2 Then Return 1
			If y < 0-h2 Then Return 1
			ElseIf y > screenheight + h2 Then Return 1
			Return 0 'OK	

Could be:
Return (x<-w2) Or (x>screenwidth+w2) Or (y<-h2) Or (y>screenheight+h2)

And even better would probably be to replace it with a RectsOverlap (or similar) function, if you use it several times.


Grey Alien(Posted 2006) [#11]
hmm, yes I should have done it like that. I often use Or like that so I wonder why I didn't here? It's like a fallback to my spectrum days...of course the way I wrote it is the method the compiled code will use to run it.


Dreamora(Posted 2006) [#12]
the breaking of lines has always worked like this, hasn't it?
If you break up if and elseif on different lines, without connecting the lines with .., you needed to finish the block with endif.

Strict

Global x:Int, y:Int
Global w2:Int, h2:Int
Global screenwidth:Int, screenheight:Int

Print test()

Function test:Int()
	If x < 0-w2 Then Return 1 ..
	ElseIf x > screenwidth + w2 Then Return 1 ..
	If y < 0-h2 Then Return 1 ..
	ElseIf y > screenheight + h2 Then Return 1
	Return 0 'OK	
End Function



Grey Alien(Posted 2006) [#13]
everything I posted above worked in V1.20 but not in 1.22.

So it seems I need .. now, but didn't before. Thanx!


Smurftra(Posted 2006) [#14]
you can use a select case if you have lots of elseifs

SELECT TRUE
CASE x <= 0-w2
del=1
CASE x>=ScreenWidth+w2
del = 1
CASE y <= 0-h2
del = 1
CASE y>=ScreenHeight+h2
del =1
END SELECT


Mark Tiffany(Posted 2006) [#15]
And you can always revert to your old style by using the line continuation feature on each line (..)


Fabian.(Posted 2006) [#16]
It's sad that so many things which worked with the versions before don't work with V122; the problem you posted isn't the only one; this is very sad, because most of the things which have be crossed out, could stay just as well.


Dreamora(Posted 2006) [#17]
Not needfully. the parser had some nasty errors before, some basing on empty lines etc.


Grey Alien(Posted 2006) [#18]
Smurftra: yeah thanks. I normally use case if I end up having more than 2 ifs, but I recently got into else if but wasn't using the .. line continuation thing, which 1.22 now insists on. Annoyingly this broke my framework, such a simple thing. So I've fixed it an a new release is on the way...


marksibly(Posted 2006) [#19]
   If x < w2 Then x = w2
   ElseIf x > screenwidth - w2 Then x = screenwidth - w2

Woah! The above used to be quite dangerous...

A bug in older versions meant 'ElseIf' could be used in place of 'If' in some cases. Therefore, the above actually meant:
   If x < w2 Then x = w2
   If x > screenwidth - w2 Then x = screenwidth - w2

No biggy in this case, but could have had serious implication in others.


Chris C(Posted 2006) [#20]
jeeze parsing source files can be a tangled web...


Grey Alien(Posted 2006) [#21]
@marksibly. Yeah I found that "If" bug myself once in V1.20 when my code didn't act as expected but thought I'd just formatted it incorrectly, so used this structure to fix it:

If condition
  do something
elseif
  do something
endif


and that worked.

which seemed to be OK.


H&K(Posted 2006) [#22]
I hate Then.

When Im reading people code I always wonder if it was an entry into a smallest number of lines of code compition.
I tend to always put If's condition and its action on different lines.


Smurftra(Posted 2006) [#23]
So do i, but i still use Then for readability

If x>0 then
do something
else
do something else
end if


H&K(Posted 2006) [#24]
Maybe but
If x>0
    Do something
else
    Do something Else
endif
is just as readable


Dreamora(Posted 2006) [#25]
I don't use then at all unless I programm in LUA.
There is no need for it, it just will take care that my tab based assignement intend is even further right so others definitely can't read it anymore. (won't happen to me on my 1680x1050 most likely ^^)

if isInRect      HandleCollision(someSprite)


Works and is clean.
But I would never use it for more than a single command that fits right next to it as it wouldn't be clean anymore in that case.


dmaz(Posted 2006) [#26]
IMHO:I think the best use of then is when the code is on the same line. I think the Separation makes it easier to read... well for sure when your editor highlights "then" as a keyword.

if x > 0 then DoSomething()
if x > 0 then DoSomething() else DoSomethingElse()	'but not an elseif on the same line


if x > 0
	DoMainThing()
elseif x = 0
	DoSomethingElse()
else
	DoOther()
endif


I only use the "or" way if I don't have many conditions (3 max but usually 2) AND I know for sure I won't be adding anything to any of the statement blocks.


Grey Alien(Posted 2006) [#27]
I've been using Then since I was 8 so it's just habit. Now adays I'm training myself to only use Then on a single line for separation and not bothering with Then in multi-line ifs.


Smurftra(Posted 2006) [#28]
Like grey alien, its a long-time habit, so to me code looks 'weird' when there is no then.


popcade(Posted 2006) [#29]
I think Grey is fixing the game framework now, hope it can get finished soon.


Grey Alien(Posted 2006) [#30]
yeah it's fixed already but I need to update all the docs before I release it, unless you want a prerelease with now docs...


Smurftra(Posted 2006) [#31]
Weird, i compiled my game that uses your framework just fine...


Grey Alien(Posted 2006) [#32]
that is weird, what in 1.22? The problem is with ElseIf in CommonTypes in TSprite.


Smurftra(Posted 2006) [#33]
I'm using Swift's sprite engin so that must be it


Grey Alien(Posted 2006) [#34]
maybe...


Blitzplotter(Posted 2006) [#35]
I got the elseif hiccup here... I should really consider merging my app (think it 'may be based on 0.99) with v1.01, anyone got any suggestions about a good merge tool ?


Grey Alien(Posted 2006) [#36]
anyway the fix is to put .. on the end of the If line that comes before the elseif and if there's another elseif, stick .. on that line too.

As for a merge tool, hmm maybe a decent text editor would show you differences and let you decide what to keep, but I don't know any more.

As for my framework, replace the core files of CommonCode and CommonTypes and the elseif probs should go away and it shouldn't affect your game at all unless you hacked my framework files at all...


Blitzplotter(Posted 2006) [#37]
Cheers Grey, I initially resolved the elseif issue by dropping else of the front - not as efficient but it worked.

I've reached a point where I really need to stop 'prototyping' (Errmm.. hacking to achieve my aims..) and work in my development with the use of functions and the like.

My BMax development to date (with the assisatance of grey's framework and Assari's top tips) has been good - but I now need to polish my code....


Grey Alien(Posted 2006) [#38]
Well good look with it, it's always a process of continual improvement.