13 January 2016

Lesson 8 – Variables in 8th

Two ways to initialize a variable

In 8th there are 2 words: var and var, which initialize a variable.

Retrieving a variable is done with the @ word.

var initializes a numeric variable giving it the initial value 0 (zero):
SED:  var \ <name> --

Example: 



 var, intializes a variable with the value in TOS at the time the variable is defined:
SED:  var, \ x <name> --

Example 1:



 So, x (from SED) has the value “my value” and <name> (from SED) is y.

Example 2:








Now we have redefined the variable y (in the same REPL session):

Note that:
8th explicitly shows that we overwrite the previous definition of the variable
we redefined the variable with an other datatype, which is not a problem in 8th

Setting variables

You can set an already defined variable to a given value, using the ! word.
Example:









Note again that a variable can take values of any 8th datatype!

Characteristics of 8th variables


  • 8th variables are “just containers”. Variables do NOT have a datatype. However the contents (or value) of variables always have a valid 8th datatype
  • 8th variables are ALWAYS GLOBAL to the namespace they are defined in
  • usage of variables should be MINIMIZED. In almost all cases you can do most of the work using the stack only.

More info about 8th

There is a central website for 8th here: http://8th-dev.com/

Video

Watch complementary video here

No comments:

Post a Comment