4 ms·
> ... I don’t know the “official” name for it, so I just stole the name from a recent reddit post (magical handler functions). What's the difference between pr
by SevenNation 4y ago
> ... I don’t know the “official” name for it, so I just stole the name from a recent reddit post (magical handler functions).
What's the difference between programming "magic" and "using the language to its fullest"?
Wikipedia has an article dedicated to the concept of magical programming. It starts with:
> In the context of computer programming, magic is an informal term for abstraction; it is used to describe code that handles complex tasks while hiding that complexity to present a simple interface. The term is somewhat tongue-in-cheek, and often carries bad connotations, implying that the true behavior of the code is not immediately apparent. For example, Perl's polymorphic typing and closure mechanisms are often called "magic". The term implies that the hidden complexity is at least in principle understandable, in contrast to black magic and deep magic (see Variants), which describe arcane techniques that are deliberately hidden or extremely difficult to understand. However, the term can also be applied endearingly, suggesting a "charm" about the code. The action of such abstractions is described as being done "automagically", a portmanteau of "automatically" and "magically".
https://en.wikipedia.org/wiki/Magic_(programming) https://en.wikipedia.org/wiki/Magic_(programming)
I think that "true behavior of the code is not immediately apparent" is the key. The harder it is to figure out how something works, the more likely it is to be viewed as "magic."
Do the magical handlers in this article fit the bill? I'm not so sure. All that's going on is generics applied to functions. True, it's not as common as generics applied to other types. But it's also not fringe.
Maybe as a technique becomes more established, it starts to seem less magical. The author notes:
> Exploring this pattern was an interesting journey. I still can’t tell why it’s more popular than the alternative that contains less magic and gives more flexibility to developers. I can only assume that even Rust developers long for some magic and coolness in their tooling.
Maybe it's more popular because it's now widely used across Rust web frameworks and therefore expected as a convention?