3 ms·
I was writing a reply that got deleted (browser refreshed on me when I left the computer alone too long), so I'm going to focus on the second part because I'm n
by Jtsummers 5y ago
I was writing a reply that got deleted (browser refreshed on me when I left the computer alone too long), so I'm going to focus on the second part because I'm not rewriting it all. Also, know that I'm new to MOP programming (which is part of how I came across this paper) so my example is high level, but (as far as I presently understand) correct, just not detailed.
So for my particular example, the difference would be (in my phrasing) automation. If someone took the time to implement everything, then you could do something like this:
(defclass protected-class (standard-class))
(defmethod initialize-instance :after ((class protected-class) ...)
...) ;; adds accessor logic with locks
(defclass my-protected-class ()
((a-number :protected)) ;; maybe not explicit, but for here it is
(:metaclass protected-class))
This would be more like (but not quite the same as) decorators in Python. In C# with computed properties you could do something like (been a few years, treat as pseudocode):
class MyProtectedClass {
private int _a_number;
public int ANumber {
get => _a_number;
set {
lock;
_a_number = value;
unlock;
}
}
}
Ok, so it's protected. But how do you do this without manually inserting this locking logic throughout? (Again, been a bit for C#, it has some good reflection libraries so you probably actually can do this similar to CL, but it won't be as straightforward or accessible to the programmers using the language). In Java (also been a few years, so don't treat as current best practice), you used to add synchronized to methods and get the automatic locking (why I did :protected above, to keep it in line with Java). But note that this is something the Java creators added, there was no way (within Java) for you to extend the language to do this yourself (or no easy way, again their was reflection capabilities from the start).
And it's important to remember, I meant that as just an example. There are other, conceivable, characteristics you may want to add for some reason (hopefully a good reason). With a MOP-ish facility, you can extend your language to do these things, but without it you can't (or can't easily, consider C where there is zero reflection so you have to have an extra-language facility to provide extensions).
- brundolf 5y agoWould JS Proxies be a better comparison? These are similar to get/set, but they let you work dynamically over any given property key: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... i.e. you could write a single get/set pair that works for every property on an object, intercepting operations and doing arbitrary logic, including passing values on to the default behavior if you want to
- Jtsummers 5y agoI'm not the most proficient JS programmer, but that looks like a more apt comparison, yes.