DEV Community

Cover image for Comp Language Struct
Peter Shinners
Peter Shinners

Posted on

Comp Language Struct

An ongoing series of my thoughts and ideas for my Comp programming language.

Comp provides a single container type. It's a flexible container that combines both ordered and named fields in a unique style. A struct literal is defined by a series of fields in side of curly braces. Named and unnamed fields can be mixed freely, while always remaining ordered. Field values can be mixed and matched with any types. All data in Comp is immutable, including these structures.

One way to think of this is similar to how Python arguments are defined for a function. Some arguments are positional and some arguments are named. When combined with Comp's shape values we get defaults and flexible morphing (casts). This makes Comp's structs work like Python arguments, but you don't need to invoke a function to assemble them.

Some examples likely make this clearer.

{"cat" 12+12 true}
{name="Joe" score=14.5}
{warning message="it did not work" timestamp=now}
{ {"car" year = 2021} {"bike" year = 2025} }
Enter fullscreen mode Exit fullscreen mode

Remember, Comp's syntax uses no separators and whitespace is arbitrary. These statements could be split over multiple lines, joined, or justified in whatever style felt appropriate.

Fields are accessed either with dotted field name or positionally using a special index field. All fields can be referenced by index, which works for both named and unnamed fields. And the first index starts at 0, this does not allow "negative indexing" from the back of the container.

user.active.#0
vehicles.#1.year
config.server-details.port
Enter fullscreen mode Exit fullscreen mode

Typical field names are simple text tokens, which support kebab-case. But any value can be used as a field name, including numbers, tags, and other structures. These "value" field named are wrapped in single quotes and allow any value or expression. Double quotes can be used as text when the field name isn't a valid token. These same rules are used for defining fields and referencing fields. There are few other "advanced field lookup" syntaxes but these ones go a long way.

{'15'=10+5  "Server Port"=12345}

data.'15'
data."Server Port"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)