前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >函数–函数字面量的一些简化写法及报错解析

函数–函数字面量的一些简化写法及报错解析

作者头像
Albert陈凯
发布2018-04-04 15:51:49
1.1K0
发布2018-04-04 15:51:49
举报
文章被收录于专栏:Albert陈凯Albert陈凯Albert陈凯

scala 下划线解析报错: missing parameter type for expanded function

本文首先介绍下eta-expansion概念,以及其对下划线的expand的解析规则和匿名函数简写注意事项,最后通过例子解析加深读者的理解

eta-expansion概念: 把 x => func(x) 简化为 func _ 或 func 的过程称为 eta-conversion 把 func 或 func _ 展开为 x => func(x) 的过程为 eta-expansion Eta Expansion的就近expand解析原则: Underscores extend outwards to the closest closing Expr : top-level expressions or expressions in parentheses

匿名函数简写注意事项:

所以当匿名函数有多个括号嵌套的时候,不要使用_的简写方式 ,而应该用普通匿名函数的书写方式。比如 (x:Int,y:Int)=> (x*2)+f(y+2)

例子解析:

例子1: scala> List(1,2,3).map(_*2) res6: List[Int] = List(2, 4, 6)

scala> List(1,2,3).map(_*2+1) res14: List[Int] = List(3, 5, 7)

但是吧*2 换成(2) 则出错,因为根据就近原则 _2 会在最近的括号中解析成匿名函数 为(x$1) => x$1.$times(2), 变成为(x=>x2) +1 , 而期望的是(x)=>(x2)+1 scala> List(1,2,3).map((*2)+1) <console>:8: error: missing parameter type for expanded function ((x$1) => x$1.$times(2)) List(1,2,3).map((*2)+1) ^

如果括号内只有_ 本身,则本身构不成一个表达式expressions,所以如下是允许的 scala> List(1,2,3).map(()+1) res8: List[Int] = List(2, 3, 4)


例子2:

scala> List(1,2,3,4).foreach(print _) 1234

_本身构不成一个表达式:expressions,所以如下是允许的

scala> List(1,2,3,4).foreach(print(_)) 1234

(.toString) 是一个在括号()里的表达式,会expand解析成匿名函数,而print期望的String类型的值,所以编译报错 scala> List(1,2,3,4).foreach(print(.toString)) <console>:8: error: missing parameter type for expanded function ((x$1) => x$1.toString) List(1,2,3,4).foreach(println(_.toString)) ^


例子3:

val myStrings = new ArrayString// do some string initialization// this works myStrings.foreach(println())// ERROR: missing parameter type for expanded function myStrings.foreach(println(.toString))

It expands to: myStrings.foreach(println(x => x.toString))

You want: myStrings.foreach(x => println(x.toString))

The placeholder syntax for anonymous functions replaces the smallest possible containing expression with a function.


**keywords:error: missing parameter type for expanded function **

Eta Expansion

The usage of placeholder syntax for anonymous functions is restricted to expressions.

参考如下:


This has already been addressed in a related question. Underscores extend outwards to the closest closing Expr

. http://stackoverflow.com/questions/4701761/why-do-i-get-a-missing-parameter-for-expanded-function-in-one-case-and-not-the

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.07.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档