Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
pjforde1978
Level 7

BrightScript feature request: string interpolation

Coming from Ruby/ES6, there are some language niceties that would go a long way towards increasing developer happiness which perhaps your team could borrow. Top of mind is string interpolation:

' Ruby-style
user = "PJ"
return "Welcome back, #{user}!"


In JavaScript, we have to use backtick strings to interpolate, likely for backward compatibility:

' ES5/ES6 style
user = "PJ"
return `Welcome back, ${user}!`


Again, I wouldn't presume to know how hard or impossible this would be in the context of the BrightScript black-box, but I'd love to hear more.
0 Kudos
7 Replies
Komag
Level 9

Re: BrightScript feature request: string interpolation

What's wrong with:

Return "Welcome back, " + user + "!"
0 Kudos
pjforde1978
Level 7

Re: BrightScript feature request: string interpolation

There's nothing explicitly wrong with it, and I didn't suggest that there was.

However, there's a reason that people love to work with some languages more than others. You'll note that interpolation doesn't stop Ruby/JS devs from appending strings. It's just far shorter and arguably easier to visually parse. In short, it's a nicer syntax and doesn't break with any pre-existing conventions or language style opinions.

Believe it or not, C didn't support strings when it came out. You worked with arrays of characters. There's nothing inherently bad about doing that, but working with strings makes some developers happy. Go figure. There are still many subsets or limited instruction dialects that work with arrays of chars today, most notably the GSM interface layer that runs on your SIM card.
0 Kudos
Roku Employee
Roku Employee

Re: BrightScript feature request: string interpolation

There's no interpolation feature, but there are substitution and replacement APIs, which are about as good IMO.


thing = "book"
color = "red"
print Substitute("My {0} is {1}.", thing, color)
' prints "My book is red."


or


thing = "book"
color = "red"
print "My %s is %s.".Format(thing, color)
' prints "My book is red."


or


thing = "book"
color = "red"
print "My ${thing} is ${color}.".Replace("${thing}", thing).Replace("${color}", color)
' prints "My book is red."


You could also have a utility function that took a string and AA with key/value pairs and iteratively do the Replace calls, instead of hard-coding it as above.

Admittedly, all of these are more manual solutions than a generic interpolation feature would be, but in my experience they align pretty well with real world needs.
0 Kudos
pjforde1978
Level 7

Re: BrightScript feature request: string interpolation

Thanks so much for taking the time to answer. All of your suggestions have their places, except possibly the horror that is the 3rd code pattern with all of the Replace calls. Burn it with fire! Smiley Happy

Moving past all of the "is it plugged in?" suggestions - I didn't ask how to fake string interpolation, after all - I guess what I'm really trying to figure out is whether BrightScript is a living, evolving language that improves over time? Like... I won't waste your time or mine suggesting that a spread operator or object destructuring would be really useful if the goal is to keep the language static and unchanging. I'm not a zealot... there's plenty of reasons to keep things hyper-conservative on an embedded device. It's also true that, while I've implemented simple language grammar parsers and can guess at how BRS is working behind the scenes, I truly have no idea what's hard and what's impossible. (I assume that everything worth doing starts at hard.)

So... does your team take suggestions? Is there a voting mechanism for things active devs would love to see arrive in OS updates? One system I've used on OSS projects in the past is to give everyone a small number of votes for requests, and if the request is implemented, they get the vote back.
0 Kudos
Komag
Level 9

Re: BrightScript feature request: string interpolation

They have updated things over the years, there are changelogs, but usually it's for new stuff that didn't have an easy alternative.
0 Kudos
Roku Employee
Roku Employee

Re: BrightScript feature request: string interpolation

Here is string interpolation sketched (there may be typos but overall this is sound):

' input = "foo ${bar} baz"
' subsAA = {bar: "xyzzy"}
' output = "foo xyzzy baz"

'cache macro regex singleton somewhere
macroRegEx = m.macroRegEx
if macroRegEx = invalid
macroRegEx = createObject("roRegEx", "\${([^}]*)}", "")
m.macroRegEx = macroRegEx
end if

matches = macroRegEx.matchAll(input) ' get all macro matches (think "introns" in a gene)
if matches.count() = 0
output = input ' nothing to replace
else
gaps = macroRegEx.split(input) ' all "exons" between them
' general case is (note that re.split() compresses empty matches at end of list)
' input = gap0 macro0 gap1 macro1 ... macroN gapN [macroN+1 ... macroN+K]
gaps.reset() ' reset() needed to iterate over roList
if gaps.isNext() then output = gaps.next() else output = ""

for each match in matches
val = subsAA[match[1]]
if val = invalid then match = match[0] ' not found - leave macro
output += val.toStr()
if gaps.isNext() then output += gaps.next()
end for
end if
0 Kudos
pjforde1978
Level 7

Re: BrightScript feature request: string interpolation

That would seem to be a reasonable implementation, but it's still implemented in BrightScript and seems to require explicitly calling a function.

I'm definitely talking about / proposing adding string interpolation to your basic string handling in the BrightScript language for a future OS upgrade release. Bigger picture, I'm trying to feel out what your tolerance and appetite is for language suggestions and contributions. For example, if you ran with adding string interpolation, I'd next start talking about things like spread operators and object destructuring.

It's all just language features that enable increasingly more powerful patterns. You guys do clearly add stuff to the language and platform, there's just no roadmap or real engagement beyond this forum, which feels like a support capacity. There's nothing inherently wrong with that, but there's definitely an opportunity for you guys to engage with a more pragmatic and helpful developer subset within the community that are trying to use the platform to do new and interesting things.

No wrong answers. I appreciate both of your replies so far.
0 Kudos