« Variable Assignment | Main | Basic Math Operations »

I Object

Smallworld Magik is an object oriented language, so almost everything you deal with is an object.  Some examples of objects:

  • Integers (1, 13, 2008)
  • Floating point (1.0, 3.1415, 2e3, 0.57721)
    • Note: the e in 2e6 is 2x10^6, not 2*e^6 where e = Natural Logarithmic Base
    • Note: 0.57721 works while .57721 will fail
  • Character( %c, %l, %t)
    • Note the % denotes a character object

There are a few special objects as well.  The keywords _unset, _true, _false, and _maybe are all objects and can be treated as such.

The string “It’s the end of the world as we know it.” is actually an array of characters.  Knowing this, we can treat it as a collection without having to parse the string.  As objects, they can be compared with each other.  This code returns the string in alphabetical order, with weight given to punctuation, and capitalization first.  This is accomplished through Magik’s _cf comparison which evaluates characters based on their ASCII values. 

Magik2> "".new_appending(_scatter  sorted_collection.new_from("It's the end of the world as we know it.").as_simple_vector())
$
"         '.Iaddeeeefhhiklnnooorssttttwww"

Or reversed:

Magik2> "".new_appending(_scatter  sorted_collection.new_from("It's the end of the world as we know it.").as_simple_vector()).reversed()
$
"wwwttttssrooonnlkihhfeeeeddaI.'         "

We create an empty string “” and append to it the result of the as_simple_vector method on the sorted collection we created from our string.  Simplicity defined.

In the next example we create a string object and assign it to tmp1.  We then assign the tmp1 to tmp2.  In this case tmp1 and tmp2 don’t just have the same value, they are the same object.

Magik2> tmp1 << "Hello"
$
"Hello"
Magik2> tmp2 << tmp1
$
"Hello"

This would not be true in the next case.  Both tmp1 and tmp2 are assigned the same value, but they are not the same object. 

Magik2> tmp1 << "Hello"
$
"Hello"
Magik2> tmp2 << "Hello"
$
"Hello"

Final notes on the subject of objects:

  • Variable assignment doesn’t copy, it references. 
  • Variable assignment is NOT strongly typed so you don’t need to declare it first, just assign a variable to an object, be it a number, a string, an array, a collection, or even a running application.

TrackBack

TrackBack URL for this entry:
http://www.thetatons.com/magiktricks-mt/mt-tb.fcgi/4

Post a comment