nicklockwood / Expression

A cross-platform Swift library for evaluating mathematical expressions at runtime
MIT License
824 stars 50 forks source link

Equality of strings, substrings and characters #27

Open nighthawk opened 4 years ago

nighthawk commented 4 years ago

Intuitively I'd expect 'Test'[0] == 'T' to return true but it evaluates to false.

In equalArgs this lands in this case:

            case let (lhs as AnyHashable, rhs as AnyHashable):
                return lhs == rhs

This is due to rhs is a String while lhs is a Character.

This could be fixed by adding:

            case let (lhs as _String, rhs as _String):
                return lhs.substring == rhs.substring

However, this makes me wonder if all the other AnyHashable cases should also get _Substring variants. Alternatively, the _String cases in arrayEvaluator could be changed to always return a String, but that could lead to inefficiencies.

Any suggestions on how best to address this? Or does this work as expected?

nicklockwood commented 4 years ago

@nighthawk you're right, this is a bug. I don't have a lot of time to work on this right now, but if you'd be willing to open a PR, I'll merge it.