"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
"TheEndless" wrote:
Since the original post was about case sensitivity, it's worth noting that this method is case-insensitive.
"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
"Komag" wrote:
EDIT - This will work to check that it's either a String or a roString?
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.
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.
"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
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
"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:
Note that roDateTime, in particular, doesn't appear to actually implement the ifDateTime interface... not sure what that's all about.
BrightScript Debugger> ? FindMemberFunction(CreateObject("roDateTime"), "asSeconds")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.
<Interface: ifRoDateTime>
"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?
"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.
"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.