Allow strings on the right side of an IS expression.
gerzytet opened this issue ยท 7 comments
The current implementation of IS requires the right side to be an imported class.
If we were to also allow simple class names, it would have 2 benefits:
- The code becomes much more concise without the imports.
- Dynamically determining which type to test for is possible.
Manual importing is still useful for cases where the simple class name is ambiguous.
Probably we can use Map to map the input String with the appropriate class (just like how the abbreviation works for custom Trigger events)
@gerzytet
I get what you mean now
I was thinking something like something IS "Player"
, which is not likely possible since we cannot infer the class from just its name
but allowing the IS statement to accept String will indeed increase the flexibility
something IS "Player"
is exactly what I meant.
We don't know exactly what class "Player" is, but we can just match any class that has the name of "Player", which will work almost every time.
@wysohn
wouldn't that mean we would have to add support manually for every class?
I want this to work for any object and any class. We just use reflection to get all the superclasses of the object and test if any of them match
@gerzytet
Hmm okay but finding the class wouldn't be the easy work
Don't you think it's better to manually create the Map instead?
It's probably something to do with ClassLoader, yet I haven't tried it myself
@wysohn
"Player" is not treated as a class. It's the name of a class.
If someone were to create their own Player class, it would also pass something IS "Player"
We're not doing a type match, we're doing a class name match.
A IS "B"
Step 1: get a complete set of all the superclasses of A
(including A.getClass
()) with https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getSuperclass-- and https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getInterfaces--
Step 2: for each class, check if the simple name is "B", if so, return true.
Step 3: if no "B" class is found, return false
It's not precise, but in almost all cases, the class names are unique enough that this isn't a problem.
@gerzytet
So we are focusing on the left hand side instead
Clever idea