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: 
TheEndless
Channel Surfer

Re: Checking Type() is case sensitive - undocumented?

"EnTerr" wrote:
Personally i recommend doing the type check with `getInterface(val, "ifString") <> invalid` instead. It's shorter and faster (just ran some tests - at least 2x faster in general, can be 7x faster if type(val)="roString" in particular).

Best however will be to define a test function for that and use it. That way code is most readable and fn can be updated in the future (or switch between the three different approaches in this thread). Here is mine:
function isString(val):
return getInterface(val, "ifString") <> invalid
end function

Since the original post was about case sensitivity, it's worth noting that this method is case-insensitive.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
EnTerr
Roku Guru

Re: Checking Type() is case sensitive - undocumented?

"TheEndless" wrote:
Since the original post was about case sensitivity, it's worth noting that this method is case-insensitive.

You mean the function getInterface() is case-insensitive regarding its 2nd argument? That it is, yes - can call with "iFstRing".
0 Kudos
Komag
Roku Guru

Re: Checking Type() is case sensitive - undocumented?

"EnTerr" wrote:
Personally i recommend doing the type check with `getInterface(val, "ifString") <> invalid` instead. It's shorter and faster (just ran some tests - at least 2x faster in general, can be 7x faster if type(val)="roString" in particular).

Best however will be to define a test function for that and use it. That way code is most readable and fn can be updated in the future (or switch between the three different approaches in this thread). Here is mine:
function isString(val):
return getInterface(val, "ifString") <> invalid
end function


But would calling the function lose some of the snappiness? I mean if this is done many times per second, wouldn't it be faster to keep it direct, just use `getInterface(val, "ifString") <> invalid`? I have a LOT of functions, and I'm beginning to wonder if the game has to do a big search through all the functions to find the one with the right name and whether that takes time, sort of like looking up a key/value pair in an AA with . operator.

EDIT - This will work to check that it's either a String or a roString?
0 Kudos
EnTerr
Roku Guru

Re: Checking Type() is case sensitive - undocumented?

"Komag" wrote:
EDIT - This will work to check that it's either a String or a roString?

Yes. It will work with all* string types in B/S.

But would calling the function lose some of the snappiness? I mean if this is done many times per second, wouldn't it be faster to keep it direct, just use `getInterface(val, "ifString") <> invalid`? I have a LOT of functions, and I'm beginning to wonder if the game has to do a big search through all the functions to find the one with the right name and whether that takes time, sort of like looking up a key/value pair in an AA with . operator.

No on "big search". Tiny penalty for calling a global function (`function myFun() ...`) or local function (`localVar = function()... end function: ...: localVar(..)`). No runtime lookup by name is needed, it was done during compilation.

The convenience of having a type-check function greatly outweighs the small slowdown - easy to read, you can change implementation, less risk of typos (like proper capitalization, har-har). To put it in perspective, even as a function call `isString(id)`, it is still faster than `LCase(Type(id)) = "string" Or UCase(Type(id)) = "ROSTRING"` . BUt are TheEndless and RokuJoel - two BrS-experienced developers - losing sleep over that check code speed? No - and neither should you.

(*) all 6 of them, let me flourish some arcane knowledge here Smiley LOL
0 Kudos
Komag
Roku Guru

Re: Checking Type() is case sensitive - undocumented?

Thanks! I appreciate the insights, good to know that calling functions is fast. I'll change my Type() checks.

What about roArray, roBoolean, and Integer and roInteger?

I'm assuming:
getInterface(val, "ifArray") <> invalid
getInterface(val, "ifBoolean") <> invalid
getInterface(val, "ifInt") <> invalid

?

The section about GetInterface is not clear to me yet:
7.3 GetInterface(object as Object, ifname as String) as Interface
Each BrightScript Component has one or more interfaces. This function returns a value of type "Interface".
Note that generally BrightScript Components allow you to skip the interface specification. In which case, the appropriate interface within the object is used. This works as long as the function names within the interfaces are unique.
0 Kudos
TheEndless
Channel Surfer

