Add f-strings, which can have expressions contained within { and }.
I.e.:
def my_name <- "Joël"
print "My name is {my_name}."
The expressions contained within the curly brackets are type-checked just as any other expression.
In addition to this, we verify that they are indeed expressions (and not statements) and that the resulting value has the __str__ method.
This means that we are slightly more strict than Python, which would in the case of getting the string value of a value that does not have the __str__ instead print its memory address:
class A:
my_field = 20
a = A()
/* prints <__main__.A object at 0x012OHD70> */
print(a)
In addition to this, strings which do not contain any expressions are treated and desugared as regular strings.
So:
"my_string {my_value}" becomes f"my_string {my_value}""Hello World" becomes/stays "Hello World"
Added Tests
Happy Expressions within f-Strings have the correct position(/offset) (which is useful for error printing
Happy All primitives and collections can be contained within expressions within f-Strings
Happy f-Strings containing expressions which have a type that implements __str__ work as expected
Happy{ and } preceded by \ are treated as regular characters
Happy Empty expressions within strings encased in { and } are ignored
Sad f-Strings containing expressions that contain an expression that doesn't implements __str__ fails
Relevant issues
Fixes #178
Summary
Add f-strings, which can have expressions contained within
{
and}
. I.e.:The expressions contained within the curly brackets are type-checked just as any other expression. In addition to this, we verify that they are indeed expressions (and not statements) and that the resulting value has the
__str__
method. This means that we are slightly more strict than Python, which would in the case of getting the string value of a value that does not have the__str__
instead print its memory address:In addition to this, strings which do not contain any expressions are treated and desugared as regular strings. So:
"my_string {my_value}"
becomesf"my_string {my_value}"
"Hello World"
becomes/stays"Hello World"
Added Tests
__str__
work as expected{
and}
preceded by\
are treated as regular characters{
and}
are ignored__str__
fails