Paths and Files
Tomo supports a built-in syntax for file and directory paths, with some logic to help prevent or mitigate the risks of errors caused by string manipulations of file paths. Tomo does not have a built-in datatype to represent files specifically, but instead relies on Paths as the API to do filesystem operations.
Syntax
Paths are domain-specific languages that have their own dedicated
syntax. A path literal begins with either /, ., or ~ and continues until
an unescaped space or the end of the line.
path := /tmp/foo
path_with_spaces := ./file\ name.txt
Paths also respect parenthesis balancing, so a closing parenthesis is not considered part of the path unless a corresponding opening parenthesis is also part of the path, which makes it easier to use paths in many cases:
paths := [(./foo), (./file(1).txt)]
contents := (./baz.txt).read()!
In the first example, the paths are parsed as ./foo and ./file(1).txt, not
./foo), and ./file(1).txt)]. Similarly, in the second example, the path is
./baz.txt, not ./baz.txt).read()!.
Usage
Paths come with a bunch of methods for doing filesystem operations. Broadly, they fall into three categories:
- File reading and writing, e.g.
(./foo.txt).write("Hello world")! - Filesystem traversal and metadata, e.g.
(/some/dir).children()! - Path manipulation, e.g.
(./file.jpg).extension()
Operations that involve the filesystem will typically return a Result type,
which is either Success or Failure(reason). In the common case where you
expect a file operation to succeed and do not want to explicitly account for
the possibility of a file operation failing, you can use ! as a suffix as a
shorthand for "if this returns Failure(reason), then fail and print the
reason."
Internal Representation
Paths are internally represented as C-style NUL-terminated char strings. This
makes it easy to use them with C APIs without any conversion. Paths do not
perform any unicode normalization, unlike Text.
API
1 # Paths and Files3 Tomo supports a built-in syntax for file and directory paths, with some logic4 to help prevent or mitigate the risks of errors caused by string manipulations5 of file paths. Tomo does not have a built-in datatype to represent files6 specifically, but instead relies on Paths as the API to do filesystem7 operations.9 ## Syntax13 an unescaped space or the end of the line.16 path := /tmp/foo17 path_with_spaces := ./file\ name.txt18 ```20 Paths also respect parenthesis balancing, so a closing parenthesis is not21 considered part of the path unless a corresponding opening parenthesis is also22 part of the path, which makes it easier to use paths in many cases:25 paths := [(./foo), (./file(1).txt)]26 contents := (./baz.txt).read()!27 ```33 ## Usage35 Paths come with a bunch of methods for doing filesystem operations. Broadly,36 they fall into three categories:44 expect a file operation to succeed and do not want to explicitly account for47 reason."49 ## Internal Representation51 Paths are internally represented as C-style NUL-terminated char strings. This55 # API57 [API documentation](../api/paths.md)