dart中的typedef <函数别名>
2024-09-04 04:09:47
typedef定义如下:
typedef 给某一种特定的函数类型起了一个名字,可以认为是一个类型的别名。或者这样理解:
自己定义了一种数据类型,不过这种数据类型是函数类型,按照这种类型实例化后的对象,就会具备类型检查;
栗子举起,先来一个没有typedef定义:
class SortedCollection {
Function compare; SortedCollection(int f(Object a, Object b)) {
compare = f;
}
} int sort(Object a, Object b) => 0; void main() {
SortedCollection coll = new SortedCollection(sort);
assert(coll.compare is Function)
}
可以看到coll.compare已经丢失了函数的具体信息;
再来一个通过typedef保存别的栗子:
typedef int Compare(Object a, Object b); class SortedCollection {
Compare compare; SortedCollection(this.compare);
} int sort(Object a, Object b) => 0; void main() {
SortedCollection coll = new SortedCollection(sort);
assert(coll.compare is Function);
assert(coll.compare is Compare);
}
可以看到,我们通过typedef,定义了Compare类型,即使在int sort 传入后,compare仍旧保持Compare的类型;
基本上typedef就是这样,最后说个高级一丢的玩法,泛型函数别名:
typedef int Compare<T>(T a, T b); int sort(int a, int b) => a - b; void main() {
assert(sort is Compare<int>);
}
最新文章
- Python:认识模块
- 卷积神经网络(CNN)学习算法之----基于LeNet网络的中文验证码识别
- mysql字符乱码
- Java dynamical proxy demo
- javascript工具--控制台详解(转自 阮一峰博客)
- solr官方文档翻译系列之schema.xml配置介绍
- android4.4组件分析--service组件
- iOS基础 - 类扩展
- 远程线程注入方法CreateRemoteThread
- 百度OCR文字识别-身份证识别
- 滑稽的下午--angularjs 2.0管道的使用
- React的组件模式
- Android 常驻广播和非常驻广播
- 还原是不可能还原的,这辈子都不可能还原(手动笑cry)
- iOS UI进阶-4.0 地图与定位
- SQLdeveloper换成windows主题后不显示的情况
- 树莓派无法挂载exfat格式硬盘
- Shell学习---Shell脚本的静态检查工具shellcheck
- win8 中如何删除 共享文件夹 用户名和密码
- UART,SPI,IIC的一点理解
热门文章
- Flask理论基础(一)视图函数和URL反转函数(url_for)
- Ubuntu 奇怪踩坑记录
- 加载ubuntu的时候卡在‘SMBus Host Controller not enabled&#39;错误
- 读书笔记---《Docker 技术入门与实践》---其一
- windows server 常用功能(一)
- Vuejs input 和 textarea 元素中使用 v-model 实现双向数据绑定
- List<;Map<;String,Object>;>; 中文排序
- javascript追加节点
- 最大字段和--GSS1 MUSHROOM ORZ
- bzoj1024题解