Defining and using NEWTYPEs
 

NEWTYPEs are exactly that - a way of defining new types in addition to the six basic types (byte, word, long, quick, float and string) already available in Blitz. Newtypes are structures built out of the basic types and other already defined newtypes.

Newtypes are usually used to create a type which allows you to store different but related information in a single variable. A typical use for this would be a record in a database - you need to store different information (name, address, books borrowed, etc.) that is related (all that information would be for one person). The easiest way to do this would be to create a newtype to allow all this information to be stored in one place (i.e. in one variable).

The first thing to do to be able to use a newtype is to create one. To start the definition of a newtype, you use 'NEWTYPE.<new_type_name>'. The type name must be unique, although for good programming practice you should give it a meaningful name. To end a definition, you use 'End NEWTYPE'.

Code ; Begin new type definition
NEWTYPE.mytype
  ; Contents of newtype defined here - see below
End NEWTYPE
To define the contents of the type, you must define the variables (and their types) inside the NEWTYPE...End NEWTYPE lines. These variable definitions must be in the form 'variable_name.type' and not using the DEFTYPE command. These variables within the newtype are often called the fields of the newtype. You can even use newtypes as the type of a field, but it must have already been defined before you use it. You can use the colon character to put multiple fields on one line - doing this also has the advantage that you may omit the type of the second, third, fourth, etc fields and they will be given the same type as the first field on that line. The same suggestion about field names apply from the variables topic - keep the names meaningful and consistent.
Code ; Begin example newtype definition
NEWTYPE.person
  ; Defining some fields in this newtype
  name$                ; Person's name
  age.w                ; Their age in years
  height.q             ; Height in metres
  ; Defining fields without the type - month and year are defined as .w (same as first field)
  day.w : month : year ; Date of birth
End NEWTYPE

NEWTYPE.anothertype
  ; Using an already defined newtype inside another newtype
  previous.person
End NEWTYPE
You will have noticed that inside the newtype definition, each line was indented by a couple of spaces. This is another point about keeping code readable - indentation. It makes the contents of a block of code (in this case the code between the NEWTYPE...End NEWTYPE) more readable, and you can see that it is all related. The amount you indent by does not really matter, as long as you keep it consistent.

You can even put arrays inside newtype definitions, although this will be discussed in later pages of this topic. Now that you know how to define a type, you need to be able to use it. Defining a variable with your newtype is exactly the same as defining a variable with one of the basic types, except you substitute your type name instead of the basic type name. To recap, you can either use DEFTYPE or simply use the variable for the first time. However, if you are using the variable for the first time, you will probably need to access one of the fields or it will not make much sense. To access the fields in a newtype, you use the format 'variable_name\field_name' and you never include the type on the end of a field, not even when you defined a string using the dollar sign at the end:

Code ; Begin example newtype definition
NEWTYPE.person
  ; Defining some fields in this newtype
  name$                ; Person's name
  age.w                ; Their age in years
  height.q             ; Height in metres
  ; Defining fields without the type - month and year are defined as .w (same as first field)
  day.w : month : year ; Date of birth
End NEWTYPE

NEWTYPE.anothertype
  ; Using an already defined newtype inside another newtype
  previous.person
End NEWTYPE

DEFTYPE.person Fred ; Create a variable of our own type
Fred\name = "Fred Smith" ; Notice no $ symbol on the end of the string!
Fred\age = 23

Bob.person\name = "Bob Roberts" ; Create a variable of our own type by using it

DEFTYPE.anothertype blah
blah\previous\name = "Sue" ; Accessing a field within a newtype within a newtype
So that's how to use newtypes with variables. The main points to remember are (in no particular order):
!
  1. Newtypes can be created from a combination of basic types and other newtypes that have already been defined
  2. Each field in the newtype must have a type when you define it, except fields separated by colons which use the first type on that line (optional)
  3. Never ever use the type suffix when you are using the field, not even if you have defined a string with a dollar sign
  4. Each field in the newtype is a variable, and so can have all the usual operators applied to it.
  5. You access fields within a newtype by using the format 'variable\field'


Previous Programming contents Next: Arrays and Lists of newtypes


News | Introduction | Search | List/Forum/IRC Webring | Contact
Installation | Troubleshooting | Programming | Archives | Links
Return to home page
Page last modified: 14th September 2001