CIP2016-07-07 Type conversion functions
Author: Mats Rydberg <email@example.com>
Cypher contains three type conversion functions:
toFloat(). This CIP suggests introducing a more comprehensive rule for specifying the existence of these functions, following a simple
Xis the name of the type. Additionally, this CIP suggests expanding this rule to encompass all scalar types.
toInt() function indicates conversion to
Int, which is neither a type nor any other known Cypher construct.
In many programming languages,
int is used as a short form of
integer, but we cannot rely on this being known to any Cypher developer.
Following Cypher’s guiding principles, explicit is better than implicit, and this CIP argues that
toInteger() is a more explicit and better name.
Furthermore, there is an opportunity to create a very simple and consistent rule that all scalar types should come with a type conversion function on the pattern
X is the name of the type.
Refer to the type system CIP to learn more about Cypher scalar types.
This has the consequence of adding a
toBoolean() function to the language, creating a full set of conversion functions.
In concrete terms, this CIP suggests two things:
toInt()should be renamed to
toBoolean()should be added to the language.
Apart from adding a new function and changing the name of a function, no syntactical changes are suggested.
toInteger() function would behave exactly the way
toBoolean() function would only accept the two strings
'false', with any case variants, as valid inputs, and would return
null on any other input, similarly to how
toInt() behave at the time of writing.
UNWIND ['true', 'FALSE', 'foo'] AS s RETURN toBoolean(s) // yields two booleans and one null
WITH toString(true) as boolString // lower-cased 'true' RETURN toBoolean(boolString)
2.4. Interaction with existing features
With the addition of
toBoolean(), it is now possible to do a round-trip conversion of booleans to strings (using
toString()) and back again.
3. Benefits to this proposal
A simple, general rule for type conversion functions for all scalar types.
Removes possible confusion where
integermay be misinterpreted for different things.
Addition of a cheap, useful function with functionality that could be expected given the other type conversion functions.