Introspection possible?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
Vanceone
Posts: 37
Joined: Sat Dec 15, 2012 7:27 pm

Introspection possible?

Post by Vanceone » Tue Dec 30, 2014 7:37 pm

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?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Introspection possible?

Post by Simon » Tue Dec 30, 2014 7:43 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6028
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Introspection possible?

Post by dunbarx » Tue Dec 30, 2014 8:55 pm

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

Vanceone
Posts: 37
Joined: Sat Dec 15, 2012 7:27 pm

Re: Introspection possible?

Post by Vanceone » Tue Dec 30, 2014 10:34 pm

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)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5024
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Introspection possible?

Post by jacque » Wed Dec 31, 2014 5:41 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6028
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Introspection possible?

Post by dunbarx » Wed Dec 31, 2014 6:09 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6678
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Introspection possible?

Post by FourthWorld » Wed Dec 31, 2014 6:43 pm

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.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Vanceone
Posts: 37
Joined: Sat Dec 15, 2012 7:27 pm

Re: Introspection possible?

Post by Vanceone » Thu Jan 01, 2015 8:27 pm

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5024
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Introspection possible?

Post by jacque » Fri Jan 02, 2015 7:25 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”