Pyro Gets Object Literals

31st July 2024

I've added support for object literals to Pyro, my hobby programming language. It's a feature inspired by Go — a language I've been using a lot at work over the past couple of years.

Go is a language that's grown on me the more I've used it. I didn't like it at first, and I still have some reservations about the design choices it makes, but the more I've used it, the more I've come to appreciate its philosophy.

One feature I've always liked is its "object literal" syntax. Here's how it works in Pyro.

Say we have a class definition like this:

class Person {
    pub var name;
    pub var age;
}

We can create an instance of this class using standard object-initialization syntax and assign values to its fields using standard field-assignment syntax, e.g.

var person = Person();
person.name = "Dave";
person.age = 42;

Or, we can create an instance of the class directly using object-literal syntax, e.g.

var person = Person as {
    name: "Dave",
    age: 42,
};

This initialization style works particularly well for what C++ aficionados call POD or "plain old data" classes.

Fields which aren't assigned a value in the object literal get their default value, e.g.

var person = Person as {
    name: "Dave",
};

assert person.name == "Dave";
assert person.age == null;

Here are the rules for object literals: