-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
F-strings and format #15
Comments
Yeah, would be strange to call it that, since there's no f :) |
Hahah. I don't think we need the f to call it an f-string. Any name options? And do you think this is a good way to implement something f-string like in Gurklang? |
My idea: make a function that turns a string into a parent-scoped function: >>> "Hello, {name}!" %
>>> inspect.dis
Disassembling function 1 (flags: PARENT_SCOPE, closure: none):
(Put :{% sentinel})
(Put "!")
(CallByName "name")
(Put "Hello, ")
(Put `--%-concat`)
(CallByValue)
>>>
>>> "Gustav" :name var
>>> "Hello, {name}!" % ! println
"Hello, Gustav!"
>>> This is similar to how template engines first compile a template to a function. The advantage is that this function could be implemented in Gurklang. |
Crude imperative implementaiton, using imaginary functions. Should work, but assumes the input is correct. { {(CallByValue) {:Put --%-concat},}] box :compiled var
"" box :part var
{ { ("{") { compiled {:Put part ->}, <-cons- part "" <- }
("}") { compiled {:CallByName part ->}, <-cons- part "" <- }
( c ) { part { c concat } <= }
} case
} string foreach-codepoint
compiled (CallByValue) <-append-
compiled -> invert-linked-list "%-string" inspect.make-function
compiled <X- part <X-
} :% jar |
That looks quite interesting. Compiling it into a function also allows it to be passed around easily, in some sense. What would be the advantages of requiring |
@gustavwilliam Well, it takes quite some time to compile the function. So the compiled function can be stored and given a name. |
True. And it could help if the same f-string is reused multiple times. The name |
Ah yes,
|
Float modulo doesn’t sound like it should really exist. I’m not a huge fan of calling it %% or %str either. It just seems a bit off imo. Honestly, maybe |
What about |
True. We could just call it Would you like to implement this, considering that you seem to have a semi-functional python version of this already? |
I'd like to work on the docs instead. Maybe I can assign you to implement string-related stuff? |
I could implement a lot of the other string commands — maybe this one as well. I’ll just have to figure out the best way to do string interpolation. |
F-strings are one of the most convenient features in python. We should be able to implement something similar in Gurklang. I'm proposing the following syntax:
This would parse the string and create a new string with the values of the variables with the names inside
{}
, in the string. Just like in python. An issue about string formatting with arguments will be opened shortly, but this would be the syntax for f-strings.What do you think that the name should be? We don't have to call it
f-str
orf-string
if we don't want to.The text was updated successfully, but these errors were encountered: