Page 1 of 1

Introspection possible?

Posted: Tue Dec 30, 2014 7:37 pm
by Vanceone
Is there a way to check if an object responds to a message before you send it? I'm thinking of building a library, and I'd like to be able to have the library call functions in the calling object. A delegate pattern, if you will. Cocoa has a "respondsToSelector" command that you can call before you actually call the function, so you won't cause a runtime error. I would like to be able to do the same--see if a function or command exists in the target object before I call it. Is there a way to do that?

In general, can you implement a delegate pattern? Also, is anything like key-value coding possible?

Re: Introspection possible?

Posted: Tue Dec 30, 2014 7:43 pm
by Simon
Hi Vanceone,
I'm not sure I understand the full implications of your request but I believe in liveCode you would use the "try/catch" method. This matches
...so you won't cause a runtime error.
Simon

Re: Introspection possible?

Posted: Tue Dec 30, 2014 8:55 pm
by dunbarx
Is there a way to check if an object responds to a message before you send it?
I must not understand either. Do you mean "Is there a handler for a message I would like to send in the target object?". In other words, "...because I would not send a message to that object if there was not"?

Craig Newman

Re: Introspection possible?

Posted: Tue Dec 30, 2014 10:34 pm
by Vanceone
Yes, I mean "Is there a handler named XXXXX? If so, call it. If not, do something else." Try/catch would work, but it would be nice to just have a command. "If object hasHandler 'onMouseDown' then send "mouseDown" to object else pass"

Or something like that.

Also, is there a way to observe changes in an object's properties? I want to build something that will be notified every time the rect of an object changes, and the object (button, field, group--whatever) doesn't have to be coded to send out the notifications itself (That makes for lots of repetitive code, and behaviors, while useful, can only exist one at a time. No multiple inheritance, in other words)

Re: Introspection possible?

Posted: Wed Dec 31, 2014 5:41 pm
by jacque
The "dispatch" command returns whether the command was handled or not in the result.

You can chain behaviors, though I'm not sure that will do what you need.

Re: Introspection possible?

Posted: Wed Dec 31, 2014 6:09 pm
by dunbarx
OK.

You can always check the script of the candidate object (A script is a property of an object), and see if there is any lonely text in it such as "on yourMessage" or "function yourMessage". If so, send the message. Whether this text needs to be refined or filtered further may require some testing.

Craig

Re: Introspection possible?

Posted: Wed Dec 31, 2014 6:43 pm
by FourthWorld
The revAvailableHandlers function will return a list of all handlers in an object (as well as in relevant behaviors, IIRC), but AFAIK this is only available in the IDE engine and not in the runtime engine from which standalones are derived.

I've submitted a request for this to be available in the runtime engines:
http://quality.runrev.com/show_bug.cgi?id=14321

That said, we may be able to provide alternatives if you can describe the specific scenario you're looking to achieve.

Re: Introspection possible?

Posted: Thu Jan 01, 2015 8:27 pm
by Vanceone
Thanks, I've been experimenting with Dispatch. Unfortunately, you have to use two different forms of Dispatch, one for commands and one for functions--and how do you know which one to use from the point of view of the script?

What I am looking for is to build a delegate pattern like Cocoa has. So I can have a script register as a delegate of another script. Like the Table View Controller stuff.

I'd like to have a generic "Here's a table view" script that handles talking to the data sources and also to the views. So the data source and the views would all register as such in the generic controller, which would send them messages. But I don't want to force each view to handle every message (for instance, Apple's table view controller has sections and section handling; which is overkill in the vast majority of table views but necessary in some. So views that only have one section just don't respond to the section messages). Apple has a "respondsToSelector" message that every object will answer. Dispatch helps a lot, but then there is the function/command dichotomy.

What I'm really building is a gesture recognizer system that buttons or objects can register for a variety of gestures to be recognized. My gesture system needs to send a variety of messages to objects that may or may not care about them. Some will want to be notified of everything, some only care once the gesture is recognized. Even registering as a delegate is troublesome; though thankfully dispatch can dispatch to a command name held in a variable. I suppose I could dispatch as if they were all commands, and if dispatch returns "unhanded" then try again as a function dispatch, and then give up if both are unhanded.

This also explains why I want to observe changes in other objects properties; I want to monitor the rect of each object that has registered to receive gesture messages. If the rect of the object changes that might mean the gestures success changes too.

Re: Introspection possible?

Posted: Fri Jan 02, 2015 7:25 pm
by jacque
I think I'd approach this from a different angle:

Set a custom property on all delegates which lists the commands and functions it wants to act on. In the calling handler, check the property to see if the action is one that the objects wants. If so, send the command to it.