Post by tsh73 on May 1, 2020 3:32:41 GMT -5
I'm a bit dated in my compiler choices
so the suddenly I encountered something like this in C#
I got puzzled and had to Google.
It happened top be "interpolated string" -
$ instructs compiler run through string,
pick variables in {},
and put their values (more exact, their .toString()) into string.
So in the end we got
(Googling some more, I found that similar thing is named "f-string" in Python since 3.6 - only visible difference is "f" instead of "$")
So let's compare LB way
It seems not that bigger/more complicated,
but whoever did not miss a semicolon or quote, could throw a stone at me.
So the questions is:
Is it nice feature to have?
Could it be made into user-defined (library) function?
Here what I got
(it does not checks for ANY errors now)
The only BIG problem - to use (EVAL) variables needed, they should be available. Only way is to make it global.
May be it is possible to make special version of EVAL what will see variables with scope upper in call chain? (not in function called EVAL, but in parent function)?
This looks really interesting to me but I wonder if it is even possible.
Only Carl could say if it for sure for LB or may be LB5.
results
code
so the suddenly I encountered something like this in C#
$"x={x}, y={y2} ms"
I got puzzled and had to Google.
It happened top be "interpolated string" -
$ instructs compiler run through string,
pick variables in {},
and put their values (more exact, their .toString()) into string.
So in the end we got
x=3.5, y=-0.35078323 ms
(Googling some more, I found that similar thing is named "f-string" in Python since 3.6 - only visible difference is "f" instead of "$")
So let's compare LB way
"x={x}, y={y2} ms" --interpolated string
"x=";x;", y=";y2;" ms" --LB way
It seems not that bigger/more complicated,
but whoever did not miss a semicolon or quote, could throw a stone at me.
So the questions is:
Is it nice feature to have?
Could it be made into user-defined (library) function?
Here what I got
(it does not checks for ANY errors now)
The only BIG problem - to use (EVAL) variables needed, they should be available. Only way is to make it global.
May be it is possible to make special version of EVAL what will see variables with scope upper in call chain? (not in function called EVAL, but in parent function)?
This looks really interesting to me but I wonder if it is even possible.
Only Carl could say if it for sure for LB or may be LB5.
results
*Interpolated strings*
string
x={x}, y={y2} ms
Interpolates to
x=3.5, y=-0.35078323 ms
But variables used should be available, that is, global
old way, prints same thing (quote chanhed to (') to show)
'x=';x;', y=';y2;' ms'
x=3.5, y=-0.35078323 ms
Bonus (after reading on python f-strings)
it works with number expressions!
(LB eval limits to native functions, no user-defined)
x=4.5, y=-0.97753012 ms
code
'needs LB for EVAL
global x, y2
a$="x={x}, y={y2} ms"
x = 3.5
y2 = sin(x)
print "*Interpolated strings*"
print
print "string"
print a$
print "Interpolates to"
print interpolate$(a$)
print "But variables used should be available, that is, global"
print
print "old way, prints same thing (quote chanhed to (') to show)"
print "'x=';x;', y=';y2;' ms'"
b$="x=";x;", y=";y2;" ms"
print b$
print
print "Bonus (after reading on python f-strings)"
print "it works with number expressions!"
print "(LB eval limits to native functions, no user-defined)"
a$="x={x+1}, y={sin(x+1)} ms"
print interpolate$(a$)
function interpolate$(a$)
'get variables inside {}
p0 = 0
do
p1 = instr(a$, "{", p0)
if p1 then
p2 = instr(a$, "}", p1+1)
varName$ = mid$(a$, p1+1, (p2-p1-1))
'you turn printing variable names on/off here
'print ">";varName$;"<"
res$ = res$ + mid$(a$, p0+1, p1-p0-1); eval(varName$)
p0 = p2
end if
loop while p1
interpolate$ = res$ +mid$(a$, p2+1)
end function