code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(69 lines)

Structs

In Tomo, you can define your own structs, which hold members with arbitrary types that can be accessed by fields:

struct Foo(name:Text, age:Int)
...
my_foo := Foo("Bob", age=10)
assert my_foo == Foo(name="Bob", age=10)
assert my_foo.name == "Bob"

Structs are value types and comparisons on them operate on the member values one after the other.

Namespaces

Structs can define their own methods that can be called with a : or different values that are stored on the type itself.

struct Foo(name:Text, age:Int)
    oldest := Foo("Methuselah", 969)

    func greet(f:Foo)
        say("Hi my name is $f.name and I am $f.age years old!")

    func get_older(f:@Foo)
        f.age += 1
...
my_foo := @Foo("Alice", 28)
my_foo.greet()
my_foo.get_older()

Method calls work when the first argument is the struct type or a pointer to the struct type.

Secret Values

If you want to prevent accidental leaking of sensitive information, you can create a struct with the secret flag turned on, which causes the struct to be converted to text without showing any of its contents:

struct Password(raw_password_text:Text; secret)
struct User(username:Text, password:Password)
...
user := User("Stanley", Password("Swordfish"))
assert user == User("Stanley", Password("Swordfish"))
assert "You are: $user" == 'You are: User(username="Stanley", password=Password(...))'

Designing APIs so they take secrecy-protected structs instead of raw data values is a great way to prevent accidentally leaking sensitive information in your logs! Secrecy-protected values still work the same as any other struct, they just don't divulge their contents when converting to strings:

assert user.password == Password("Swordfish")

You can also access the fields directly, but hopefully this extra amount of friction reduces the chances of accidentally divulging sensitive content:

assert user.password.raw_password_text == "Swordfish"
1 # Structs
3 In Tomo, you can define your own structs, which hold members with arbitrary
4 types that can be accessed by fields:
6 ```tomo
7 struct Foo(name:Text, age:Int)
8 ...
9 my_foo := Foo("Bob", age=10)
10 assert my_foo == Foo(name="Bob", age=10)
11 assert my_foo.name == "Bob"
12 ```
14 Structs are value types and comparisons on them operate on the member values
15 one after the other.
17 ## Namespaces
19 Structs can define their own methods that can be called with a `:` or different
20 values that are stored on the type itself.
22 ```tomo
23 struct Foo(name:Text, age:Int)
24 oldest := Foo("Methuselah", 969)
26 func greet(f:Foo)
27 say("Hi my name is $f.name and I am $f.age years old!")
29 func get_older(f:@Foo)
30 f.age += 1
31 ...
32 my_foo := @Foo("Alice", 28)
33 my_foo.greet()
34 my_foo.get_older()
35 ```
37 Method calls work when the first argument is the struct type or a pointer to
38 the struct type.
40 ## Secret Values
42 If you want to prevent accidental leaking of sensitive information, you can
43 create a struct with the `secret` flag turned on, which causes the struct to
44 be converted to text without showing any of its contents:
46 ```tomo
47 struct Password(raw_password_text:Text; secret)
48 struct User(username:Text, password:Password)
49 ...
50 user := User("Stanley", Password("Swordfish"))
51 assert user == User("Stanley", Password("Swordfish"))
52 assert "You are: $user" == 'You are: User(username="Stanley", password=Password(...))'
53 ```
55 Designing APIs so they take secrecy-protected structs instead of raw data
56 values is a great way to prevent accidentally leaking sensitive information in
57 your logs! Secrecy-protected values still work the same as any other struct,
58 they just don't divulge their contents when converting to strings:
60 ```tomo
61 assert user.password == Password("Swordfish")
62 ```
64 You can also access the fields directly, but hopefully this extra amount of
65 friction reduces the chances of accidentally divulging sensitive content:
67 ```tomo
68 assert user.password.raw_password_text == "Swordfish"
69 ```