Martin sent good links, I'll just add a practical example: Without macros, styled-components works like this: import styled from "styled-components"; const Label = styled.div` color: red; `; (that's a js template literal, next level after f-strings, here without any arguments) When e.g. <Label>Hi</Label> is rendered, the result is <div class="4242">Hi</div> where class name is a hash of the inline css, and css is injected into the document head. This is great for many reasons, except developer often ends up with the below on the page (dev tools, though page source is possible too) where it's quite hard to mentally work back which styled.div was is that generated hash 7676... <div class="1231"> <div class="7676"> <div class="9898"> ... Here's the same with babel macros: import styled from "styled-components/macro"; const Label = styled.div` color: red `; When <Label>Hi</Label> is rendered, the result is <div class="filename_Label_4242">Hi</div> where class name encodes hash of the inline css, but also the file name where it was "defined" as well as the name of the variable to which it was assigned, changing the div tree e.g. to: <div class="bezel_Head_1231"> <div class="bezel_Container_7676"> <div class="menu_Header_9898"> ... How it works: * babel macros work on AST, not text * babel macro has access to entire module AST, and can thus infer and modify the module: * here, module name is recorded, and if the result of macro call is assigned, then target variable name is recorded * I've seen automatic import injection (useful to pass glocal state in e.g. localisation libraries) * almost anything is possible There are downsides: 1. the macro code is more involved, e.g. see https://github.com/styled-components/styled-components/blob/master/packages/... 2. multiple macros can and do collide on occasion (usually when written naively), which somewhat limits composability by end users On Sat, 17 Oct 2020 at 07:18, Guido van Rossum <guido@python.org> wrote:
Dima,
Do you have a link to "babel macros"? Searching for that brought up several different things; not being a frequent JS user I don't know how to filter these.
--Guido
On Wed, Oct 14, 2020 at 11:55 PM Dima Tisnek <dimaqq@gmail.com> wrote:
My 2c as a Python user (mostly) and someone who dabbled in ES2020:
The shouting syntax! does not sit well with me. The $hygenic is also cumbersome.
To contrast, babel macros: * looks like regular code, without special syntax: existing tooling works, less mental strain * have access to call site environment, so not strictly hygienic(?): allow for greater expressive power
I these the two points above really helped adopt babel macros in the js community and should, at the very least be seriously considered by the py community.
Cheers, d.
On Sat, 26 Sep 2020 at 21:16, Mark Shannon <mark@hotpy.org> wrote:
Hi everyone,
I've submitted my PEP on syntactic macros as PEP 638. https://www.python.org/dev/peps/pep-0638/
All comments and suggestions are welcome.
Cheers, Mark _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/U4C4XHNR... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/VEC7VWY5... Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido) Pronouns: he/him (why is my pronoun here?)