Re: Checking Type() is case sensitive - undocumented?

"Komag" wrote:
What about roArray, roBoolean, and Integer and roInteger?

I'm assuming:
getInterface(val, "ifArray") <> invalid
getInterface(val, "ifBoolean") <> invalid
getInterface(val, "ifInt") <> invalid

Generally speaking, yes, but I've apparently run into at least one occasion where the interface check didn't work as expected, resulting in me having these utility functions:
Function IsBoolean(value As Dynamic) As Boolean
Return GetInterface(value, "ifBoolean") <> 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 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 IsString(value As Dynamic) As Boolean
Return GetInterface(value, "ifString") <> 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


Note that roDateTime, in particular, doesn't appear to actually implement the ifDateTime interface... not sure what that's all about.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
EnTerr
Roku Guru

Re: Checking Type() is case sensitive - undocumented?

"TheEndless" wrote:
Generally speaking, yes, but I've apparently run into at least one occasion where the interface check didn't work as expected, resulting in me having these utility functions:

The way you check in isFloat() and isDouble() might be a case of wearing both belt and suspenders (which you have been observed to, occasionally 😛 ).
Your isInteger() logic is more interesting and i wonder why. Was it that you suspected some other type may be coerced into integer by GetInterface?

Note that roDateTime, in particular, doesn't appear to actually implement the ifDateTime interface... not sure what that's all about.

The newly hatched FindMemberFunction (thanks... RokuKC?) to the rescue:
BrightScript Debugger> ? FindMemberFunction(CreateObject("roDateTime"), "asSeconds")
<Interface: ifRoDateTime>
Aha! Seems the interface currently is named ifRoDateTime and not ifDateTime. I wouldn't put both my eggs in ifRoDateTime's basket though - naming seems to be a bug that RokuCo may fix and bring in line with teh documentation.
0 Kudos
TheEndless
Channel Surfer

Re: Checking Type() is case sensitive - undocumented?

"EnTerr" wrote:
Your isInteger() logic is more interesting and i wonder why. Was it that you suspected some other type may be coerced into integer by GetInterface?

I believe this is exactly it, because one of the roXXXEvents (don't recall which) implements the ifInt interface, which caused an IsInteger() check to return True, which while technically correct I suppose, is not what I was after.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
RokuKC
Roku Employee
Roku Employee

Re: Checking Type() is case sensitive - undocumented?

"EnTerr" wrote:
"Komag" wrote:
EDIT - This will work to check that it's either a String or a roString?

Yes. It will work with all* string types in B/S.


Note that there are components other than String / roString that implement the ifString interface.

This may be academic in your case, but if your intent is to truly identify string objects that have regular string object behavior I would advise doing the lcase(type(x)) = "string" etc. comparison as TheEndless demonstrated.

Similarly for ifInt et al.
0 Kudos
EnTerr
Roku Guru

Re: Checking Type() is case sensitive - undocumented?

"RokuKC" wrote:
Note that there are components other than String / roString that implement the ifString interface.

This may be academic in your case, but if your intent is to truly identify string objects that have regular string object behavior I would advise doing the lcase(type(x)) = "string" etc. comparison as TheEndless demonstrated.

Similarly for ifInt et al.

Mmmm, good point.
I see roPath does ifString but who does ifInt (but roInt)?

Personally per my Python exposure i am fan of "duck typing" ("if it walks like a duck and swims like a duck and quacks like a duck, i call that [type] a duck"). In other words if the type can perform the duties of a String, for all practical purposes it is a string. Which i can probably check if the value can/has ifStringOps, to be more precise.

It would be very, very rare (methinks) that someone will ever need the real type(val) - and then they'd probably want to check type(val, 3) too.

PS. ultimateley, the advantage of DIY "isString()" is it can be fine tuned to one's needs
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.