> The f-string `f"..."` could be implemented as macro as `f!("...")`. Not quite as nice to read, but would still be useful for experimenting with.
In julia, we have a rule where a macro whose name ends with `_str` is usable as a "string macro" and it receives the input string in its raw form, and then any string macro is usable by just prefixing the name at the beginning of the string.
An example is that our regex string literal implementation is literally just
macro r_str(s)
Regex(s)
end
which allows for easy compile-time regex generation. With that you can simply write e.g. `r"^[ \t]+|[ \t]+$"` which is the same as `@r_str("^[ \t]+|[ \t]+$")`, e.g. julia> match(r"^[ \t]+|[ \t]+$", " hi")
RegexMatch(" ")
So using this rule, Python's f-strings could just be re-implemented as a string macro.https://docs.julialang.org/en/v1/manual/metaprogramming/#met...
The nice thing about macros is that they run during the parsing of the code, so they have no runtime cost associated with them.
Basically, you can think of it as a (limited) way for users or packages to add new keywords to a language without having to change the whole language implementation for everyone.
Many recent new keywords that were added to Python could have been implemented as macros, which means that they could have just lived in packages rather than needing to be upstreamed into the base language implementation from day 1.
pansa2•2mo ago
Looks like this was written in 2020, but IMO Python crossed the “fits in your brain” (or at least my brain!) threshold years earlier. Nowadays, are there any popular languages that could be described that way? Maybe Go? Or Lua?
vintagedave•2mo ago