# Object

The **obj** type is used to store values of the following types - **int, bool, float, str, arr.obj, map.obj**. If no value is assigned to the object, then it is equal to **nil**. An object can be assigned values of a type that is different from the current one.\
The operators and functions for working with objects are described here.

* [arr( obj o ) arr.obj](/stdlib/obj.md#arr-obj-o-arr-obj)
* [arrstr( obj o ) arr.str](/stdlib/obj.md#arrstr-obj-o-arr-str)
* [bool( obj o ) bool](/stdlib/obj.md#bool-obj-o-bool)
* [bool( obj o, bool def ) bool](/stdlib/obj.md#bool-obj-o-bool-def-bool)
* [float( obj o ) float](/stdlib/obj.md#float-obj-o-float)
* [float( obj o, float def ) float](/stdlib/obj.md#float-obj-o-float-def-float)
* [int( obj o ) int](/stdlib/obj.md#int-obj-o-int)
* [int( obj o, int def ) int](/stdlib/obj.md#int-obj-o-int-def-int)
* [IsArray( obj o ) bool](/stdlib/obj.md#isarray-obj-o-bool)
* [IsMap( obj o ) bool](/stdlib/obj.md#ismap-obj-o-bool)
* [IsNil( obj o ) bool](/stdlib/obj.md#isnil-obj-o-bool)
* [item( obj o, int i ) obj](/stdlib/obj.md#item-obj-o-int-i-obj)
* [item( obj o, str s ) obj](/stdlib/obj.md#item-obj-o-str-s-obj)
* [map( obj o ) map.obj](/stdlib/obj.md#map-obj-o-map-obj)
* [obj( arr.typename a ) obj](/stdlib/obj.md#obj-arr-typename-a-obj)
* [obj( bool b ) obj](/stdlib/obj.md#obj-bool-b-obj)
* [obj( float f ) obj](/stdlib/obj.md#obj-float-f-obj)
* [obj( int i ) obj](/stdlib/obj.md#obj-int-i-obj)
* [obj( map.typename m ) obj](/stdlib/obj.md#obj-map-typename-m-obj)
* [obj( str s ) obj](/stdlib/obj.md#obj-str-s-obj)
* [Sort( arr.obj o, cmpobjfunc cmpfunc ) arr.obj](/stdlib/obj.md#sort-arr-obj-o-cmpobjfunc-cmpfunc-arr-obj)
* [str( obj o ) str](/stdlib/obj.md#str-obj-o-str)
* [str( obj o, str def ) str](/stdlib/obj.md#str-obj-o-str-def-str)
* [Type( obj o ) str](/stdlib/obj.md#type-obj-o-str)

## Types

### fn cmpobjfunc(obj, obj) int

The **cmpobjtype** function type is used to compare two objects. Functions of this type are used to sort objects in an array.

## Operators

| Operator                 | Result | Description                                                                                                     |
| ------------------------ | ------ | --------------------------------------------------------------------------------------------------------------- |
| \*obj                    | int    | If the object is *arr.obj* or *map.obj*, the number of items in the array is returned. Otherwise, it returns 0. |
| obj **?**                | bool   | Calls *bool(obj)*.                                                                                              |
| obj **=** arr.typename   | obj    | Assigning an array to an object.                                                                                |
| obj **=** bool           | obj    | Assigning a boolean value to an object.                                                                         |
| obj **=** float          | obj    | Assigning a decimal number to an object.                                                                        |
| obj **=** int            | obj    | Assigning a number to an object.                                                                                |
| obj **=** map.typename   | obj    | Assigning an associative array to an object.                                                                    |
| obj **=** obj            | obj    | Assignment operator.                                                                                            |
| obj **=** str            | obj    | Assigning a string to an object.                                                                                |
| obj **+=** obj           | obj    | Adding an object to an array of objects.                                                                        |
| obj **&=** obj           | obj    | Creates a clone of the object. The new variable will work with the same data set.                               |
| obj **\[** int/str **]** | obj    | Assign / get the value of an array by index. If the object is not *arr.obj* or *map.obj*, then an error occurs. |

## Functions

### arr(obj o) arr.obj

The *arr* function returns an array of objects. Object *o* must be an array, otherwise it returns an error. Calling the function does not create a new array, but returns the current array that contains the *o* object.

### arrstr(obj o) arr.str

The *arrstr* function converts an array of objects into an array of strings. The *o* object must be an array, otherwise an error is returned. The function returns the resulting array of strings.

### bool(obj o) bool

The *bool* function returns a logical value of the current type. For example, if an object contains a string, the result of calling *bool(str)* is returned. If the object is not defined, an error is returned.

### bool(obj o, bool def) bool

The *bool* function returns a logical value of the current type. If the object is not defined, the second parameter is returned.

### float(obj o) float

The *float* function converts an object to a decimal floating-point number. The object must contain a value of **str, int, float** type, otherwise, an error occurs.

### float(obj o, float def) float

The *float* function converts an object to a decimal floating-point number. If the object is not defined, the second parameter is returned.

### int(obj o) int

The *int* function converts an object to an integer. The object must contain a value of **str, int, float, bool** type, otherwise, an error occurs.

### int(obj o, int def) int

The *int* function converts an object to an integer. If the object is not defined, the second parameter is returned.

### IsArray(obj o) bool

The *IsArray* function returns *true* if the object is an array. Otherwise, the function returns *false*.

### IsMap(obj o) bool

The *IsMap* function returns *true* if the object is an associative array (map). Otherwise, the function returns *false*.

### IsNil(obj o) bool

The *IsNil* function returns *true* if the object is undefined (equal to **nil**). Otherwise, the function returns *false*.

### item(obj o, int i) obj

The *item* function returns the i-th element of the object. The object must be of **arr.obj** type. If the element is missing, then an empty object is returned.

### item(obj o, str s) obj

The *item* function returns the value of key **s**. The object must be of **map.obj** type. If there is no element, it returns an empty object.

### map(obj o) map.obj

The *map* function returns an associative array of objects. The *o* object must be an associative array (map), otherwise an error is returned. When the function is called, no new array is created, but the current *map* which contains object *o* is returned.

### obj(arr.typename a) obj

The *obj* function converts an array of the *arr* type into an object.

### obj(bool b) obj

The *obj* function creates an object with the specified logical value.

### obj(float f) obj

The *obj* function creates an object with the specified **float** value.

### obj(int i) obj

The *obj* function creates an object with the specified **int** value.

### obj(map.typename m) obj

The *obj* function converts an associative array of the *map* type into an object.

### obj(str s) obj

The *obj* function creates an object with the specified **str** value.

### Sort(arr.obj o, cmpobjfunc cmpfunc) arr.obj

The *Sort* function sorts an array of objects and returns it. Sorting is done using a function of **cmpobjfunc** type.

```go
func mySort(obj left, obj right) int {
  if str(left) < str(right) : return -1
  if str(left) > str(right) : return 1
  return 0
}

run str {
  arr a = {"qwr","7","10","ab","тест","абв", "ka"}
  obj o = a
  Sort( arr(o), &mySort.cmpobjfunc )
  ...
}
```

### str(obj o) str

The *str* function converts the object to a string and returns it.

### str(obj o, str def) str

The *str* function converts the object to a string and returns it. If the object is not defined, the second parameter is returned.

### Type(obj o) str

The *Type* function returns the value type of the specified object. The following types may be returned: **int, bool, float, str, arr.obj, map.obj**. If the object is undefined, then **nil** is returned.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gentee.org/stdlib/obj.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
