ID:2896844
 
Redundant
Applies to:DM Language
Status: Redundant

This feature has already been implemented, or is already achievable with existing methods.
There are some optimizations in filtered loops that you can't match with soft code, in particular loops over the world, or view, and some other such special "lists". However the filter of a for loop can only be specified at compile time, some usecases need to be able to filter what they loop over at runtime, like say mob ai looking for various objects around them. It would be a good optimization if we could make use of the faster filtered for loops in these cases instead of having to write our own filter checks inside unfiltered loops.

So instead of
var/search_type = /mob/tasty_critter
//[...]
for(var/atom/thing in view())
if(!istype(thing, search_type))
continue
//[do stuff to found target]


You'd write something like
var/search_type = /mob/tasty_critter
//[...]
for(var/atom/thing as search_type in view())
//[do stuff to found target]


Or another usecase that would be nice would be filtering for multiple types at once.
var/search_types = list(/mob/tasty_critter, /obj/tasty_snack)
//[...]
for(var/atom/thing as search_types in view())
//[do stuff to found target]
Lummox JR resolved issue (Redundant)
SS13 already does this. They found that "as anything" turns off the implicit istype() call for the loop, allowing them to use their own in its place or even skip it if the list contents are guaranteed to be the right type.
I think you might've misunderstood the feature request?

I believe they're asking for a syntax where you can do `as arbitrary_variable_containing_a_typepath` in a loop, instead of the compile-time form.

These wouldn't be for lists where everything is the same time.
Imagine a list of nearby objects where you want to just filter by things that the player chooses. You would do (pseudocode):
var/obj/favorite_food = input() in list(/obj/apple, /obj/orange)
for (var/obj/thing as favorite_food in view())

What Zewaka said, this is for when you *want* the for loop to use the implicit istype but with a type specified during runtime.
At the syntax "as (SOMETHING)", SOMETHING only can accept anything, atom, obj, somewhat hardcoded things. You can't use other inputs.
In response to EvilDragonfiend
EvilDragonfiend wrote:
You can't use other inputs.

My friend, this is the feature requests subforum.
In response to Zewaka
Zewaka wrote:
EvilDragonfiend wrote:
You can't use other inputs.

My friend, this is the feature requests subforum.

No, I am telling the current behaviour that it appears Lummmox misunderstood regarding this post, not telling him to fix this.
The reason this is redundant is that you can use "as anything" in the loop, then manually add an istype() against the var you're using to store a type path. It would behave no differently than a regular loop with a fixed type which adds an implicit istype().

If the request is merely for syntactical sugar, I don't see it as too practical. It doesn't solve a common need or even much improve on existing syntax.
ah, yes. Thanks for the clarification.
there is an assumption (that is probably true in some cases) that byond will be faster at filtering without needing to dip into user code every item.

(there is also the feature where byond optimizes certain typed loops like mobs in contents or mobs|obj|turfs|areas in world that would be nice to generalize but the current solution is switch trees which serves that usecase fully if not the most pretty way)