Skip to content

Commit

Permalink
仓颉试用完
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodxbodon committed Jul 27, 2024
1 parent 1702210 commit f570a81
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions _posts/2024-07-27-仓颉语言试写猜数字(三).md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://zhuanlan.zhihu.com/p/711324572

[上文](https://zhuanlan.zhihu.com/p/710551238)

Expand Down
86 changes: 86 additions & 0 deletions _posts/2024-07-27-仓颉语言试写猜数字(完).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
https://zhuanlan.zhihu.com/p/711325333

[上文](https://zhuanlan.zhihu.com/p/711324572)

在仓颉编程语言库 API 文档里找线索,搜 string 仅有 stringReader/Writer

再搜 int,看到 BigInt 示例里:`let int = BigInt("-123456")`

于是试试:`let sint = Int("123")`

报错:
```
error: no matching function for operator '()' function call
==> tests/int.cj:5:20:
|
5 | let sint = Int("123")
| ^
```

如果用 Int64,还是见过的错误:

```
error: the expression for numeric type conversion must have a numeric type
==> tests/int.cj:5:26:
|
5 | let sint = Int64("123")
```

如果用 BigInt 比较整数呢?那样可以把字符串转为 BigInt 再比较。

```
error: cannot convert an integer literal to type 'Struct-BigInt'
==> tests/int.cj:8:29:
|
8 | println(int.compare(1))
```

还有啥办法?std库逐个看过去,发现 std.convert !!例程里有:
`println("After the conversion of parse, \"-9223372036854775808\" became ${Int64.parse(strInt64_parse)}")`

马上试:

```
let i = Int64.parse("23")
```

还是报错:

```
error: undeclared identifier 'parse'
==> tests/int.cj:9:23:
|
9 | let i = Int64.parse("23")
| ^^^^^
```

需导入此库:`import std.convert.*`,运行通过。这时发现上一篇试过 `Int64.parse`,但就是未导入库。

回头看,api文档仅提供了在文档标题中搜索的功能。要是可以将报错信息和api文档全文搜索结合起来,也许可以方便用户。[《一站式 IDE》](https://zhuanlan.zhihu.com/p/260117393) 提到过:

> 在开发全过程中,对各种报错警告信息都可以指向相关的知识点、例程、或是他人提过的问题和解决方法(如何集成论坛或是问题追溯平台功能另行讨论)。
完成:

```
import std.console.*
import std.random.*
import std.math.*
import std.convert.*
main() {
let 想的 = abs(Random().nextInt64()%50)
while(true) {
let 猜的 = Int64.parse(Console.stdIn.readln() ?? "")
if (猜的 > 想的) {
println("大了")
} else if (猜的 < 想的) {
println("小了")
} else {
println("${猜的} 中了!")
break
}
}
}
```
Binary file added assets/2024-07-05-仓颉异常.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f570a81

Please sign in to comment.