Other useful newtype info and tricks
 

Arrays inside newtypes
Arrays inside newtypes are slightly different from normal arrays (and you cannot put lists inside newtypes). You must still give the array a name and type, the same way as all the other fields in a newtype, and you must not use the Dim command. Also, you must use square brackets for the array index, not parenthesis (rounded brackets). Finally, the index you specify when you create the array is not the maximum index number you can use for the array (as it is with normal arrays), it is the maximum number of items in the array. Using the arrays inside the newtype is the same as using any other field of a newtype, except you specify the index of the array in square brackets:
Code NEWTYPE.arrayexample
  values.w[10] ; We can access items in the array with indexes 0...9
End NEWTYPE

DEFTYPE.arrayexample foo
foo\values[5] = 69 ; OK
foo\values[10] = 123 ; Error!!!
Of course, apart from those differences, arrays inside newtypes are exactly like normal arrays and other fields of newtypes. That means they can be arrays of newtypes themselves, and you would access them as shown above followed by '\field', just like normal arrays.

Sizeof
The SizeOf command is a useful one for getting sizes of newtypes (or basic types if you wish) and also getting the offset (in bytes) from the start of the newtype to a specific field. So far, this offset has shown to be useful for the array and list sort commands, but there are many other uses. The structure of this command is 'SizeOf.type' and optionally 'SizeOf.type\field' and it will return the size of the type or the offset for the field in the number of bytes:

Code NEWTYPE.arrayexample
  values.w[10] ; We can access items in the array with indexes 0...9
End NEWTYPE

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

NPrint SizeOf.w ; Size of a word type
NPrint SizeOf.s ; Shows only the base size of a string, that is the 4 bytes which all strings require
NPrint SizeOf.arrayexample ; Gives the total size of the newtype, which is 5*2 (size of array * size of each array item)
NPrint SizeOf.person\height ; Gives offset of the height field (= size of all fields before it)

USEPATH
This command can be used to save yourself some typing. It allows you to specify a piece of a variable path (with newtype access in it) and whenever the compiler after that sees a variable which starts with the \ character (i.e. if you miss off the variable part from the front) it will automatically expand it to include the path you specify in the USEPATH command. For example:

Code 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

DEFTYPE.person foo ; Create our variable
foo\name = "Hugh" ; Jass ;)
foo\age = 24 ; years old
foo\height = 1.8 ; metres tall

USEPATH foo

NPrint \name," is ",\height," metres tall."
; In the above line, the compiler automatically took the path we specified in
; the USEPATH command (in this case, 'foo') and placed it in front of the variables
; we accessed starting with the \ character. This meant we were accessing the fields
; foo\name and foo\height in the NPrint line
MouseWait
End

Initialising a whole newtype in one command
In the previous example, it took three lines to initialise three fields of the variable foo. There is a shortcut which you can use to initialise the contents of a newtype in one statement. You must initialise the fields in order, and you cannot skip any fields in the middle of those you are initialising. The way to do it is to start off with the usual assignment of a value to a field, and then put a comma followed by the value for the next field, and so on, for all the fields you want to initialise. You can start from any field, but you must then carry on in order. So, we could have made the line in the above example look like this:

Code ; Initialising all the fields from the above example in one statement
foo\name = "Hugh",24,1.8
NPrint foo\name," is ",foo\age," years old and ",foo\height," metres tall."

foo\day = 25,12,1977 ; Initialises three other fields; you can start from any field but always initialises in order
NPrint "Date of birth: ",foo\day,"/",foo\height,"/",foo\year
MouseWait
End

Unions
You are very unlikely to see these, so feel free to skip this part as it is fairly complex, compared to the rest of the info presented here.


Previous: Arrays and Lists of newtypes Programming contents Next


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