Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
sawood6626
Visitor

Possible Bug

There seems to be a bug when dealing with strings in brightscript. It's easiest to explain with an example:
aarray = {
   foo: "bar"
}
print type(aarray.foo)
'roString

aaray = {
   foo: "bar"+"baz"
}
print type(aarray.foo)
'String


So whether I get the intrinsic string type or the object wrapper type seems to depend on whether or not I used concatenation when specifying an associative array value. If there is a good reason for this behavior then please explain it to me.
0 Kudos
3 REPLIES 3
NB_
Roku Guru

Re: Possible Bug

Not technically a bug, rather type() may lie because of some archaic reasons - e.g. see "BrightScript 3 Compatibility" under https://sdkdocs.roku.com/display/sdkdoc ... ypes-Types

Because of that (and not only), i recommend not relying on string-check for the result of type() - but instead doing a duck-typing^ check with getInterface(obj, "ifString"), e.g.
Brightscript Debugger> if getInterface({foo: "bar"}.foo, "ifString") <> invalid then ? "tis a string" 
tis a string

Brightscript Debugger> if getInterface({foo: "b"+"ar"}.foo, "ifString") <> invalid then ? "tis a string"
tis a string

(^) "In other words, don't check whether it IS-a duck: check whether it QUACKS-like-a duck, WALKS-like-a duck, etc, etc, depending on exactly what subset of duck-like behaviour you need to play your language-games with." (Alex Martelli)
0 Kudos
sawood6626
Visitor

Re: Possible Bug

Thanks for the quick response and the advice. I'll keep this in mind.
0 Kudos
Komag
Roku Guru

Re: Possible Bug

Function IsBoolean(value As Dynamic) As Boolean
    Return GetInterface(value, "ifBoolean") <> INVALID
End Function

Function IsString(value As Dynamic) As Boolean
    Return GetInterface(value, "ifString") <> INVALID
End Function

Function IsArray(value As Dynamic) As Boolean
    Return GetInterface(value, "ifArray") <> INVALID
End Function

Function IsAssociativeArray(value As Dynamic) As Boolean
    Return GetInterface(value, "ifAssociativeArray") <> INVALID
End Function

Function IsInteger(value As Dynamic) As Boolean
    Return GetInterface(value, "ifInt") <> INVALID And (Type(value) = "roInt" Or Type(value) = "roInteger" Or Type(value) = "Integer")
End Function

Function IsFloat(value As Dynamic) As Boolean
    Return (GetInterface(value, "ifFloat") <> INVALID Or (Type(value) = "roFloat" Or Type(value) = "Float"))
End Function

Function IsDouble(value As Dynamic) As Boolean
    Return (GetInterface(value, "ifDouble") <> INVALID Or (Type(value) = "roDouble" Or Type(value) = "roIntrinsicDouble" Or Type(value) = "Double"))
End Function

Function IsList(value As Dynamic) As Boolean
    Return GetInterface(value, "ifList") <> INVALID
End Function

Function IsDateTime(value As Dynamic) As Boolean
    Return (GetInterface(value, "ifDateTime") <> INVALID Or Type(value) = "roDateTime")
End Function

Function IsXmlElement(value As Dynamic) As Boolean
    Return GetInterface(value, "ifXMLElement") <> INVALID
End Function

Function IsFunction(value As Dynamic) As Boolean
    Return GetInterface(value, "ifFunction") <> INVALID
End Function

Function IsHttpAgent(value As Dynamic) As Boolean
    Return GetInterface(value, "ifHttpAgent") <> INVALID
End Function

All these from The Endless: viewtopic.php?f=34&t=82858&view=unread#p476634
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.