Skip to content

Latest commit

 

History

History
53 lines (33 loc) · 1.66 KB

coercions.md

File metadata and controls

53 lines (33 loc) · 1.66 KB

Coercions

[TOC]

This chapter describes implicit coercions that Soy performs.

Some Soy operations will take an expression of a given type and coerce it to another type. This page will describe how these operations behave and when they trigger.

TODO(lukes): this page is insufficiently exhaustive, but it is a start

String coercions {#string}

There are a few ways to cause an implicit string coercion in Soy.

While every value in Soy can be coerced to a string, not every value has a useful string representation, or a string representation that is consistent in all the backends.

number, string, bool, safe string types

These mostly do what you expect, but for numbers consider using a number formatting library to make I18n appropriate text.

list, record, map, proto, null, ve, ve_data

These mostly do not do what you expect. Avoid doing this other than for debugging purposes.

Boolean coercions

There are a few ways to coerce a value to a boolean.

All values have a boolean coercion (sometimes referred to as a 'truthiness' check), these mostly follow JavaScript semantics:

  • '0' values are falsy, e.g. null, "", 0
  • all other values are truthy

NOTE: there are some inconsistencies in how these work across backends. For example, in Python, the empty list, empty record, and empty maps are all currently falsy (b/19271140).