mapbox-gl-js version: 0.42.2
Use this code:
layout: {"text-field": ["coalesce", ["get", "fieldA"], ["get", "fieldB"]]}
where fieldA is null and fieldB is not null
Display fieldB.
Empty label. (It works though when fieldA is not null, displaying fieldA.)
A work-around with case
did the job. However this seems to be a bug. Sorry if I somehow misunderstood the Style Spec and it isn't!
Thanks for the report @muesliq! Could you by any chance provide a live jsfiddle example reproducing this issue?
This is an outcome of type inference and automatic type assertions. What's happening is that since text-field
must be a string, the result of coalesce
must be a string, so it's inferred that each of the two get
s must also be a string. So there is a string
assertion automatically inserted, as if you had written:
["coalesce", ["string", ["get", "fieldA"]], ["string", ["get", "fieldB"]]]
Then when the expression is evaluated, it's found that the first get
produces null
rather than a string, which causes an error and aborts the evaluation. (The visual result is that you get the default value of the property, an empty string.)
This is _arguably_ working as intended, but I think the behavior is so surprising that we will need to revisit the design.
Nice analysis @jfirebaugh.
I think the best fix here is to treat coalesce
as a special case when performing inference, not inserting assertions for its arguments, and instead inserting an assertion around the whole coalesce expression where necessary.
Most helpful comment
Nice analysis @jfirebaugh.
I think the best fix here is to treat
coalesce
as a special case when performing inference, not inserting assertions for its arguments, and instead inserting an assertion around the whole coalesce expression where necessary.