Forum Discussion

MarkRoddy's avatar
MarkRoddy
Visitor
16 years ago

dot operator behavior question

I've been using the 'dot' operator for some "classes" I'm using, but I'm hitting a road block when trying to build a dynamic dispatch table.

To give an example of what I need to accomplish consider the following function. Effectively, it returns the attribute specified if the 'm' variable is assigned as expected when the function return attr is called using the '.' operator or a string stating that the attribute does not exist:
Function returnattr(attrname as String) as Object                                                                 
if not m.DoesExist(attrname) then
return "Attribute " + attrname + " does not exist"
else
return m.Lookup(attrname)
end if
End Function


Now consider the following scenarios:
Sub test_dynamic_dispatch(t as object)                                                                            
obj = CreateObject("roAssociativeArray")
obj["getattr"] = returnattr
obj["a"] = "a"

'Using dot operator to perform call
t.assertEqual("a", obj.getattr("a"))

'Using dot operater in an eval()
result = "NO VALUE ASSIGNED"
attrname = "a"
code_snippet = "result = obj.getattr(attrname)"
eval(code_snippet)
t.assertEqual("a", result)

'Using dot operator to do method dispatch
method = obj.getattr
t.assertEqual("a", method("a"))
End Sub


The first (explicitly using the dot operator) and second (using eval) approaches work as expected. In the case of the third (looking up the method then calling it) the 'm' variable is assigned to an empty roAssociativeArray. Is to be expected that the only time 'm' will be set is when the method is explicitly called with the dot operator even if the method is accessed with the dot operator initially?

-Mark

14 Replies

  • Note that the technique of doing "dynamic dispatch" with the associative array syntax:

    c["func"]()


    will only work if the underlying object is an associative array. This is not true for objects created with the CreateObject() function. This technique will not work with those objects, and you are back to using Run() or eval()

    --Kevin
  • I suspected as much, but hadn't bothered to test. Luckily, that can be solved through a simple wrapper class that just dispatches to the component methods. Obviously there will by a little overhead (probably negligible unless there's LOTS of calls), but also the ability to put in sane defaults to the common use cases. In all, a fair trade-off in my opinion.
  • "RokuKevin" wrote:
    ...we will continue to expand BrightScript's capabilities in the future. Including dynamic dispatch, closures, etc... could reasonably make the cut at some point.


    I know it's a long shot, and there's other stuff to work on that's more important, but closures would be really useful right about now. Are there any scheduled plans for them in the future, or is it still a "it's possible" thing? Anonymous functions are great, but anonymous functions with closures are awesome.
  • There are no immediate plans to provide closures in the language. I agree they would be useful.

    --Kevin