string
func string(x: string): stringstring returns the string x holds: x is a string or a named primitive over string, which it unwraps. It does not format other values; String.from does.
Details
stringdoes not format or convert other kinds. A number, abool, ornullis a compile error. Use String.from to render any value as text, and String.toInt to read a number from text.- A named primitive type over
stringis not astring. Library functions such as String.length needstring(x)to use its value.
Basic examples
Unwrap a value of a named primitive type over string:
type UserId = string
let id = UserId("u-1")
id // UserId("u-1")
string(id) // "u-1"On a plain string, string is the identity:
string("héllo") // "héllo"
string("") // ""Scope
Unwrap before calling a String function, directly or through .:
type Email = string
let e = Email("Ann@Example.com")
String.toLower(string(e)) // "ann@example.com"
string(e).length() // 15string is a value, so it can be passed to a function. It unwraps every element:
type Tag = string
let tags = [Tag("ant"), Tag("docs")]
tags.map(string) // ["ant", "docs"]In a pipe:
type Email = string
let e = Email("Ann@Example.com")
e | string() | String.toLower() // "ann@example.com"The keys of a map:
type Slug = string
let pages = Map{ Slug("home"): 1, Slug("about"): 2 }
pages | Map.keys() | Array.map(string) | Array.sort() // ["about", "home"]Applications
Build a URL path from a tagged identifier:
type UserId = string
func profileUrl(id: UserId): string {
"/users/" + string(id)
}
println(profileUrl(UserId("u-42")))/users/u-42Join tagged values into one line of text:
type Tag = string
let tags = [Tag("go"), Tag("ant"), Tag("docs")]
println(tags | Array.map(string) | String.join(", "))go, ant, docsProperties and relations
string gives back the payload a constructor wrapped, and the identity on a plain string:
type Name = string
for s in ["", "Ann", "日本"] {
assert(string(Name(s)) == s)
assert(string(s) == s)
}len unwraps on its own, so it agrees with String.length of the payload:
type Code = string
let c = Code("ß-1")
assert(len(c) == String.length(string(c)))Possible issues
string does not render a number. That is a compile error; use String.from or interpolation:
let s = string(42)
// error: string converts only a string or a named primitive type over string, got intString.from(42) // "42"
"${42}" // "42"string does not unwrap a named primitive type over another kind:
type Count = int
let s = string(Count(3))
// error: string converts only a string or a named primitive type over string, got CountA tagged value is not accepted where a string is expected. Unwrap it first:
type UserId = string
let id = UserId("u-1")
let n = String.length(id) // error: cannot use UserId where string is expectedIts own namespace has no String functions, so . does not reach them either:
type UserId = string
let id = UserId("u-1")
let n = id.length() // error: UserId has no field or function lengthA tagged value is not equal to a plain string, and comparing them is a compile error:
type UserId = string
let same = UserId("u-1") == "u-1"
// error: UserId and string cannot be compared with ==: neither is assignable to the otherInterpolation and String.from render a tagged value with its tag. Unwrap it for the bare text:
type UserId = string
let id = UserId("u-1")
"${id}" // "UserId(\"u-1\")"
"${string(id)}" // "u-1"Markup does not take a named primitive type inside {...}. Unwrap it:
type UserId = string
let id = UserId("u-1")
let h = <p>{id}</p>
// error: markup takes a string, a number, void, Html, or an array of them, got UserId; convert it, as {string(x)}type UserId = string
let id = UserId("u-1")
println(<p>User {string(id)}</p>)<p>User u-1</p>A nullable string must be narrowed first, with ?? or a null check:
let maybe: string | null = null
let s = string(maybe)
// error: string converts only a string or a named primitive type over string, got string | nulllet maybe: string | null = null
maybe ?? "" // ""See also
bool, int, len, String.from, String.toInt, string type, String literals, type
