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: 
sawood6626
Level 7

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
Roku Employee
Roku Employee

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
Level 7

Re: Possible Bug

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

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