10 · Strings
Validated nominal string literals
A literal such as Email\"ana@empresa.com\" 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"ana@empresa.com" 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: ana@empresa.com
// expect: true
// expect: empty
// expect: alias@empresa.com
// 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"ana@empresa.com"
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"alias@empresa.com".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