RPL types

Discussions about the RPL and RPL+ programming languages

RPL types

Postby oliver » Fri Jul 27, 2012 7:18 am

ND1 has two principal commands to inquire the type of an object: TYPE (or VTYPE) and @typeof.
The former two report a type number that is largely compatible with the HP-50g TYPE command, whereas the latter reports the MorphEngine "modern" type name as a string.

Dave (d_motto) graciously started a table that lists the correspondences.
Here's an extended version:
Code: Select all
TYPE | @typeof | 50g name
---------------------------------------------------------------------
0 number - Real Number
1 complex number - Complex Number
2 string - String
3 vector - Real Array
4 vector - Complex Array
5 vector - List
6 expression or name - Global name
7 expression or name - Local Name
8 RPL program - Program
9 expression or name - Algebraic Object
10 binary number - Binary Integer
11 img - Graphics Object
12 tagged - Tagged Object
13 n/a - Unit Object
14 n/a - XLIB Name
15 n/a - Directory
16 n/a - Library
17 n/a - Backup Object
18 operator - Built-in Function
19 n/a - Built-in Command
20 n/a - System Binary
21 n/a - Extended Real
22 n/a - Extended Complex
23 n/a - Linked Array
24 n/a - Character
25 n/a - Code Object
26 n/a - Library Data
27 n/a - Minifont
28 bignum - Real Integer
29 n/a - Symbolic Vector/Matrix
30 n/a - Font
31 n/a - Extended Object


As you see, most basic types have a direct correspondence, but there're a good number of 50g types "not applicable" (not available) in ND1. All of these, except Unit Object, are likely to remain not applicable or unavailable.

By the same token, there's a number of ND1 types that don't exist on the 50g:
Code: Select all
fraction [i](though equivalently available as TYPE 9 in 50g)[/i]
boolean [i](though equivalently available as TYPE 0 in 50g)[/i]
matrix [i](though effectively a TYPE 3, 4, or 5)[/i]
bigfloat
CF (ContinuedFractionType)
function (JS function)
URL (http:)
dataURL (data:image[i]; e.g. screenshots[/i])
code (Code)
object (JS Object)
undefined (JS undefined type)


and a growing list of custom types from optional extensions:
Code: Select all
color (Color)
chem (ChemicalFormula)
GS (GolfScript)


The "classic" TYPE code is part of the optional properties of a custom type object.
For example, the BigInt class/type begins like this:
Code: Select all
var BigNum = {
   type: "bignum",
   typeHP: 28,
   ...

Hence, the modern name is "bignum" and 28 is the classic code.

If someone wanted to re-create a classic object like 26 (Library Data), they could create a class in JavaScript that implements it and automatically blend their classic type code into the system by setting this property to 26.


Implications of array unification

Because of the unification of lists, vector, matrices in ND1 to a single array/vector type, each of these is reported as "vector" by @typeof. However, special logic in TYPE will differentiate them into different type numbers.
For example,
[3 4 5] will report as TYPE 3 (Real Array)
[(3,4) 4 5] will report as TYPE 4 (Complex Array)
[3, "Hi", (4,5)] will report as TYPE 5 (List)
This is done for backwards compatibility to 50g code.

The recommendation is to use @typeof in new code as it makes for more readable code and covers data types that TYPE doesn't cover because there's no equivalent. If you need to differentiate between different kinds of arrays, though, you may want to use TYPE.


Argument type checking

To perform argument type checking, you can use Tools.checkType, which will throw an error if the given argument is not of the type with the modern (that is, @typeof-style) name also given.
For example:
Code: Select all
≪ =s s "string" Tools.checkType ... ≫

or, equivalently,
Code: Select all
≪ =:s "string" Tools.checkType ... ≫

or, old-school,
Code: Select all
≪ ->s ≪ s "string" Tools.checkType ... ≫ ≫


This will only check for one type. To allow multiple types, do something like this:
Code: Select all
≪ =x
   x @typeof =type
   IF "number" type !=
       "bignum" type !=
       AND
   THEN "wrong type of argument" DOERR END
   ... @ do something with x, being of number of BigInt type



Argument type checking in JavaScript is discussed in this tech note: http://forums.naivedesign.com/viewtopic.php?f=2&t=668
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to RPL / RPL+

Who is online

Users browsing this forum: No registered users and 1 guest

cron