ToString

ToStringExpr( value )
ToString( value )
ToString( value )
ToString( value )
to_string( value )
ToString( value )

Description

The ToString function converts a value to a string type, if possible.

Attempting to convert a value to a string which has no string representation results in an "invalid argument" error.

Parameters

Argument Type Definition and Requirements

value

Any

The value to attempt to convert to a string type.

Returns

A string type of value.

Examples

The query below executes an array of independent ToString operations and returns the results in an array. The result array position matches the execution array position. The first operation converts a floating point literal to a string. The second operation takes an integer literal and converts it to a string. The third operation converts a date to a string.

Value result = await client.Query(
  Arr(
    ToStringExpr(1234.5678),
    ToStringExpr(true),
    ToStringExpr(Date("2018-06-06")),
    ToStringExpr(null),
    ToStringExpr(Time("2015-02-20T06:30:00Z"))
  )
);
Arr(StringV(1234.5678), StringV(true), StringV(2018-06-06), StringV(null), StringV(2015-02-20T06:30:00Z))
result, err := client.Query(
  f.Arr(
    f.ToString(1234.5678),
    f.ToString(true),
    f.ToString(f.Date("2018-06-06")),
    f.ToString(nil),
    f.ToString(f.Null()),
    f.ToString(f.Time("2015-02-20T06:30:00Z"))))

if (err != nil) {
  fmt.Println(err)
} else {
  fmt.Println(result)
}
[1234.5678 true 2018-06-06 null null 2015-02-20T06:30:00Z]
System.out.println(
        client.query(Arr(
                ToString(Value(1234.5678)),
                ToString(Value(true)),
                ToString(Date("2018-06-06")),
                ToString(null),
                ToString(Time("2015-02-20T06:30:00Z"))
        )).get());
["1234.5678", "true", "2018-06-06", "null", "2015-02-20T06:30:00Z"]
client.query([
  q.ToString(1234.5678),
  q.ToString(true),
  q.ToString(q.Date('2018-06-06')),
  q.ToString(null),
  q.ToString(q.Time('2015-02-20T06:30:00Z')),
])
.then((ret) => console.log(ret))
[ '1234.5678', 'true', '2018-06-06', 'null', '2015-02-20T06:30:00Z' ]
print(client.query([
  q.to_string(1234.5678),
  q.to_string(True),
  q.to_string(q.date('2018-06-06')),
  q.to_string(None),
  q.to_string(q.time('2015-02-20T06:30:00Z')),
]))
['1234.5678', 'true', '2018-06-06', 'null', '2015-02-20T06:30:00Z']
println(Await.result(
  client.query(
    Arr(
      ToString(1234.5678),
      ToString(true),
      ToString(Date("2018-06-06")),
      ToString(Null),
      ToString(Time("2015-02-20T06:30:00Z"))
    )
  ),
  5.seconds
))
["1234.5678", "true", "2018-06-06", "null", "2015-02-20T06:30:00Z"]

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!