8th Tutorial

Lession 5: Data types in 8th

Note: if you haven’t yet installed 8th, please do so right now and follow the installation instructions as presented in the manual!

Simple data types

You’ve already seen numbers in 8th. What you haven’t seen is that numbers come in a variety of “flavors”. So for example, 10, 3.14, 1.23e10 and 234235123452345234523452345234523452345234534 are all acceptable numbers to 8th. Read the manual for more detail on what constitutes a “number” as far as 8th is concerned.

Another simple data type is called a “string”. That’s just a sequence of characters, which is entered by starting with a double-quote, typing some characters and then ending with a double-quote:

"this is a string"
You have seen how to print a number using the . word. Don’t be too surprised to learn that it works on strings as well:
. cr
this is a string
When printed, the double-quotes are not printed — since they are not actually part of the string itself. They are used to let 8th know that you intend to input a string.

Another basic type is the “variable”, which is used to store any other 8th type. You declare a variable using the word var followed by the name by which you want to invoke it. You then save a value into it using ! and retrieve a value from it using @:

var somevar 123 somevar ! somevar @ . cr
123
"hello" somevar ! somevar @ . cr
hello
You’ve just seen that an 8th variable does not have an intrinsic type, unlike what you are used to from most other languages. Instead, a variable is just a “container” which can hold onto another data item. Bear this in mind as you continue working with 8th.

Also bear in mind that the “name” of the variable refers solely to the variable, and not to its contents! That means that the value “123” is not called “somevar”, it simply resides (temporarily, as it happens) in the variable which is named somevar.

One special data type is null — which is a value which means, variously, “nothing to see here” or “don’t know what to do”. It can be a valid data value, e.g. if you really don’t know what a value should be. However, it is also returned from a number of words as a “soft failure” indicator. For example, if you try to access some hardware device which doesn’t exist or isn’t available just now. You check for “null” using the word null?

Exercises

Try the following:

  • Create a variable called “x” and store the string “Hello, world!” into it
  • Now store the number 3.14159 into “x”
  • Now retrieve the value of “x” and print it
  • What do you think happened to the string you originally put into “x”?
  • What happens if you type "dogs and" "cats" + . cr ?

Conclusion

You’re now familiar with numbers, strings and variables. You should read the manual to learn more about what you can do with those data types.