Why doesn't this compile in 1.22?
BlitzMax Forums/BlitzMax Programming/Why doesn't this compile in 1.22?
| ||
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 |
| ||
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... |
| ||
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? |
| ||
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... |
| ||
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 |
| ||
ElseIf is still supported. |
| ||
yeah it's in the docs. |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
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 |
| ||
everything I posted above worked in V1.20 but not in 1.22. So it seems I need .. now, but didn't before. Thanx! |
| ||
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 |
| ||
And you can always revert to your old style by using the line continuation feature on each line (..) |
| ||
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. |
| ||
Not needfully. the parser had some nasty errors before, some basing on empty lines etc. |
| ||
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... |
| ||
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. |
| ||
jeeze parsing source files can be a tangled web... |
| ||
@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. |
| ||
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. |
| ||
So do i, but i still use Then for readability If x>0 then do something else do something else end if |
| ||
Maybe butIf x>0 Do something else Do something Else endifis just as readable |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
Like grey alien, its a long-time habit, so to me code looks 'weird' when there is no then. |
| ||
I think Grey is fixing the game framework now, hope it can get finished soon. |
| ||
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... |
| ||
Weird, i compiled my game that uses your framework just fine... |
| ||
that is weird, what in 1.22? The problem is with ElseIf in CommonTypes in TSprite. |
| ||
I'm using Swift's sprite engin so that must be it |
| ||
maybe... |
| ||
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 ? |
| ||
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... |
| ||
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.... |
| ||
Well good look with it, it's always a process of continual improvement. |