4 ms·
Pages 4 and 5 illustrate the central conceit: Instead of a set of fixed operations (like allocating a class instance or accessing properties/slots/fields of a c
by Jtsummers 5y ago
Pages 4 and 5 illustrate the central conceit: Instead of a set of fixed operations (like allocating a class instance or accessing properties/slots/fields of a class in a fixed way), you can reprogram how your classes behave (in CLOS, at both compile and runtime). By way of analogy, consider a GPU implementing a fixed-function pipeline versus having shader units. The former gets the job done, but only offers so many ways to control what gets rendered, the latter is far more capable in the longterm (at the cost of added complexity).
Trying to get this to fit into a comment so I'm being handwavy a bit.
Consider the Person class that they describe. Because it's sparse (not every field is populated), instead of having a large fixed size for all thousand fields (or however many) they change out the underlying representation to use a hash table. Now, anyone can represent their data using a map. However, just storing your data in a map doesn't let you continue to use the other language features that are built around classes and objects. In particular, take C++, if you stored your data in a map you'd lose this bit of handy syntax:
Person p = ...
p.age = ...
Well, if age is stored in a map inside the person class, there is no age field to assign to. With a MOP, however, you can still have this more compact data representation format (since the data is sparse), but still have all the normal language syntax and semantics around the object (like accessing fields using the dot syntax). In Common Lisp you might use (or any other slot accessor):
(with-slots (age height) person
...)
And the change in representation doesn't change how the slot values are accessed (both for reading and for writing).
Now, changing the underlying data representation is useful, but a relatively small thing. Consider something a bit more useful: Ada's protected objects. A protected object in Ada adds, basically, mutexes and condition variables via language syntax to control access to data shared across tasks (and other things, this is just the main utility of them). It's a special set of syntax added to provide this ability, in principle you can use MOP to extend CLOS to provide this behavior. Change your meta-class from standard-class to protected-class. Users still access objects (from their perspective) exactly like any other, but all the mutex and condition variable stuffs can be automatically baked in (no need to add extra logic in your methods).
Now, this does introduce potential runtime complexity which eats into performance. And that's addressed further into the linked paper. It's not totally insurmountable, but it does have to be considered (any time you add indirection or potential indirection you have to decide if it's worth the cost).
- brundolf 5y agoOn the surface, this: > With a MOP, however, you can still have this more compact data representation format (since the data is sparse), but still have all the normal language syntax and semantics around the object (like accessing fields using the dot syntax) just sounds like the JavaScript object model, and this: > Users still access objects (from their perspective) exactly like any other, but all the mutex and condition variable stuffs can be automatically baked in (no need to add extra logic in your methods). kinda sounds like computed properties/getters+setters (or simply "Properties" in C#), which many languages have at this point. I assume I'm missing or misunderstanding something?
- db48x 5y agoYes, you missed the point where you as the programmer can choose the semantics of the language as you go, rather than the language designer doing it ahead of time. Instead of having a language designer pick one spot in a large space of possible designs, the Metaobject Protocol gives the language user the ability to pick their own spot within that space. You can have sparse objects in the one project where they are needed, while not bothering in projects where they aren’t. You can have version controlled objects, or database–backed objects, or objects which implement inheritance differently. If you can think of it, you can probably do it.
- brundolf 5y agoThat still sounds like getters/setters, then (which you could use to implement properties-as-a-hash-map, for example). Maybe the ergonomics are different - we're still speaking pretty abstractly here so it's hard to tell - but it sounds like the same basic idea: class TimePeriod { private double _seconds; public double Hours { get { return _seconds / 3600; } set { if (value < 0 || value > 24) throw new ArgumentOutOfRangeException( $"{nameof(value)} must be between 0 and 24."); _seconds = value * 3600; } } }
- db48x 5y agoGetters and setters are nice, but you can’t use them to write a new solution to the diamond inheritance problem.