Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically type-annotate arguments to "==", "!=" expressions #5835

Closed
peterbodo opened this issue Dec 10, 2017 · 2 comments
Closed

Automatically type-annotate arguments to "==", "!=" expressions #5835

peterbodo opened this issue Dec 10, 2017 · 2 comments

Comments

@peterbodo
Copy link

peterbodo commented Dec 10, 2017

Motivation

After 2 hours of experimentation, I relaised that a simple tag filter looks like this:
["all",["has","amenity"], ["==", ["string",["get", "amenity"]], "toilets"]]

because I have to ensure first that amenity exists, than making its value string, in order to avoid type errors.
This should not be so difficult, and definitely people will not love expresssions, if this very common use case is so difficult to achieve.

Design Alternatives

["==","amenity","toilets"]
I think, this should work out of the box. Because properties is a special object, so I suggest a direct access, and various type issues should not be handled in the forntend code.

(Why on Earth, "get","something" is a Value, when it is in fact a "String"??? :) )

I could get along with:
["==",["get","amenity"],"toilets"]

Design

no idea

Mock-Up

no idea

Concepts

no idea

Implementation

no idea

@peterbodo peterbodo changed the title Make type conversions automatic Make type conversions automatic in expressions Dec 10, 2017
@anandthakker
Copy link
Contributor

Type inference is performed automatically in most cases, but currently, == and != are an exception. I agree that requiring explicit type annotations for == / != is not ideal. (See also: #5761 (comment))

As an immediate option to simplify your expression a bit, you could use, ["==", ["to-string",["get", "amenity"]], "toilets"]. What this does is uses to-string to convert amenity to a string (no matter what its type happens to be in the actual data). Since to-string can accept null values, this approach means you don't have to do the [has,...] check first.

Why on Earth, "get","something" is a Value, when it is in fact a "String"?

get must be typed value, because at "compile" time (i.e., when we parse the expression), we cannot know ahead of time what the feature data will actually look like.

@anandthakker anandthakker changed the title Make type conversions automatic in expressions Automatically type-annotate arguments to "==", "!=" expressions Dec 11, 2017
anandthakker pushed a commit that referenced this issue Dec 11, 2017
Closes #5761
Closes #5835

The overloads of == and != are now effectively:
 - `(T1: Comparable, T2: Comparable) => boolean { T1 == T2 }`
 - `(Comparable, value) => boolean`
 - `(value, Comparable) => boolean`

Where `Comparable = string | number | boolean | null`.
@peterbodo
Copy link
Author

@anandthakker Thanks, That looks much better.
Maybe I should dig more into expressions.
Is there a more detailed documentation/examples for common use cases somewhere? I could not find any.

anandthakker added a commit that referenced this issue Dec 12, 2017
* Improve typing for ==, != expressions

Closes #5761
Closes #5835

The overloads of == and != are now effectively:
 - `(T1: Comparable, T2: Comparable) => boolean { T1 == T2 }`
 - `(Comparable, value) => boolean`
 - `(value, Comparable) => boolean`

Where `Comparable = string | number | boolean | null`.

* Simplify comparability check

* Add internal docs, fix possibleValues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants