Equivalent of classes in LiveCode

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: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Brother Bill
Posts: 9
Joined: Sat Jul 11, 2020 6:51 pm

Equivalent of classes in LiveCode

Post by Brother Bill » Tue Jul 14, 2020 1:20 pm

In other development languages, such as JavaScript or C#, there exist classes, such as Employee, where an Employee is a Person (First name, last name, birthday) and an Employee (hire date, salary, position title, boss, subordinates?). And a boss could have an array of Employee's who report to her.

In Pseudo JavaScript

Code: Select all

let bob = new Employee("Bob", "Smith", "2000-01-01", "2020-01-04", 80000, "Marketing", "Belinda Heffenrucker")
let sally = new Employee("Sally", "Ride", "1980-04-11", "2018-01-04", 60000, "Sales", "Belinda Heffenrucker");
let my_boss= new Employee("Belinda", "Heffenrucker", "1960-05-15", "1992-12-17", "Management", "Big Boss", [bob, sally]);
What is the equivalent to porting this to LiveCode?

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

Re: Equivalent of classes in LiveCode

Post by bogs » Tue Jul 14, 2020 1:53 pm

While probably not what you are looking for specifically, I'd suggest using custom properties and an array , or even just cps alone. CPs are easy to set and get information from, arrays are indexed.

You can also put arrays into cps, and retrieve them from cps. CPs are constructed just like arrays as well, and so are very easy to parse and keep track of. You have a key, and contents, like so:
aPic_classVsCp.png
Your version will not look like this....
In the dictionary, type "custom" in the search and the top returns should all be related to keys, properties, & property set(s).

*Edit - Here is what it would look like in the current version of Lc -
aPic_classVsCp2.png
Set the keys, add the values...
Image

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

Re: Equivalent of classes in LiveCode

Post by dunbarx » Tue Jul 14, 2020 3:05 pm

What Bogs said.

But do you know how custom properties work? They are always associated with an object, not a variable. So you could create a card with the following:

Code: Select all

set the bob of card "classes" to "Bob", "Smith", "2000-01-01", "2020-01-04", 80000, "Marketing", "Belinda Heffenrucker"
set the sally of of card "classes" to ("Sally", "Ride", "1980-04-11", "2018-01-04", 60000, "Sales", "Belinda Heffenrucker")
etc.
and

Code: Select all

put the sally of of card "classes" into yourVariable
That card becomes your repository for all such data. You can similarly use any object, like a button or stack, if that suits you.

As Bogs mentioned, it might be more compact to place the property data in an array, but that is a matter of style.
Craig

Brother Bill
Posts: 9
Joined: Sat Jul 11, 2020 6:51 pm

Re: Equivalent of classes in LiveCode

Post by Brother Bill » Tue Jul 14, 2020 3:40 pm

Does LiveCode have similar support to inheritance? I'm trying to grok how the basic objects work.
In other languages, you have scalars such as numbers, bool, string, date, and structures such as arrays and hashmaps, each structure can be deeply nested.

I see the array of items. Can item be anything, including hashmaps?
Is there a hashmap equivalent?

As far as Custom Properties, I see these factoids.
1. They are attached to an object, such as field or widget, card or stack.
2. They can survive closing app and restarting it.
3. They can execute a function when get or set.

What I don't see with Custom Properties is ability to add FirstName, LastName, Birthdate in one custom property, or would these be 3 separate custom properties?

There should be a document explaining LiveCode for experienced developers, that takes what we know and guides us to learning LiveCode, given what we know about other languages.

JackieBlue1970
Posts: 64
Joined: Thu Jan 16, 2020 10:28 pm
Location: Max Meadows, VA USA

Re: Equivalent of classes in LiveCode

Post by JackieBlue1970 » Tue Jul 14, 2020 5:32 pm

Welcome. First, I encourage the more experienced LiveCode Coders to chime in on my post to correct any mistakes.

LiveCode is a bit different than your traditional OO language. I don't think of it has an OO language. The closest thing I can come up with is it is an event oriented language as events drive the language. That said, everything pretty much is an object in LiveCode as I understand it. As others have pointed out, each object has properties you can set. Each object then has events you can code for. The message path is the "order of operations" that are checked in an event. A lot of code will be at the card level. Think more of functions than code classes. This is my interpretation of things.

I don't think inheritance is not really a concept in LiveCode, at least in a traditional sense. I believe you can add custom properties to existing objects or create custom objects yourself. I'm not as experienced as others with LC so someone may be able to discuss it better.

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

Re: Equivalent of classes in LiveCode

Post by dunbarx » Tue Jul 14, 2020 6:02 pm

What I don't see with Custom Properties is ability to add FirstName, LastName, Birthdate in one custom property, or would these be 3 separate custom properties?

Code: Select all

on mouseUp
   set the bobSmith of this card to  "Bob,Smith,2000-01-01,2020-01-04,80000,Marketing,Belinda Heffenrucker"
end mouseUp
Once you have this, you can, if Bob gets demoted:

Code: Select all

on mouseUp
   get the bobSmith of this card
   put 60000 into item 5 of it
   set the bobSmith of this cd to it
end mouseUp
If you become fluent with arrays, you can assign the various values to elements of a single array, "BobSmith". A single custom property can then be created with the name of that array.This has the advantage that you can extract the "salary" of "BobSmith", change the value, and restore. Much easier to read and manage.

On another note, I would create a table field with all the data. This is easy to read and is, like a spreadsheet, tab and return delimited. No custom properties. You can find Bob Smith in the table and extract or modify any data under script control.

One day, you could do the same with a dataGrid, but I would stay away from that for a while.

Craig

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

Re: Equivalent of classes in LiveCode

Post by bogs » Tue Jul 14, 2020 7:06 pm

Heh, Craig, I loved your answer, but I have to wonder why, if Bob was demoted, you didn't take him out of marketing and put him in building maintenance :twisted:
Image

Brother Bill
Posts: 9
Joined: Sat Jul 11, 2020 6:51 pm

Re: Equivalent of classes in LiveCode

Post by Brother Bill » Tue Jul 14, 2020 7:40 pm

I see that bobSmith is represented by an array.

Code: Select all

 set the bobSmith of this card to  "Bob,Smith,2000-01-01,2020-01-04,80000,Marketing,Belinda Heffenrucker"

Code: Select all

   get the bobSmith of this card
   put 60000 into item 5 of it   
   set the bobSmith of this cd to it
My training says that writing code such as this is scary and bad. I need to know that item 5 means salary.
Isn't this kind of coding quite fragile?

Could bobSmith be represented by a JavaScript style object? If so, what would be LiveCode equivalent?
{
first: "Bob",
last: "Smith",
dob: "2000-01-01",
hired: "2020-01-04",
salary: 80000,
role: "Marketing",
boss: "Belinda Heffenrucker"
}

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

Re: Equivalent of classes in LiveCode

Post by dunbarx » Tue Jul 14, 2020 7:52 pm

Bogs.

You don't read Dilbert???

If you are demoted, you are taken out of engineering and put into marketing.

Craig

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

Re: Equivalent of classes in LiveCode

Post by dunbarx » Tue Jul 14, 2020 7:59 pm

Bill.
I see that bobSmith is represented by an array.
No.

My example intentionally did not use an array, just for clarity, to show how LC can access parts of a string using delimiters. An array example would be:

Code: Select all

on mouseUp
   put "Bob" into bobSmith[firstName]
   put "Smith" into bobSmith[lastName]
   put "2000-01-01" into bobSmith[birthDate]
   put "2020-01-04" into bobSmith[hireDate]
   put "80000" into bobSmith[salary]
   
   --multi-level
   put "Bob" into employees["bob"][firstname]
   put "Smith" into employees["bob"][lastName]
   
     answer  employees["bob"][lastName]
   breakpoint
end mouseUp
Try this in a button. Open the array variables in the script editor and see how they are made. It is a first demo at assembling data in an array. Multiple levels are available, as you can see above, so you can organize the array to you liking. I will bet this is what you have been after all along. There is no limit to the number of levels.

Craig

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Equivalent of classes in LiveCode

Post by SparkOut » Tue Jul 14, 2020 8:57 pm

Hi Bill

Not wishing to get too strange too soon, but as well as Custom Properties being able to hold an array, you can create CustomPropertySets which are .. er... sets of Custom Properties that you can apply to specific objects for specific cases, and as Craig mentioned, you can have arrays within arrays.

Now as far as inheritance goes, there is no directly inheritable custom property hierarchy that I know of in the Object Oriented way I expect you are used to. You already discovered setProp and getProp though I believe, and it is possible, though not as direct as you might wish, to use these (getProp really) to fetch "virtual" properties - by calculating or looking up in a template object. So you could potentially change the Department template's head from Belinda Heffenrucker to Angus McNorton and when you fetch Bob's "Boss" property it will return Angus without you having to edit.

So there is a lot of thought that needs to happen and a lot of learning, and no doubt you will bump against the limitations of the LiveCode structures as a direct translation from Object Oriented classes, but if you are prepared to rethink and relearn as you go, you can achieve "something like" what you currently understand. But you may (hopefully) find LiveCode a friendlier paradigm and discover another way to do what you want anyway, without those old thought processes.

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

Re: Equivalent of classes in LiveCode

Post by bogs » Tue Jul 14, 2020 9:37 pm

Craig and SparkOut are dead on, I purposely left multi dimensional arrays out of the equation initially, since
a.) I don't know your familiarity with them and...
b.) property sets are very similar, but far easier to visualize (since you can create them right in the inspector) and ...
c.) you initially asked about inheritance, which the closest I could think of would be cps, an array is an array no matter what language your in.

As far as knowing which item is which goes, you'd have to know that in any programming language far as I know, and this is no different in how you can structure it. For instance, a cp *could* be formatted like your first example, containing nothing more than ....

Code: Select all

"Bob", "Smith", "2000-01-01", "2020-01-04", 80000, "Marketing", "Belinda Heffenrucker"
although I would certainly suggest uniformity in data storage (each item except salary is a literal string, for instance)...

...or like your last example, where everything is on it's own line...

Code: Select all

first: "Bob",
last: "Smith",
dob: "2000-01-01",
hired: "2020-01-04",
salary: 80000,
role: "Marketing",
boss: "Belinda Heffenrucker"
without having to go to js or some other language. Format it as you like (within reason), and you can pull the information in either case in a number of ways. Craig rightly mentioned delimiters, but aside from that, you have chunks, offsets, lines, etc. or you can create your own function to store it, sort it, and pull it.
Image

Brother Bill
Posts: 9
Joined: Sat Jul 11, 2020 6:51 pm

Re: Equivalent of classes in LiveCode

Post by Brother Bill » Tue Jul 14, 2020 10:06 pm

I will digest this, and respond with some sample apps that demonstrate this.

I intend to make a free video tutorial with word document file, with LiveCode projects to aid current developers to transition to LiveCode.
When done, will request reviews of it, so it can be made better.

It appears that LiveCode can have arrays that are indexed by numbers and strings.

There are ideas and concepts in LiveCode that are unique to it, and need to be explained with what LiveCode newbies already know from prior languages.

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

Re: Equivalent of classes in LiveCode

Post by dunbarx » Wed Jul 15, 2020 2:41 am

There are ideas and concepts in LiveCode that are unique to it, and need to be explained with what LiveCode newbies already know from prior languages.
Yep. And that is why this forum is so much fun, and so useful.

Craig

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Equivalent of classes in LiveCode

Post by mwieder » Thu Jul 16, 2020 4:24 am

There are ideas and concepts in LiveCode that are unique to it, and need to be explained with what LiveCode newbies already know from prior languages.
Indeed. 8)

LiveCode's arrays are in other languages referred to as hashes or associative arrays.

If you need to keep track of bobsmith's salary you could do something like

Code: Select all

put 80000 into employee["bobsmith"]["salary"]
and then

Code: Select all

put employee["bobsmith"]["salary"] into tBobSmithsSalary
or even get fancier by using arrays of arrays

Code: Select all

put 80000 into bobsmith["salary"]
put bobsmith into employees["bobsmith"]

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”