Raised This Month: $51 Target: $400
 12% 

Switch error: expected token: ":", but found ".."


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Pawn 3-pg
Senior Member
Join Date: Jul 2009
Old 04-11-2010 , 15:53   Switch error: expected token: ":", but found ".."
Reply With Quote #1

I'm unable to get this code from the Pawn Tutorial to compile:

PHP Code:
//Example of an range switch statement
switch (a)
{
    case 
.. 50:
    {
       
//This code will run if 0 <= a <= 50
    
}
 
    case 
51 .. 100:
    {
       
//This code will run if 51 <= a <= 100
    
}
 
    case 
101 .. 200:
    {
       
//This code will run if 101 <= a <= 200
    
}
 
    default:
    {
       
//This code will run if all other cases fail
    
}

Code:
// switch_test.sp(8) : error 001: expected token: ":", but found ".."
// switch_test.sp(8) : warning 215: expression has no effect
// switch_test.sp(8) : error 001: expected token: ";", but found ":"
Is the tutorial wrong?
Pawn 3-pg is offline
The JCS
AlliedModders Donor
Join Date: Jan 2007
Location: Brazil
Old 04-11-2010 , 16:08   Re: Switch error: expected token: ":", but found ".."
Reply With Quote #2

Sourcemod uses a modified version of the Pawn language, give a look on this wiki section:
http://wiki.alliedmods.net/Category:SourceMod_Scripting

Or go to the switch explanation directly.
The JCS is offline
Send a message via MSN to The JCS
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 04-11-2010 , 23:11   Re: Switch error: expected token: ":", but found ".."
Reply With Quote #3

Since SourcePawn is based on Pawn, I wonder why we can't use ranges in switch statements then. It would be really useful in some cases.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)
rhelgeby is offline
Send a message via MSN to rhelgeby
BAILOPAN
Join Date: Jan 2004
Old 04-12-2010 , 00:05   Re: Switch error: expected token: ":", but found ".."
Reply With Quote #4

rhelgeby: We removed it because the implementation is extremely bad. It essentially expands it to a series of "if" statements, and this can mislead people into doing something like "0..100" whereas this would be much better:

Code:
if (value >= 0 && value <= 100)

Since the ".." syntax would result in:
Code:
if (value == 0 || value == 1 || value == 2 || value == 3 || value == 4 || value == 5) ...

(We could re-optimize this at run-time, but it seems like the right idea is to use if statements anyway.)
__________________
egg
BAILOPAN is offline
API
Veteran Member
Join Date: May 2006
Old 04-12-2010 , 12:24   Re: Switch error: expected token: ":", but found ".."
Reply With Quote #5

I agree about using an if statement over a range, but why not do something like this?
case 123: // Since no upper/lower bound is defined: if(a==123)
case 123 .. 456: // Lower and upper bound found: if(a<=upperbound || a>=lowerbound)
__________________
API is offline
Send a message via AIM to API
BAILOPAN
Join Date: Jan 2004
Old 04-13-2010 , 03:51   Re: Switch error: expected token: ":", but found ".."
Reply With Quote #6

That's not what the compiler was generating, and we opted to remove the syntax feature rather than fix the code generation.

We could always add it back, but it's kind of a non-trivial change to fix the problem, and extremely low priority.
__________________
egg
BAILOPAN is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:57.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode