参数化类型的多态
来源:互联网 发布:淘宝购物返利最高的 编辑:程序博客网 时间:2024/06/05 15:12
如果我们结合隐式转换implicits使用容器,我们会得到“特设的”多态性:即对容器写泛型函数的能力。
scala> trait Container[M[_]] { def put[A](x: A): M[A]; def get[A](m: M[A]): A }scala> implicit val listContainer = new Container[List] { def put[A](x: A) = List(x); def get[A](m: List[A]) = m.head }scala> implicit val optionContainer = new Container[Some] { def put[A](x: A) = Some(x); def get[A](m: Some[A]) = m.get }scala> def tupleize[M[_]: Container, A, B](fst: M[A], snd: M[B]) = { | val c = implicitly[Container[M]] | c.put(c.get(fst), c.get(snd)) | }tupleize: [M[_],A,B](fst: M[A],snd: M[B])(implicit evidence$1: Container[M])M[(A, B)]scala> tupleize(Some(1), Some(2))res33: Some[(Int, Int)] = Some((1,2))scala> tupleize(List(1), List(2))res34: List[(Int, Int)] = List((1,2))
0 0
- 参数化类型的多态
- [疯狂Java]泛型:类型参数多态问题、类型通配符(?)、类型通配符的上下限、类型参数的上限(类、接口)
- 类型参数的类型推断
- 类型参数的类型推断
- 函数参数的类型
- printf的参数类型
- list的参数类型
- switch的参数类型
- Object类型的参数
- &类型参数的坑
- 形式参数的类型
- C#的参数类型
- Python的参数类型
- <Java编程思想> 多态与参数化类型
- 参数类型化机制
- 十一,类型参数化
- java泛型:通过自定义ParameterizedType实现参数化类型中类型参数的替换
- 返回类型与参数类型的关系
- Linux网络编程常见面试题
- JQuery模板
- iOS:json格式字符串转字典,字典转json格式字符串
- ubuntu su进入root权限
- JS面向对象编程之对象
- 参数化类型的多态
- 由于对必需的库\jdk1.7.0\jre\lib\rt.jar 具有一定的访问限制问题 解决方法
- IBATIS配置简介
- Linux系统编程——系统调用之 I/O 操作(文件操作)
- 第一次写博客
- html css 响应式布局
- DaoCloud体验-使用node构建应用程序
- 在docker中使用pm2或forever
- Linux系统编程——文件描述符的复制:dup()和dup2()