Note: if you haven’t yet installed 8th, please do so right now and follow the installation instructions as presented in the manual!
What’s in a name?
In the previous lesson we presented a teaser. In the “exercises” we showed you that the word + joins two strings together in addition to its ability to add two numbers together. Now we’ll explain what is really going on.
In truth, the word + is what’s called an unqualified name. There are actually quite a few words named “+”, as you can see by typing help + in the 8th console. If you look at the output from “help”, you’ll notice that the first line in each entry starts with “ns:” followed by a space and then some text. That text is the “namespace” of the word in question.
A namespace is a logical grouping of related words. So in the “n” namespace you will find words which relate to numbers. In the “s” namespace you will find words which relate to strings, and so on.
In the console, if you issue an unqualified name then 8th looks at the item in TOS and sees if there is a word with the name you invoked, in the namespace of that item. If there is, it’s invoked; if there isn’t, 8th will continue looking for a matching name. For details on how names are looked up, read the manual.
The “G” namespace is the “global” namespace. It is where general-purpose 8th words are found. The “user” namespace is the one your own words and variables are created in, by default.
Because it is quite possible that the item on TOS is not from the correct namespace, you are encouraged to use qualified names. That means that you prefix the word name with the namespace and a colon, like so:
An aside: “exceptions” in 8th are thrown when something very bad happens. In the example we just gave, you tried to operate on a number as if it were a string. Since 8th does not convert data types automatically, it cannot do what you asked. In fact, what you told it to do is probably a severe programming error, and so 8th throws an exception.
Unlike languages like Java and C++, 8th assumes that exceptions are fatal errors and they normally terminate the program. If you happen to be running in the interactive console, an exception will not normally terminate 8th because it is assumed you simply mistyped something. However, garbage may be left on the stack: so if you do get an exception while tinkering in the console, use .s to see what’s on the stack before continuing. And don’t forget that you can use the console editing keys to correct your mistakes!
Try the following:
- Type words/ s:
- You might have guessed that 8th showed you all the words in the “s” namespace. Use “help” to find out what some of those words do.
- Type words/ + and note that all “+” words are shown. Use help to understand what some of them do.
- Why do you think 8th uses “+” for different data types and actions?
- Do you have to use “G:” all the time? Why not?
You’re now familiar with namespaces and exceptions. Read the manual to learn more about what exceptions 8th can throw, and what namespaces are available to you.