modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.92k stars 2.58k forks source link

[Feature Request] Format Strings or `f'string'` #398

Open amqndin opened 1 year ago

amqndin commented 1 year ago

Bug description

https://i.imgur.com/T4HF4dY.png
as you can see in the screenshot i was not able to use f'string' in mojo.

Steps to reproduce

- bro = 'mojo'
print(f'hello, {bro}!')

- https://i.imgur.com/T4HF4dY.png

- Include anything else that might help us debug the issue.

System information

%%python
import subprocess
subprocess.Popen(["cat", "/proc/self/cgroup"])
Mogball commented 1 year ago

Thanks for filing this issue. Mojo is missing this feature at the moment but it's something we plan to add.

Kratosix commented 1 year ago

asked in https://github.com/modularml/mojo/issues/86#issuecomment-1542587207

ivellapillil commented 1 year ago

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.

ksandvik commented 1 year ago

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.

ivellapillil commented 1 year ago

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.

gryznar commented 1 year ago

+1 for more generic implementation

drunkwcodes commented 1 year ago

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.

ivellapillil commented 1 year ago

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.

drunkwcodes commented 1 year ago

@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.

lattner commented 1 year ago

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.

lattner commented 1 year ago

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

drunkwcodes commented 1 year ago

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?

iFrozenPhoenix commented 2 weeks ago

Hello, are there any updates on this topic? Is this something that will be implemented in the short time?