Problem naming groups with integers

Richard Gaskin ambassador at fourthworld.com
Sat Apr 19 17:19:04 EDT 2014


Stephen MacLean wrote:

 > So, even though I'm enclosing it in quotes, LC still thinks that?
 >
 > Strange behavior

It seems strange if you're used to working with languages that support 
explicit data types.

Generally speaking, LiveCode is a typeless language.  Any value is 
considered a string, and converted to a number only when doing 
arithmetic on it, and again automatically converted back to a string 
when you do any string operation on it:

   put 123 into x    -- x is a string
   add 4 to x        -- x is converted to a number
   put x into fld 1  -- converted back to a string
   add 1 to fld 1    -- field 1 is retrieved as a string, converted to
   --                   a number for the arithmetic operation, then
   --                   converted back to a string for display
   put (fld 1) + 2 into x  -- x is a number
   put "string:" before x  -- converted to a string by the concatenation

In those few lines we had 8 type coercions, and unlike most languages we 
didn't have to manually handle any of them.

The upside to this is that we don't need to think about data types, as 
the engine handles the coercion for us automatically as needed.

It only becomes problematic when attempting to use strings consisting of 
only numeric characters as object names, and this is an issue only 
because LiveCode supports convenient syntax for addressing objects by 
ordinal number; e.g., to refer to the second field on a card you could 
write:

   get the long id of field 2

When the name is an integer, LiveCode has no way to know whether you're 
asking for the object by name or by ordinal number, since the two forms 
of syntax as pretty much the same:

   get the long id of field MyFieldName

Putting quotes around values doesn't coerce type, since the language is 
inherently typeless by nature.  Whether comprised of alphanumeric 
characters or solely numeric characters, a string is a string.

Quotes are helpful for readability, and to help the engine distinguish 
between values which might also be variable names or reserved words.

One useful thing about quoting is that it not only makes code more 
readable to you and clearer to the engine, but since quoted strings 
can't be variable names or reserved words, they can give you a modest 
performance boost, since the engine doesn't need to compare the string 
against lists of variables or language tokens.

But whether quoted or not it's still a string, and since objects can be 
referred to by ordinal number where the syntax is identical to referring 
to them by name, avoiding integers as names will keep the engine and you 
both happy.

In cases where you need to maintain some sort of integer value in the 
object's name, you could simply prepend the name with anything else, 
removing the extra stuff when you to obtain the number, e.g.:

   set the name of field 1 to "f1"
   --
   get the short name of field 1
   delete char 1 of it
   DoSomethingWithTheNumber it


--
  Richard Gaskin
  Fourth World
  LiveCode training and consulting: http://www.fourthworld.com
  Webzine for LiveCode developers: http://www.LiveCodeJournal.com
  Follow me on Twitter:  http://twitter.com/FourthWorldSys





More information about the use-livecode mailing list