Your Digital Media Has Never Looked So Good

 
pjforde1978
Topic Author
Posts: 15
Joined: Mon Mar 25, 2019 1:26 pm

BrightScript feature request: string interpolation

Sat Mar 30, 2019 3:06 pm

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.
 
User avatar
Komag
Posts: 797
Joined: Fri Aug 22, 2014 3:42 am

Re: BrightScript feature request: string interpolation

Sat Mar 30, 2019 4:11 pm

What's wrong with:

Return "Welcome back, " + user + "!"
 
pjforde1978
Topic Author
Posts: 15
Joined: Mon Mar 25, 2019 1:26 pm

Re: BrightScript feature request: string interpolation

Sat Mar 30, 2019 5:04 pm

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.
 
User avatar
RokuKC
Posts: 313
Joined: Wed Sep 10, 2014 10:44 am
Location: Roku HQ

Re: BrightScript feature request: string interpolation

Wed Apr 03, 2019 4:43 pm

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.
 
pjforde1978
Topic Author
Posts: 15
Joined: Mon Mar 25, 2019 1:26 pm

Re: BrightScript feature request: string interpolation

Wed Apr 03, 2019 11:19 pm

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! :)

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.
 
User avatar
Komag
Posts: 797
Joined: Fri Aug 22, 2014 3:42 am

Re: BrightScript feature request: string interpolation

Wed Apr 03, 2019 11:38 pm

They have updated things over the years, there are changelogs, but usually it's for new stuff that didn't have an easy alternative.
 
User avatar
RokuNB
Posts: 459
Joined: Fri Mar 31, 2017 2:22 pm

Re: BrightScript feature request: string interpolation

Sun Apr 14, 2019 11:13 am

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
 
pjforde1978
Topic Author
Posts: 15
Joined: Mon Mar 25, 2019 1:26 pm

Re: BrightScript feature request: string interpolation

Mon Apr 15, 2019 5:54 pm

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.

Who is online

Users browsing this forum: marcelo.cabral and 7 guests