Go to main content

type

This function return the type of a variable.

TYPE

NAME

type - This function return the type of a variable.

SYNOPSIS

variable.type() or type(variable)

DESCRIPTION

The function return a string representation of the type of the specify variable.

See in example "Class special case" for how to handle Class types.

The following values can be return:

  • type = "undefined";
  • type = "int";
  • type = "float";
  • type = "string";
  • type = "null";
  • type = "pointer";
  • type = "buffer";
  • type = "reference";
  • type = "context";
  • type = "array";
  • type = "bool";
  • type = "c function";
  • type = "user function";
  • type = "class";

PARAMETERS

Variable

RETURN

String representation of the type

EXAMPLES

Primitive types

// Undefined variable check
type(bar); // undefined
foo.type(); // undefined

// String check
my_str = "Allo";
type(my_str); // string
my_str.type(); // string

// Integer check
my_int = 12;
type(my_int); // int 
my_int.type(); // int

// Float
my_flt = 1.2;
type(my_flt); // float
my_flt.type(); // float

// Bool
my_bool = true;
type(my_bool); // bool
my_bool.type(); // bool

// Null
my_null = null;
type(my_null); // null
my_null.type(); // null

// Array
my_arr = ["Allo", "Monde"];
type(my_arr); // array
my_arr.type(); // array

// Context
my_ctx = {"att": "ribute"};
type(my_ctx); // context
my_ctx.type(); // context

// User function study
function main()
endf

type(main); // user function
main.type(); // user function

// Language reserved word
type(exec); // c function
exec.type(); // c function

Class special case

class My_class
    
    method My_class(param)
        this.param = param;
    endm
endc
my_instance = new My_class("Allo");

type(my_instance); // class
my_instance.type(); // WATCH OUT!! Sncode error page: "This method 'type' is not defined for class 'My_class'."

// Class with method `type` defined
class My_class
    
    method My_class(param)
        this.param = param;
    endm
    
    // Defined a function type
    method type()
        return classtype(this);
    endm
endc
my_instance2 = new My_class("Allo2");

my_instance.type(); // My_class <- DIFFERENT!
type(my_instance); // class <- DIFFERENT!

MODIFICATIONS

2023-10-31 guillaume@sednove.com

Edit

© 2024 extenso Inc. All rights reserved.