> ## Documentation Index
> Fetch the complete documentation index at: https://fluxon.tabooproject.org/llms.txt
> Use this file to discover all available pages before exploring further.

# 类型函数

> 类型转换、类型检查与集合转换函数

类型函数由 `FunctionType` 提供，用于值的显式转换与类型信息获取。

## 基础类型转换

这些转换函数会尽量按运行时的转换规则进行解析，当转换失败时通常返回 `0` / `0L` / `0f` / `0d`：

| 函数       | 签名              | 返回类型     | 说明             |
| -------- | --------------- | -------- | -------------- |
| `int`    | `int(value)`    | `int`    | 尝试将值转换为整数。     |
| `long`   | `long(value)`   | `long`   | 尝试将值转换为长整型。    |
| `float`  | `float(value)`  | `float`  | 尝试将值转换为单精度浮点数。 |
| `double` | `double(value)` | `double` | 尝试将值转换为双精度浮点数。 |
| `string` | `string(value)` | `string` | 按运行时规则转为字符串。   |

```fluxon theme={null}
int("123")                # => 123
int("abc")                # => 0（转换失败）
double(1)                 # => 1.0
string(PI)                # => "3.141592653589793"
```

## 安全类型转换

安全转换函数在转换失败时返回 `null` 而不是默认值：

| 函数             | 签名                    | 返回类型      | 说明                       |
| -------------- | --------------------- | --------- | ------------------------ |
| `intOrNull`    | `intOrNull(value)`    | `int?`    | 尝试转换为整数，失败返回 `null`。     |
| `longOrNull`   | `longOrNull(value)`   | `long?`   | 尝试转换为长整型，失败返回 `null`。    |
| `floatOrNull`  | `floatOrNull(value)`  | `float?`  | 尝试转换为单精度浮点数，失败返回 `null`。 |
| `doubleOrNull` | `doubleOrNull(value)` | `double?` | 尝试转换为双精度浮点数，失败返回 `null`。 |

```fluxon theme={null}
intOrNull("123")          # => 123
intOrNull("abc")          # => null

# 配合 Elvis 运算符使用
value = intOrNull(&input) ?: -1
```

## 类型检查

| 函数         | 签名                | 返回类型      | 说明                                     |
| ---------- | ----------------- | --------- | -------------------------------------- |
| `isString` | `isString(value)` | `boolean` | 检查值是否为字符串类型。                           |
| `isNumber` | `isNumber(value)` | `boolean` | 检查值是否为数值类型（int/long/float/double）。     |
| `isArray`  | `isArray(value)`  | `boolean` | 检查值是否为数组类型。                            |
| `isList`   | `isList(value)`   | `boolean` | 检查值是否为列表类型。                            |
| `isMap`    | `isMap(value)`    | `boolean` | 检查值是否为映射类型。                            |
| `typeOf`   | `typeOf(value)`   | `string`  | 返回底层对象的简短类型名。若为 `null`，返回字符串 `"null"`。 |

```fluxon theme={null}
isString("hello")         # => true
isString(123)             # => false
isNumber(3.14)            # => true
isList([1, 2, 3])         # => true
isMap([a: 1, b: 2])       # => true

typeOf("hello")           # => "String"
typeOf(123)               # => "Integer"
typeOf(null)              # => "null"
```

<Note>类型检查也可以使用 `is` 运算符：`&value is string` 等价于 `isString(&value)`。详见 [运算符与优先级](/language/operators-and-priority#类型检查运算符-is)。</Note>

## 集合转换

| 函数            | 签名                   | 返回类型             | 说明                                      |
| ------------- | -------------------- | ---------------- | --------------------------------------- |
| `array`       | `array(collection)`  | `Array` 或 `null` | 若参数是 `Collection`，则转为数组并返回；否则返回 `null`。 |
| `list`        | `list(array)`        | `List`           | 将数组转换为不可变列表。                            |
| `mutableList` | `mutableList(array)` | `MutableList`    | 将数组转换为可变列表。                             |

```fluxon theme={null}
# 列表转数组
items = [1, 2, 3]
arr = array(&items)

# 数组转列表
newList = list(&arr)
mutableItems = mutableList(&arr)
mutableItems :: add(4)    # 可变列表可以添加元素
```

## 相关链接

* [系统函数](/language/functions/system)
* [数学函数](/language/functions/math)
* [运算符与优先级](/language/operators-and-priority)
* [字面量与类型](/language/literals-and-types)
