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?

@george_ why not simply adding a method `fn token(&self) -> &Token;` to your trait?

@musicmatze

Yeah, is this the more standard rust way to do things? and I'd have to impl it every time?

Just seems a bit verbose to do

struct LetStatement {
token: Token
}

impl Statement for LetStatement {
fn token(&self) => {
self.token
}
}

But I guess it works

@george_ yeah, I guess that's the standard way... not sure though.

The trait members feature is not yet there I guess...

Follow

@musicmatze

Ah, found the RFC:

github.com/rust-lang/rfcs/pull

Looks like nothing has happened on it since 2017.

I'll go for your suggestion and see how I do.

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.