Was recently looking into "annotations" in Self an...
# thinking-together
s
Was recently looking into "annotations" in Self and Java, as well as the related concepts of "property wrappers" and "function builders" in Swift. Are there other languages with similar features? Also, what are the most interesting uses of annotations you've run across?
m
decorators in python? https://peps.python.org/pep-0318/
πŸ‘ 1
a
Python decorators. I think Rust calls them annotations as well. Elixir via Erlang, apparently. Nim. I'm less sure about C# but I'd bet 5$ they have something like that too. They're pretty common these days.
m
πŸ‘€ 1
they already exist in typescript
i
And now to torpedo this thread β€” can someone explain to me how these are meaningfully different from lisp macros?
Or, on the other hand, higher order functions? Are decorators just... sort of like a HOF or macro, but applied in a specific way, and thus it's useful to introduce a new term for them to help us talk about this particular usage? Or are they truly a new and different and fully general sort of thing?
a
Well, to the extent you can even consider them as a group, considering the very wide variety in semantics, they're pretty sharply limited (by design) relative to proper macros. The others seem to be more complicated, but Python decorators are literally sugar for wrapping the advisor annotated item in a function call. I think Rust annotations are just tags that can be processed by some other build-time code.
k
@Ivan Reese Python decorators are just syntactic sugar for a particular use case of higher-order function. But since readability is an important criterion for Python programmers, and since Python does not have anything like Lisp macros, syntactic sugar can make a big difference.
πŸ‘ 1
☝🏽 1
☝️ 1
s
I'm more interested in the idea of an abstraction around slots (instance variable name and value) that manages meta data and computation related to it than a particular feature of this general area (such as decorators). Yes, we can implement decorators with macros etc, but that's just one small feature related to this area.
🍰 1
m
clojure supports metadata for any datastructure https://clojuredocs.org/clojure.core/with-meta
πŸ‘ 2
k
@Steve Dekorte Pharo Smalltalk has something like that, see https://hal.inria.fr/hal-00641716/document. Slots can be defined by classes, making them almost objects themselves. I have used this myself only once, to trace and record slot accesses. It’s surprisingly straightforward to use.
πŸ‘ 1
w
Rubyist checking in. At some point (probably a decade or two ago), the method definition construct was made to return the method name so that you can easily write your own decorators. This means that when you write
synchronize memoize def calculate(something); ...; end
, the interpreter: 1. defines the
calculate
method, 2. sends the symbol
:calculate
to the
memoize
class method, which will presumably define a new version of
calculate
and return
:calculate
, so that the interpreter 3. again sends the symbol
:calculate
to the
synchronize
class method, which will presumably wrap
calculate
again.
πŸ’‘ 1
In Ruby,
public
,
protected
, and
private
annotations are all methods that one could (and should not) override.
🀯 1
p
Kotlin has so called "delegated properties": https://kotlinlang.org/docs/delegated-properties.html