Page 1 of 1

What is the difference between....

Posted: Wed Dec 04, 2013 5:20 pm
by chris25
Dictionary - empty constant
I know I am living in Ireland but.....if I script this: local sVar = 'empty' or 'false' or "" and then I read this I am slightly lost.

Putting empty into a field or variable clears it. (Ok good, understood) I thought this would have been enough...

Putting empty into a nonexistent variable creates the variable with no content. This is a philosophical playground over wine and cheese, anyone care to join me?...

Re: What is the difference between....

Posted: Wed Dec 04, 2013 5:57 pm
by Klaus
Hi Chris,
chris25 wrote:Dictionary - empty constant
I know I am living in Ireland but.....if I script this: local sVar = 'empty' or 'false' or "" and then I read this I am slightly lost.
hm, EMPTY <> FALSE
But EMPTY = ""
chris25 wrote:Putting empty into a nonexistent variable creates the variable with no content.
Come on, Chris you really need a bit more imagination! 8)

What about this "real life" example, I am sure you will be able to translate this to some real Livecode problem 8)
When do you decide to buy new food?
I bet it is something like this:

Code: Select all

...
if TheFridge = EMPTY then
   buy_new_food
   exit to top
end if
if the num of items of TheFridge = 1 then
   put item 1 of TheFridge into tMouth

   ## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   put empty into TheFridge
else
   put item -1 of TheFridge into tMouth
end if
send "check_fridge" to me in 60*60*24 secs
...
If TheFridge is never MARKED as EMPTY, you will not buy any food and starve and you don't want that! :D


Best

Klaus

Re: What is the difference between....

Posted: Wed Dec 04, 2013 6:44 pm
by dunbarx
I thought that Klaus used Kühlschranks, not fridges.

This may throw a monkey wrench into your thinking:

Code: Select all

on mouseUp
   put empty into temp
   add 2 to temp
   answer temp
end mouseUp
You get "2". Same as if you started out by putting "0" into temp. HC did this as well, allowing one to add a number to empty, as if it was nought. But at least temp was in fact created.

Now then. You cannot do this:

Code: Select all

on mouseUp
   add 2 to temp
   answer temp
end mouseUp
Because temp does not exist as of yet, LC pouts. So there is a big difference between an empty variable and no variable at all.

Craig

Re: What is the difference between....

Posted: Wed Dec 04, 2013 6:59 pm
by Klaus
:D :D :D

OK, the plural is actually "Kühlschränke", but I did not want to confuse Chris more than neccessary! 8)

Re: What is the difference between....

Posted: Wed Dec 04, 2013 7:20 pm
by chris25
You know, you guys can make my question sound as if I had no need to ask it. :) But the reality is you are answering the question, or should I say more accurately, understanding the definition in the dictionary from a point of perspective of extensive experience and knowledge. I read that statement and am left with ugh, because I have not yet learned to translate English into programmers English into "ah, that makes perfect sense" ...not yet - I'm working on it.

Now actually your answers really have made things very clear, so that when somebody else asks this same question, I will be able to also say: "What? it's so easy how can you not understand that"? :)

All in all , thankyou, yes I mean that. Your examples both were actually fun to read, and really now it does make it clear. Though one important point, and I have to blame someone else for this - sorry :) but one of the first things I read and actually read more than once was this: A variable is created at the moment it is given a value... this for me was clear. So in Craig's second example: "" ad 2 to temp, but temp does not exist"" clearly needs re-adjusting perhaps? not Craig, but the instructional teaching about the variable I mean.

German plurals? - no problem Klaus :D (Koelkasten) loved your story.
regards
chris

Re: What is the difference between....

Posted: Wed Dec 04, 2013 7:24 pm
by Klaus
Well:

Code: Select all

on mouseUp
   add 2 to temp
   answer temp
end mouseUp
actually displays 2 in the answer dialog!? 8)

Re: What is the difference between....

Posted: Wed Dec 04, 2013 7:34 pm
by dunbarx
Chris.

The code starts to execute. The very first thing asked of the engine is to add "2" to (what seems like) a variable name, but is NOT a variable reference, since no variable with that name exists.

So "putting" something into a variable does indeed create it, and at the same time loads it. But "adding" a value to a variable requires that variable already to exist. It does not. Just be happy, unlike most other languages, that variables do not have to be explicitly declared (or typed). Unless explicitVariables is turned on, you just create them on the fly. We love this.

This is not an issue with making a new custom property, for example, since the only way to create and load such a thing is by using "set". This is analogous to using "put" for a variable. Note that you cannot "put" anything into a custom property at all. It is not a container, it is a property. Take a script for example. You can set a script to something, but you cannot put anything into it. It is a property.

Craig

Re: What is the difference between....

Posted: Wed Dec 04, 2013 7:52 pm
by chris25
I thought putting something into a variable and giving it a value meant exactly the same thing even though you are doing two different things, the definition of value is literally anything that a variable contains? A string of words , letters, text or numbers.
kind regards

chris

Re: What is the difference between....

Posted: Wed Dec 04, 2013 10:18 pm
by dunbarx
I thought putting something into a variable and giving it a value meant exactly the same thing even though you are doing two different things, the definition of value is literally anything that a variable contains? A string of words , letters, text or numbers.
kind regards
If you put something (even empty) into a variable, whatever you put is the value. Putting and what you put are rolled up together, and the simple act of putting will create a new variable if one does not already exist.

In other words, you can declare a new variable, which will create it with empty as a value (what else could it contain?) :

Code: Select all

local xx
on mouseUp
 if xx = "" then answer "kk"
end mouseUp
or you can put something into it:

Code: Select all

on mouseUp
put "" into kk
 if xx = "" then answer "kk"
end mouseUp
But if you do this:

Code: Select all

on mouseUp
   answer gg
end mouseUp
LC will have to assume gg is a literal, since nobody in recent memory ever created a variable by that name, and it has no other options.

Craig

Re: What is the difference between....

Posted: Thu Dec 05, 2013 3:30 pm
by [-hh]
..........

Re: What is the difference between....

Posted: Thu Dec 05, 2013 4:31 pm
by chris25
Hermann - I had no idea that 'empty' could be so 'full' :? Congratulations - I need more wine...forget the cheese. :D

Re: What is the difference between....

Posted: Thu Dec 05, 2013 7:47 pm
by jacque

Code: Select all

10 (empty is in empty) = false
11 (empty is not in empty) = true

12 (empty is in (empty & "A")) = false
13 (empty is not in (empty & "A")) = true
I think this is due to the nature of the parser. The first "empty" is being defined as an unquoted literal, and "empty" is not in empty. I'm not sure there is a way around that, given that the language is untyped.

Re: What is the difference between....

Posted: Thu Dec 05, 2013 7:59 pm
by dunbarx
Jacque,

Would the parser take a reserved constant and interpret it as a literal?

This works:

if colon is in ":" then answer "Yep" --Yep

Empty is an odd duck, like quote. Hard to isolate.

Craig