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

High level description of type system, assertions vs. conversion #5701

Merged
merged 1 commit into from Nov 17, 2017

Conversation

jfirebaugh
Copy link
Contributor

Per #5219, add documentation regarding the expression type system.

@jfirebaugh jfirebaugh force-pushed the expression-type-docs branch 2 times, most recently from 2f94ffd to aae567f Compare November 17, 2017 20:56
Copy link
Contributor

@anandthakker anandthakker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. One suggestion inline.

in certain situations, the SDK may be unable to automatically determine the expected
result type of a data expression from surrounding context. For example, it is not clear
whether the expression <code>["&lt;", ["get", "a"], ["get", "b"]]</code> is attempting
to compare strings or numbers. In situations like this, you must use one of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"must" could be misleading, since using a conversion rather than an assertion is also an option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

In situations like this, the simplest option is to use type assertions: ["<", ["number", ["get", "a"]], ["number", ["get", "b"]]]. A type assertion checks that the feature data actually matches the expected type of the data expression. If this check fails, it produces an error and causes the whole expression to fall back to the default value for the property being defined. The assertion operators are ...

Another option is to use a type conversion. Expressions only perform one kind of implicit type conversion...

This repeats the description of runtime type checking from the previous paragraph, but I think the redundancy is worthwhile in order to explicitly state what an assertion does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not convinced about this change. I think we should keep the use cases clearly separated here: conversion is for when you have one type, but need another. Assertion is for when the implementation needs you to tell it what type you have. If you are using a conversion, then you're not in a situation where the implementation is unable to automatically determine the type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using a conversion, then you're not in a situation where the implementation is unable to automatically determine the type.

You could be in such a situation -- e.g., say you want to do ["==", ["id"], ["get", "name"]]. Depending on your data, you really might want to use conversion rather than assertion as your way out of the type checking error.

That said, I'm 👍 for focusing on assertions as the primary way to deal with this, since, though conversions can be used in this way, their typical use cases are much broader. I still think the word "must" is misleading, and also that we should include an explicit description of assertions' behavior.

@jfirebaugh jfirebaugh merged commit 0b4f1cd into mb-pages Nov 17, 2017
@jfirebaugh jfirebaugh deleted the expression-type-docs branch November 17, 2017 22:37
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

Successfully merging this pull request may close these issues.

None yet

2 participants