当前位置:网站首页>d具体化函数参数
d具体化函数参数
2022-06-10 01:09:00 【fqbqrr】
原文
今天,我有一堆带有执行某些操作的参数的函数,我想通过程序的命令行向用户公开他们.此外,为了尽量方便用户,程序帮助文本应准确地描述每个函数(在命令行上被视为子命令)及每个函数带的参数.
当然,传统方法很简单:编写打印描述函数及其参数文本的showHelp()函数.然后在main()中为函数名编写大的switch语句,每个case块解析程序参数,转换为合适形式,并调用该显示函数.
因此,我本着DRY寻求了更好方法:把所述函数放入包装结构中,按静态标记它,添加由showHelp()自省的串UDA,来生成保证最新的函数列表,并通过main()生成必要的若块.帮助文本很简单:只需检查包装器结构,提取函数名,描述串UDA和参数名列表,然后打印出来.case块有点棘手.如果每个函数有相同参数集,那就很简单了:只需在标准调用中插件函数名即可.但是,如果函数有*不同*参数呢?
然后灵感来了:为什么要单独声明这些变量呢?我不必.我可用is(typeof(func)Params:__parameters)参数元组保存参数,然后按该类型用参数元组声明复合变量:
void main(string[] args) {
...
static if (is(typeof(myfunc) Params : __parameters)) {
Params funcArgs; //为所有函数参数使用一个名字
//现在`转换`命令行参数至正确类型来填充参数
foreach (i, T; Params) {
// std.conv.to是大炸弹
import std.conv : to;
funcArgs[i] = args[i+2].to!T;
}
//要调用函数,只需一次性传递整个聚集给它:
myfunc(funcArgs);
//感谢神奇元组类型,它会自动展开`funcArgs`元组为多个参数
}
}
复合变量'funcArgs'具体化了目标函数参数;允许解析命令行参数循环中像伪数组一样操作它.不必构建繁琐参数列表的mixin!(当然,实际代码,仍需要mixin来绑定myfunc到实际按名迭代的目标函数.)
边栏推荐
- 使用fiddler断点修改某个接口的 请求参数与返回参数,拦击请求,修改请求与响应
- Thingsboard tutorial (18): overview of TB rule engine
- How can win11 directly return to the desktop?
- window.open(url)多次打开下载链接被浏览器拦截问题解决方案,js实现循环访问多个下载链接
- Use the fiddler breakpoint to modify the request parameters and return parameters of an interface, intercept requests, and modify requests and responses
- Mongodb open source "queryable encryption" system
- 分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
- Disorder of flinksql
- Picture batch download + picture mosaic: multiple pictures constitute the Dragon Boat Festival Ankang!
- What are the reasons for frequent channel drops in easycvr cascaded video convergence platform?
猜你喜欢

Source code analysis of Tencent libco collaboration open source library (I) -- download libco compilation and installation and try to run the sample code

【ICLR 2022】Towards Continual Knowledge Learning of Language Models

Teaching Broad Reasoning Skills via Decomposition-Guided Contexts

进阶自动化测试的看过来,一文7个阶段5000字带你全面了解自动化测试

图片批量下载 +图片马赛克:多张图片组成端午安康!

腾讯Libco协程开源库 源码分析(一)---- 下载Libco 编译安装 尝试运行示例代码

【ICLR 2022】Towards Continual Knowledge Learning of Language Models

把 GPL 视作“病毒”?请停止污名化 GPL

国内现货白银有哪些好技术:常见指标的简单用法

FPGA-VGA显示
随机推荐
【LeetCode】461. 汉明距离
Where should middle-aged test development engineers go? The unknown is tomorrow, break the label
Shell Eval usage explanation command replacement
TCP 连接中的keep-alive
从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
Introduction to cross platform multimedia rendering engine OPR
My creation anniversary
Random talk on test coverage
MongoDB 开源“可查询加密”系统 Queryable Encryption
Thingsboard tutorial (18): overview of TB rule engine
洛谷P1220 关路灯 题解 区间DP
别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!
洛谷P1029 最大公约数和最小公倍数问题 题解
【ACL 2022】Hallucinated but Factual! Inspecting the Factuality of Hallucinations
Rhcsa day 7
Regular expression does not contain a string
Win11退回Win10没有返回选项怎么办?
【LeetCode】128. 最长连续序列
梯度下降引发AI大牛们“激辩”,网友:每个人的答案都值得一看
Picture batch download + picture mosaic: multiple pictures constitute the Dragon Boat Festival Ankang!