Language Tips and Tricks - Basic

This page is intended for basic level programmers. See Advanced Magik Language for more advanced language uses.

proc()

To create a global variable that is to run a proc use the following

_global a_global_proc <<
_proc @a_global_proc(_optional args)   # The @ is a label
    # Do something
_endproc

A gotcha to look out for… do not put () after _endproc() or your global will be evaluated to whatever the proc returns when the proc is loaded. Although this may be useful ins some cases, it can cause grief to figure out why the global proc isn't a proc but a result.

Colours (Colors)

Determining predefined colours

Many of you know about the method colour.called() and you can pass in a symbol like :red, :black, :white, etc. But how do you know what colors are already defined? Well, it is private constant called colour_table. So you can do a print or loop over colour.sys!perform(:colour_table).

Sets, Ropes, Vectors…

There are several types of "collections" (not to be confused with database collections which is a type of collection). Most collections can have any type of object as elements. Some exceptions are database collections, and specific typed vectors such as byte_vector and integer_vector.

  • simple_vector - fixed length, ordered collection defined with elements using {} notation.
  • rope - a variable length ordered collection.
  • set - a variable length, non ordered collection
  • equality_set - variable length, non ordered collection of unique values.
  • limited_stack - fixed length, ordered, where once filled, 1st in is dropped when object is added.

Database Collections

More Info From describe_fields()

These changes will show the size of the selection or db_rope within describe fields

Loops

Gather Tuples (loop variables)

If you are unsure (or don't care) what is being returned from the loopbody you can use _gather on the _for statement.

_for _gather vals _over {:a,:b,:c}.fast_keys_and_elements()
_loop
  show(_scatter vals)
_endloop

Formatting

Date Format

You can use date_time_format.new() with :date_format_string to develop a format you desire. Example
date_time_format.new(:date_format_string,"#Y#m#d").format(date_time_now())
Result: "20210929"

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License