黑猴子的家:Scala 参数(类型)推断

// 传入函数表达式
highOrderFunction1((x: Double) => 3 * x)

// 参数推断省去类型信息
highOrderFunction1((x) => 3 * x)

// 单个参数可以省去括号
highOrderFunction1(x => 3 * x)

// 如果变量旨在=>右边只出现一次,可以用_来代替
highOrderFunction1(3 * _)

implicit 关键字,implicit是隐士的,它的功能非常强大,自动帮你实现类型推断
当你看源码的时候,要知道,implicit 关键字 是类型推断的一种体现
类型推断,主要用来简化代码

伪代码,用来理解一下implicit类型推断

class Dog{
  public void play(String a){
}
implicit int a => a.toString
}

转载于:https://www.jianshu.com/p/648f8340e6ae

猜你喜欢

转载自blog.csdn.net/weixin_34148508/article/details/91182468