Bit of a question:

As a bit of a quarantine project I'm working my way through the book "Writing an interpreter in go" but I'm writing it in rust. (Stops me copy-pasteing / skimming)

His AST data structure is based on interfaces that have standard properties and methods. I decided to use structs for this, but structs can only have methods.

Whats the best way to deal with this in rust? Is there a way to define standard properties in a type? Or should I use a mix of enums and traits?

Follow

The suggestions about using an enum type for Token and returning it are probably the best option.

If I was writing it I would only have one trait for the default AST node, and specific node types would just be structs that implement the node trait plus their own methods.

@ashafer

> one trait for the default AST node, and specific node types would just be structs that implement the node trait plus their own methods.

Yes, I think I'll go down this route, see how it goes.

Sign in to participate in the conversation
Fosstodon

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.