Open Alexius-Huang opened 7 years ago
@Maxwell-Alexius I'm thinking support formatting string in Goby first like:
s = String.fmt("I love %s lang!", "Goby")
puts(s) #=> I live Goby lang!
This is less convenient but easier to implement.
Update: Already implemented, see usage here
When you implement the fmt
package's Formatter
interface, you can utilize the golang's handling of formt. In the project monkey, I've implemented a fmt module, which does the formatting of the language's object, so you can have a look.
Just a memorandum: When the string interpolation is supported, I suppose disabling the concatenation String#+
intentionally might be good instead. String concatenation with +
sometimes would lead to inefficient codes and security holes like SQL injections.
The string interpolation should equip auto #to_s
(and if possible, auto sanitization as a default like below)
"Hello #{var}"
: auto #to_s
and auto sanitization"Hello ##{var}"
: auto #to_s
onlyauto sanitization
SQL sanitization can't be perfomed by the language, as it's a functionality of the database (driver); each database (driver) can/does escape in a different way, also depending on the semantic of the value to escape: tables/schemas/columns and literals are/can be escaped in different ways.
I should've noticed that: thank you!
So, as someone who mostly uses ruby in a systems environment(replacement for bash, automation etc), this is one of the most important features of the language to me. As such I am happy to take on implementation of it.
I played around with making it work the other day, and decided it would be worth discussing how best to implement this.
to_s
method.@eliothedeman sounds like the first one is more close to how Ruby handles this? (I don't really know how string interpolation work though)
@hachi8833 Thanks for the pointer. Will read!
Yeah it looks like it is actual logic is done by the compiler, which makes sense.
x = 10
y = 20
puts "x=#{x} y=#{y}"
turns into (logically speaking)
x = 10
y = 20
puts "x=" + x.to_s + " y=" + y.to_s
There is plenty that could be done to make this after at runtime, but I think a good first pass is implementing it in the same way ruby does.
Only double quotation mark support string interpolation + escaped character; Single quotation mark parse escaped character only