10 · Strings
Validated nominal string literals
A literal such as Email\"[email protected]\" is admitted only when the real validated string newtype's predicate accepts its static contents at compile time. Dynamic strings still use the fallible constructor. This syntax also preserves existing callable tagged templates.
On this page
A literal such as Email"[email protected]" is admitted only when the real validated string newtype's predicate accepts its static contents at compile time. Dynamic strings still use the fallible constructor. This syntax also preserves existing callable tagged templates.
Complete example
Validated nominal string literals.
nominal-string-literals.zolo
// expect: [email protected]
// expect: true
// expect: empty
// expect: [email protected]
// expect: callable
const required = "@"
fn valid_email(value: str) -> bool = value.contains(required)
newtype Email(str) where |value| valid_email(value)
newtype Empty(str) where |value| value == ""
let email = Email"[email protected]"
print(email.unwrap())
let input = "invalid"
print(Email.new(input).is_err())
let empty = Empty""
print(if empty.unwrap() == "" { "empty" } else { "wrong" })
mod address { pub newtype Address(str) where |value| value.contains("@") }
use address::{Address as Mail}
print(Mail"[email protected]".unwrap())
fn callable() -> str { let Email = |parts: [str]| parts[0]; Email"callable" }
print(callable())
Rules and behavior
Validation, schemas and units · Documentation
From a checkout of the repository:
zolo examples/features/nominal-string-literals.zolo
See also