How to tell what a function is returning

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: How to tell what a function is returning

Post by dunbarx » Fri Feb 12, 2021 5:25 pm

Another in a string of last posts...

I only use the "text" thing to illustrate the difference between how the strings returned from certain functions, er, differ. We can now differ instead of disagreeing.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How to tell what a function is returning

Post by jacque » Fri Feb 12, 2021 6:54 pm

Let's translate some keywords:

The selectedline = which line is currently selected
The selectedtext = which text is currently selected
The selectedField = which field is currently selected
The selectedChunk = which chars are currently selected

Except for the selectedtext, which specifies text as a return value, these all return a description, that is, a chunk expression.

The mouseloc = the current coordinates of the mouse
The mousecontrol = the object under the mouse

The mouseloc returns two integers. The mousecomtrol returns a chunk expression (a description.) I would not expect to get text if the mouse were over a field.

The key is in the return type. If the function returns a chunk expression you'll be getting a description back. If it's a type the compiler can use it will generally work as-is.

There are some exceptions of course, as the selectedline shows. In these cases we need to force evaluation. Sometimes just putting the chunk expression in parentheses or into a variable is enough.

Edit: re-reading this sounds very pedantic. I could have been more concise. I'm not really looking down my nose at you.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: How to tell what a function is returning

Post by richmond62 » Fri Feb 12, 2021 6:59 pm

re-reading this sounds very pedantic.
Possibly: but certainly a spot of pedantry is not a bad thing from time to time. 8)

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to tell what a function is returning

Post by bogs » Fri Feb 12, 2021 9:32 pm

Like I said earlier, Jacque, my problem is probably in my understanding of a chunk, but,
In these cases we need to force evaluation.
Is there one sure fire way to force the evaluation whether required or not, that won't shoot off an error if used with one that doesn't? This I would like to know, for sure, as it would at least give me a consistent way of dealing with all of these I might come across.

I also don't understand why the engine can 'deal with it' if I stick it in a variable, but can't deal with it using the return itself (which I thought was nothing more than a variable anyway). :?
jacque wrote:
Fri Feb 12, 2021 6:54 pm
I'm not really looking down my nose at you.
Well, I know your not. For one thing, I'm taller than you :twisted:

Besides, I'm a card carrying member of the pendantics 407 union, and your never at the meetings :twisted: :twisted:
dunbarx wrote:
Fri Feb 12, 2021 5:25 pm
We can now differ instead of disagreeing.
I already told you we differ in that your a better man all round heh.
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How to tell what a function is returning

Post by jacque » Sat Feb 13, 2021 10:34 pm

bogs wrote:
Fri Feb 12, 2021 9:32 pm
Like I said earlier, Jacque, my problem is probably in my understanding of a chunk, but,
In these cases we need to force evaluation.
Is there one sure fire way to force the evaluation whether required or not, that won't shoot off an error if used with one that doesn't? This I would like to know, for sure, as it would at least give me a consistent way of dealing with all of these I might come across.
I think you understand chunk expressions just fine. What was unclear is what the dictionary is referring to when it says a chunk is returned. The return value is itself a chunk. It doesn't refer to the value the chunk references.

One way to ensure evaluation is to use the value function: put value(the selectedline). This works on most chunk expressions. The only thing to remember is that this loads the compiler which involves overhead. If there's another way to get the same result without using "value" (or "do") then that's preferable.
I also don't understand why the engine can 'deal with it' if I stick it in a variable, but can't deal with it using the return itself (which I thought was nothing more than a variable anyway). :?
Putting a chunk expression into parentheses or into a variable can also (but not always) force evaluation; i.e., the compiler will type the chunk. One example is URLs.

Code: Select all

answer file "Choose a file:"
get url "file:" & it -- this is just a string, can't be used as a URL, so just returns the string "file:myfile.txt"

answer file "Choose a file;"
get url ("file:" & it) -- now it's a url and returns the content of the file

answer file "Choose a file:"
put it into tURL -- forces evaluation
get url tURL -- this works and returns the content of the file
Just to throw a spanner into things:

Code: Select all

put url value("file:" & it)
put value("file:" & it)
both throw an error.

To go back to your original question -- how do you know? -- I think the only answer is to understand what type the engine currently thinks the value is. The operators "is a" and "is strictly a" will tell you that if you don't want to consult the dictionary. One of the best things about LC is that you don't need to worry about variable typing, but it does cause these misunderstandings sometimes.
Well, I know your not. For one thing, I'm taller than you :twisted:
Everybody is taller than me!
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to tell what a function is returning

Post by bogs » Sat Feb 13, 2021 10:56 pm

jacque wrote:
Sat Feb 13, 2021 10:34 pm
To go back to your original question -- how do you know? -- I think the only answer is to understand what type the engine currently thinks the value is. The operators "is a" and "is strictly a" will tell you that if you don't want to consult the dictionary. One of the best things about LC is that you don't need to worry about variable typing, but it does cause these misunderstandings sometimes.
Well that is something I sure didn't know, thanks a bunch !! As far as the variables go, sometimes I think I'm better off if I pick them myself. I don't think I had this hard a time learning Delphi (around v1 to 2), and you had to not only setup the variable then, you had to tell Delphi how long it was going to be to boot (for strings). I think I may have to go check whether or not you still do heh.
Well, I know your not. For one thing, I'm taller than you :twisted:
Everybody is taller than me!
No, I'm pretty sure my wife is shorter than you, and she is getting shorter by the minute (as I so often tell her, "...not just in temper" :twisted: )
Image

Post Reply