boo-lang / boo

The Boo Programming Language.
BSD 3-Clause "New" or "Revised" License
874 stars 148 forks source link

Interpolated string extension methods #190

Closed Guevara-chan closed 6 years ago

Guevara-chan commented 6 years ago

Consider following sample:

import System.Runtime.CompilerServices
[Extension] def cap(text as string):
    print Char.ToUpper(text[0]) + text[1:].ToLower()
name = "tester"
"hello, $name !".cap()

Do you expect it to work ? Me too. However, compiler disagree:

BCE0043: Unexpected token: \<EOL>. BCE0043: Unexpected token: .

What could be done ? Is it bug or somewhat intended restriction ?

masonwheeler commented 6 years ago

This should work. You're seeing a parser error, which happens well before any interpretation of string interpolations, and one that doesn't reproduce on my end, so I'm not quite sure what's going on with your code.

Guevara-chan commented 6 years ago

Cool. That being said, enclosing interpolated string with brackets works for some reason: ("hello, $name !").cap() # No error here.

masonwheeler commented 6 years ago

Right. If it's a simple reference expression, no parens or brackets are needed. They're only needed if you're enclosing more than one identifier, like $(name.ToUpper), so the parser knows how much is interpolation and how much is the literal part of the string.

Guevara-chan commented 6 years ago

Um, cool... I guess. So, this was intended behavior ?

masonwheeler commented 6 years ago

Yeah. Did you ever figure out what was going on with the parse error?