ID:158383
 
Is there a possible way to do the above?

Currently what I do is
switch(Var) if(Num1,Num2), which works fine. I just wanted to know if there was a way to do it just in an if() statement alone without typing if(X==Num1||X==Num2)
If I'm checking for two or more values of X in a single if() statement that make it true, I use a list.

if(X in list(Num1,Num2))
No, there isn't a way to do what you're wanting. You could do the list method already described, but it should be much faster to do regular comparisons.

I think the switch() syntax was inspired by the C-based switch() design. Consider:
   switch(n)
{
case 1:
case 2:
// stuff if n==1 or n==2
case 3:
case 5:
// stuff if n==1 or n==2 or n==3 or n==5
break;
case 9:
case 12:
// stuff if n==9 or n==12
break;
default:
// if n equals anything else
}


In those languages, there's a fall-through mechanism that's shown above. To many beginners, this is often a common error-point: you forget a break statement, and unexpected results occur.

My guess is, when DM was being designed, a way of avoiding this common pitfall was desired while still allowing switch() to have some sort of similar mechanism and the ability to specify a default case. You can't really fall through like the above as far as I'm aware (for the case where n==1||n==2, since it would fall through and also do the stuff for n==3||n==5), but you can do stuff like the following:
   switch(n)
if(1,2)
// stuff
if(3,5)
// other stuff
if(9,12)
// further stuff
else
// default case


So while all of the functionality of C-based switch() design wasn't retained, much of it was (and some functionality was added, such as the ability to use switch() on strings). The trade-off, then, is that DM doesn't require you to remember to break after each switch() case and, considering the typical audience DM reaches, that's probably a good thing.
If you really want to do it weird you could do this
if(!((X-Num1)&&(X-Num2)))

Or if num1 was 5 and num2 was 2

You could do this if(!((X-5)(X-2)))
and simplify it too
if(!(X*X-7x+10))


Although it probably won't have any gains depending on how byond handles things one of these might o.O ...

In response to Chowder
Chowder wrote:
If you really want to do it weird you could do this
if(!((X-Num1)&&(X-Num2)))

I admire your interest in helping, but you never seem to actually read the post fully before responding.

AJX wrote:
without typing if(X==Num1||X==Num2)

The response you supplied is in fact LONGER than the longer version I already listed that I didn't want to do.


To Kuraudo and MFC:
Thanks for the tips. As stated by Kuraudo I can't use the lists because they are too much slower than direct comparisons. Thanks anyway. :)
In response to AJX
Its pretty obvious that its longer just thought you wanted to know some alternatives... The only requirements you listed was it not being "if(X==Num1||X==Num2)" but being equivalent to it. I full-filled the requirement and posted another method. It being shorter or more convenient was not listed as a requirement. I did in fact read your post... Although it is debatable whether or not I misunderstood it.

Yep... o . o
Now if only I could put this kind of effort into one of my language art assignments. . .
Clearly what's really on your mind here is the amount of code and typing required, so I'm going to uncover the obvious to you and tell you to come up with a few #defines macros and/or procs to shorten the typing, f.ex.
proc/compare(val)
for(var/i=2,i <= args.len,i++)
if(val == args[i]) return 1

#define if_equals(a,b,c) switch(a) if(b,c)

mob/verb/check()
var/n = rand(1,12)
if(compare(n,1,2,4,8,10,12)) src << "even number"
if_equals(n,7,12) src << "typological number"

A proc is more convenient because DM's preprocessor is pretty limited, but since you're overoptimization-crazy you may opt for a macro.
(Or, solution number 2: just type it out.)
In response to Kaioken
That is exactly what is on my mind!

And I personally find it a bit easier to read.

I hadn't actually thought of using #define. That would work quite well. Thanks for the help.
In response to Kaioken
The for() loop would be good for use with a broad range of checks with varying numbers of arguments.
But I wonder/worry, if you had say, 30-40 arguments to check through, would it not take a while to perform compare?
In response to Saucepan Man
Not at all. All that proc does is perform equality checks; a very basic, simple and quick operation. 40 such checks would execute "instantly" - you wouldn't be able to tell the difference in time from 4. (To be specific, you wouldn't be able to tell the difference until the time to execute consisted of a tick or so, which would probably take like a million checks or so)