goby icon indicating copy to clipboard operation
goby copied to clipboard

Support String Interpolation in Goby

Open Alexius-Huang opened this issue 7 years ago • 10 comments

>> lang = 'Goby'
#>> Goby
>> message = "I love #{lang} lang!"
#>> I love Goby lang!
>> message2 = 'I love #{lang} lang!'
#>> I love #{lang} lang!

Only double quotation mark support string interpolation + escaped character; Single quotation mark parse escaped character only

Alexius-Huang avatar Aug 15 '17 03:08 Alexius-Huang

@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

st0012 avatar Aug 15 '17 03:08 st0012

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.

haifenghuang avatar Oct 26 '17 09:10 haifenghuang

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 only

hachi8833 avatar Mar 31 '18 04:03 hachi8833

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

64kramsystem avatar Mar 31 '18 10:03 64kramsystem

I should've noticed that: thank you!

hachi8833 avatar Apr 01 '18 01:04 hachi8833

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.

Where should this code live

  • Should the lexer be splitting the string into segments, and creating sub expressions. This could then be wrapped in a class by the parser that would do the string formatting in a to_s method.
  • Put all logic in the parser and put the logic in the regular string class. We could mark at compile time if the string contained interpolation, and each sub expression could be handled as a block.

eliothedeman avatar May 16 '18 04:05 eliothedeman

@eliothedeman sounds like the first one is more close to how Ruby handles this? (I don't really know how string interpolation work though)

st0012 avatar May 16 '18 06:05 st0012

This might be the one? https://stackoverflow.com/questions/25488902/what-happens-when-you-use-string-interpolation-in-ruby

hachi8833 avatar May 16 '18 06:05 hachi8833

@hachi8833 Thanks for the pointer. Will read!

eliothedeman avatar May 20 '18 16:05 eliothedeman

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.

eliothedeman avatar May 20 '18 16:05 eliothedeman