Open amqndin opened 1 year ago
Thanks for filing this issue. Mojo is missing this feature at the moment but it's something we plan to add.
It would be great if the f-strings are implemented similar to how it is done in Scala: https://docs.scala-lang.org/scala3/book/string-interpolation.html
Basically, instead of hard-coded handling in the compiler, please provide a generic solution, where we can define our own interpolations. This allows for typesafe SQL, and other uses.
Most likely there will be f-strings as defined in Python for compatibility, and something else then that could be very different, maybe as an open-source initiative.
There's also an option to influence Python to implement f-string interpolation via a PEP.
I don't think it need to be mutually exclusive. AFAIK, Scala is able to achieve f-string like functionality using a generic solution. The official f-function/macro(?) can be part of the SDK, while the community could provide other functionalities such as type-safe SQL. My main request is to avoid implementing f-strings hard-coded into the compiler.
Just to be clear, I also want a Python compatible f-string implementation in the Mojo SDK. However, I would like to see it as a library function/macro and not as a "language" feature.
+1 for more generic implementation
I would like f-string to be a language feature.
I use print(f"{variable = }")
to debug all of the time.
It's so useful to make additional importing bothersome.
Some languages like Java have default imports for core language libraries (e.g. java.lang). f-strings can be such an automatic import, while still keeping the implementation as a library instead of in the compiler.
@svihar It's a good advice considering PEP 701 just made f-string more complex. It's improving and changing quickly.
I don't mind if it is as usable as other mojo syntax.
FWIW, I agree with the above - we can provide something more generic which is nonetheless compatible with Python. Swift also has a bunch of user-extensible string interpolation features as well.
We probably won't wait this long, but ideally you design the parsing and unparsing components of a language together - eg you want to add enums (aka algebraic datatypes) when you add pattern matching support. In this case, it means designing string interpolation and regex support together. This ensures the concepts line up nicely.
Swift didn't do this, but did eventually add regex's. I'm not arguing that Mojo should follow it's path necessarily, but if you're curious there are lots of links online about how it works: https://www.hackingwithswift.com/swift/5.7/regexes
Swift's regex is interesting that it uses //
to quote to regex pattern.
julia's regex integrates with strings more. It even bakes the regex class into raw strings. I'm wondering if mojo can do this and be compatible with python at the same time.
The ruff linter story is astonishing:
We should make it in mojo too!
The regex engine behind ruff is rust regex, which provides a clear benchmark.
The engine which can defeat rust regex is PCRE-JIT
.
I'm willing to try to make a wrapper for mojo.
Is it ok for us to choose PCRE-JIT
as our regex engine?
It is a good test-run for my project manager. Is there any brief tutorial for writing a C/C++ extension for mojo?
Hello, are there any updates on this topic? Is this something that will be implemented in the short time?
Bug description
Steps to reproduce
System information