"RokuMarkn" wrote:
That's what I'm concerned about -- I don't think we should encourage using the type numbers in a switch in production code. The isXXX predicates isolate the code from those values, leaving us free to change them (unlikely) or make more complicated predicates (maybe two different events would return true for isSomething). Anyway, I don't object to documenting them, I just want the documentation to be clear on how the values should be used.
I have to say, the more i think of it - the more i like the idea of "switch"-style event dispatching using this sort of "branch table" (after all, it's preferred way of doing it in languages w/o switch proper like
Python and
Perl). It gives expressive power - say i already have the main show event handling nailed down and want in addition to handle some "side show" like
roUrlEvent (or roTextureRequestEvent, roFileSystemEvent, ...). Well if i have the utility handlers in my tool-belt, all i have to do to add that functionality is to say "
dispatcher.append(urlEventHandler); dispatcher.append(devInfoEventHandler);" - and by adding the items from tool AAs to the dispatcher AA, all is done. Am i the only one getting excited about this, anyone else... anyone?
Mark - I understand and share your abundance of caution about "magic constants". But using such is already fact of life in BrightScript - many events (
roUrlEvent, roUniversalControlEvent, roVideoPlayerEvent etc) come with tables of magic numbers. Also we have to check for magic strings, e.g.
if type(msg) = "roUniversalControlEvent". Which gets annoying when something can come as either "String" or "roString" - or my gut tells me to capitalize it "roXmlList" (wrongly). The righteous way would have been to have
isTypeRoUniversalControlEvent() predicate and so on.
And indeed it is highly unlikely that assigned numbers will change; documenting them in a table will help to keep them straight. Having compound/meta predicates is worth a thought - but does not prevent the use of "raw" numbers. E.g. ummm... (can't come with a good example)... say there were to be a
isMediaEvent() that holds true if any of
isPaused(), isResumed(), isFullResult(), isPartialResult(), isStreamStarted(), isRequestFailed(), isRequestSucceeded(), isRequestInterrupted(). But that does not change the fact that a specific type of event had happened and the branch table will still work.
To clarify, i don't advocate use of branch tables by everyone - isXXX predicates are clearly better for beginner programmers. With a dispatcher dictionary one needs to understand functions as object/values, about scope of variables and who "M" is... things that experienced developer would say "well duh". But if the ability is there, advanced coders can use it at their own risk.