nim-lang - Nim 在数组中使用输入无法在编译时评估
问题描述
几天前我刚开始使用 nim 并且无法弄清楚为什么我总是收到此错误:错误:无法在编译时评估:线程计数
import strutils
proc thread_test()=
echo "test"
echo "How many threads do you want to use?"
var threadcount = readLine(stdin)
echo threadcount
var threads: array[threadcount, Thread[void]]
for i in 0..high(threads):
threads[i].createThread(thread_test)
joinThreads(threads)
echo "i"
解决方案
First 类型参数array
必须是编译时常量(例如,在程序编译时知道,而不是在运行时知道)。所以不可能从输入中读取大小并将其用于array
- 您需要有一个动态容器,例如seq
.
对此没有特别的解决方法 - 您可以将线程数存储在 中const threadCount = 12
,但它也必须是编译时常量。
使用seq
您的代码将是
import strutils
proc thread_test()=
echo "test"
echo "How many threads do you want to use?"
var threadcount = readLine(stdin)
echo threadcount
var threads: seq[Thread[void]]
for i in 0..high(threads):
threads.add createThread(thread_test)
joinThreads(threads)
echo "i"
推荐阅读
- css - 你如何让 flex-column 在 iOS Safari 上工作?
- python - 文件的其余部分被忽略 C:\google_cloud_storage-1.30.0-py3.8-nspkg.pth
- c++ - C++:有没有办法将各种变量类型的列表合并为一个?
- mysql - 如果没有足够的行,则 rand() 的 SQL 顺序会使用重复项填充结果
- sql - 如何使用 show_query?返回“没有适用的方法”
- ant-media-server - 验证 Antmedia REST API
- jquery - DataTables 警告:表 id='getTestCaseTable' - 请求第 17 行第 0 列的未知参数“dbi”
- typescript - 我正在尝试更新现有的 Excel 工作表数据,但它给了我错误:TypeError: Cannot read property 'toString' of undefined
- arrays - C中动态数组的时间完整性
- google-cloud-platform - 在 Compute Engine 中,无法在指定的挂载路径访问我的卷挂载