rust-hosted-langs / book

Writing Interpreters in Rust: a Guide
https://rust-hosted-langs.github.io/book/
Creative Commons Attribution 4.0 International
493 stars 30 forks source link

Object model #8

Open yorickpeterse opened 6 years ago

yorickpeterse commented 6 years ago

We need a chapter that outlines the object model the VM will use, what the trade-offs are, etc, etc.

madmalik commented 6 years ago

I'll try to sum up a few different models. Please correct me where i'm wrong ;)

All of these approaches are not big on information hiding.

Also:

In general i'd like to see inheritance de-emphasised or completely left out. Another aspect that we can discuss is the addition of a trait system. There is a little bit of overlap with the described record approach that allows open method definition.

yorickpeterse commented 6 years ago

@madmalik

no type safety of any kind, since all objects have the same type (the flipside of the mentioned flexibility)

I disagree, type safety has little to do with prototype OO. See TypeScript for example, and Inko uses prototype OO and the language offers type safety (through gradual typing).

One of the big benefits of prototype OO is that it's super easy to model other object systems (class based OO for example) on top of it, while the inverse is not necessarily true. I personally also found it a bit easier to wrap my head around.

madmalik commented 6 years ago

@YorickPeterse

I disagree, type safety has little to do with prototype OO

Your are correct, i worded that wrong. What i meant is that the structure is more... amorphous? But I'll take a look into how typescript handles that (and of course Inko ;) ).

yorickpeterse commented 6 years ago

@madmalik Yeah, the underlying data structures are definitely less specialised. For example, for Inko it's (more or less) just this:

struct Object {
  prototype: *mut Object,
  attributes: Box<HashMap<*mut Object, *mut Object>>,
  value: ObjectValue
}

enum ObjectValue {
  Integer(i64),
  Float(f64),
  String(Box<String>),
  ...
}