ID:2267853
 
(See the best response by Kaiochao.)
I'm trying to turn a lengthy if-else if chain into a faster switch, but it's all if(istype(thing, path), is there a way to incorporate that into a switch?

My original idea would be to just do switch(istype(thing)), but I am unsure if that'd work at all?

What exactly are you doing? Something like this?
if(istype(thing, typeA))
...
else if(istype(thing, typeB))
...
else if(istype(thing, typeC))
...
...

A switch() won't help here because you're evaluating multiple separate expressions, not comparing a single expression to multiple possible (constant) values.

If "thing" is "src", then you should be using proc overriding instead. Otherwise, you should consider if overriding something is more appropriate anyway. DM is an object-oriented programming language.
I was hoping more in the sense of this:
switch(istype(thing))
if(Type1)
if(Type2)
if(Type3)
etc


But I was never really sure if it is even possible
In response to Laser50
Best response
Your snippet doesn't make sense, but I kinda get what you're thinking. What you wrote is already valid in DM because istype(thing) returns true or false, and you're attempting to match that true or false value with a number of types. That's not what you wanted, though.

There are other programming languages out there that let you do this kind of "pattern-matching":
switch(thing)
if(var/typeA/a)
// do stuff if istype(thing, typeA)
if(var/typeB/b)
// do stuff if istype(thing, typeB)
if(var/typeC/c)
// do stuff if istype(thing, typeC)

However, DM doesn't support that kind of syntax.
switch(thing.type)
if("/mob/Player")
//do thing
if("/obj/Whatever")
//do thing
if("/obj/Whatever2")
//do thing

Would this accomplish what you want?
In response to Gunbuddy13
It might work in some cases, but, in general, it's a very limiting approach.

You wouldn't be able to check if something is of a child type of the types you're checking in the switch() conditions. So, you wouldn't be able to reuse this code when you eventually have child types, without adding those types to the switch().

In object-oriented programming, you should always be able to use an instance of a child type where an instance of a parent type is expected. This is known as the Liskov Substitution Principle. I think the rest of SOLID are pretty handy to know, too.

Also, type paths are literal values, not strings:
switch(thing.type)
if(/mob/Player)
// do thing
if(/obj/Whatever)
// do thing
if(/obj/Whatever2)
// do thing
<dm>
I used
switch(istype(thing))
if(pathtothing1)
//do stuff
if(pathtothing2)

etc. I think it worked. And since the code in that instance only accepted 1 input I think it worked.
In response to Laser50
As I said in my first post, since istype(thing) is a proc that returns true or false, and you're comparing that true or false value to paths, none of the conditions are going to happen.

If it helps, this is what you're doing:
var result = istype(thing) // 1 or 0
if(result == /some/path1)
// do stuff
else if(result == /some/path2)
// do stuff
else if(result == /some/path3)
// do stuff

...which is clearly never going to work.

Compared to what you wanted to do:
if(istype(thing, /some/path1))
// do stuff
else if(istype(thing, /some/path2))
// do stuff
else if(istype(thing, /some/path3))
// do stuff

Nothing about the switch() syntax allows you to cause the if() conditions to be inserted as an argument to some function call. That just isn't how it works. The expression in the parentheses that comes after "switch" is evaluated before the switch() can do anything.
Wouldn't it return true or false if the path is correct?
In response to Laser50
I'm not sure what you mean.

istype(thing) is a call to the istype() proc, with the reference held by the variable "thing" as the first argument. Since the second argument isn't provided, it's assumed to be the declared type of the variable "thing". So, if "thing" is declared like "var/obj/thing", then istype(thing) is short for istype(thing, /obj).

If "thing" does actually refer to an obj when istype() is called, then istype(thing) returns TRUE.

switch() is a control structure like if() that lets you shorten an if/else if/else chain like this:
if(x == a)
// ...
else if(x == b)
// ...
else if(x == c || x == d)
// ...
else if(x in y to z)
// ...
else
// ...

to this:
switch(x)
if(a)
// ...
if(b)
// ...
if(c, d)
// ...
if(y to z)
// ...
else
// ...

Therefore, your ideal switch(istype(thing)) is, in reality, just comparing the result of istype(thing) (TRUE or FALSE) to a bunch of paths.

It's pretty obvious that this condition wouldn't succeed:
if(TRUE == /obj/poop)

So, what are you actually thinking?