Alexito's World

A world of coding 💻, by Alejandro Martinez

Improving Swift struct composition, inspired by Jai

Today I was reading Composing types in Swift which showcases the different ways of composing the different types in Swift: structs, classes and enums. Is a good read, specially if you are too deep in the OOP mindset.

While reading the part on structs composition I remembered something I learn watching Jonathan Blow's new language Jai (what a surprise! I'm talking again about Jai). Let's first recap what John Sundell (yeah, don't be confused with the names :P) tells us about composing structs in Swift.

The post compares a simple inheritance with classes, the usual OOP way:

class User {
    var name: String
    var age: Int
}

class Friend: User {
    var friendshipDate: Date
}

versus a composition of structs:

struct User {
    var name: String
    var age: Int
}

struct Friend {
    let user: User
    var friendshipDate: Date
}

The advantages are explained in the post, but one thing that is annoying about using this technique is that now the properties in User are not directly part of Friend, you need to access them though the user property.

friend.name // with class inheritance
friend.user.name // with struct composition :(

That makes sense and is what we're used to, but what if there was a better way of doing this? I learned this by watching Jai's development and it's a really great feature.

Introducing using

In Jai using is a keyword that brings the namespace of another type into the current scope. This can be used in functions, which is really nice to avoid repeatedly accessing through type hierarchies. But the best part is that you can also use it in types definitions!

See this snippet from a real code example of Jai:

Build for challenge

Imagine that we had using in Swift:

struct User {
    var name: String
    var age: Int
}

struct Friend {
    using let user: User // using IS THE NEW KEYWORD
    var friendshipDate: Date
}

By adding the using keyword in the property definition of user now the User properties are directly accessible from the Friend struct. Now you could write the same code as if structs had inheritance.

friend.name // with class inheritance
friend.name // with struct composition and using :D

I think this would be a really nice addition to the language allowing for better patterns with composition of value types. Maybe someday!

If you liked this article please consider supporting me