Type()

Type()

Retrieves the type of an expression

Syntax

      Type( <cExp> ) --> <cRetType>

Arguments

<cExp> must be a character expression.

Returns

<cRetType> a string indicating the type of the passed expression.

        <cRetType>   Meaning

        "A"          Array
        "B"          Block
        "C"          Character (string)
        "D"          Date
        "L"          Logical
        "M"          Memo
        "N"          Numeric
        "O"          Object
        "P"          Pointer
        "S"          Symbol
        "U"          NIL, local or static variable, or not linked-in function
        "UE"         syntax error in the expression or invalid arguments
        "UI"         function with non-reserved name was requested

Description

This function returns a string which represents the data type of the argument. The argument can be any valid Harbour expression. If there is a syntax error in passed expression then “UE” is returned. If there is a call for any non-reserved Harbour function then “UI” is returned (in other words there is no call for passed UDF function during a data type determination – this is CA-Cl*pper compatible behavior). Additionally if requested user defined function is not linked into executable then “U” is returned.

The data type of expression is checked by invoking a macro compiler and by evaluation of generated code (if there is no syntax errors). This causes that Type() cannot determine a type of local or static variables – only symbols visible at runtime can be checked.

Notice the subtle difference between TYPE and VALTYPE functions. ValType() function doesn’t call a macro compiler – it simply checks the type of passed argument of any type. Type() requires a string argument with a valid Harbour expression – the data type of this expression is returned.

Examples

      ? Type( "{ 1, 2 }" )                                // prints "A"
      ? Type( "iif( .T., SubStr( "TYPE", 2, 1 ), .F. )" ) // prints "C"
      ? Type( "At( "OK", MyUDF() ) > 0" )                 // prints "UI"
      ? Type( "{ 1, 2 }[ 5 ]" )                           // prints "UE"

      //--------------------------------------------------------

      LOCAL c
      PRIVATE a := "A", b := "B"
      ? Type( "a + b + c" )     // prints: "U" ('C' variable is a local one)

      //--------------------------------------------------------

      LOCAL cFilter := Space( 60 )
      ACCEPT "Enter filter expression:" TO cFilter
      IF Type( cFilter ) $ "CDLMN"
         // this is a valid expression
         SET FILTER TO &cFilter
      ENDIF

Compliance

– Incompatibility with CA-Cl*pper: In the following code:

PRIVATE lCond := 0 ? Type( "iof( lCond, 'true', MyUDF() )" )

CA-Cl*pper will print “UE” – in Harbour the output will be “UI”

– If “UI” is returned then the syntax of the expression is correct. However invalid arguments can be passed to function/procedure that will cause runtime errors during evaluation of expression.

– Harbour supports two new types (Pointer and Symbol) which does not exists in CA-Cl*pper.

Files

Library is core

Seealso

ValType()

3 responses to “Type()

  1. Pingback: How I can check validity of a macro ? | Viva Clipper !

  2. Pingback: Harbour All Functions – T | Viva Clipper !

  3. Pingback: Variable Management Functions | Viva Clipper !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.