示例工程 16-call-and-tostring

16-call-and-tostring/02-tostring-and-name/README.md

markdown · 0.6 KB

# 02 · tostring-and-name
 
**演示**:`__tostring` 控制 `tostring` 输出;`__name` 用于错误信息。
**运行时**:`lua54`、`luajit`。
**运行**:
 
```sh
.tools/bin/lua54 scripts/run-example.lua examples/16-call-and-tostring/02-tostring-and-name
.tools/bin/lua54 scripts/test-example.lua examples/16-call-and-tostring/02-tostring-and-name
```
 
**预期输出**(lua 5.4.9 / macOS arm64):
 
```text
text=Point(1,2) name=Point
```
 
**看点**:`__tostring` 必须返回字符串,否则运行时报错。
**注意**:`__name` 只影响错误/调试,不改变 `type()`。