1

我开始学习电梯,我被卡住了。我对简单的片段有疑问:

class Util {

    def in(html: NodeSeq) : NodeSeq ={

        if (User.loggedIn_?)
            Helpers.bind("user", html, "name" -> User.currentUser.map(_.lastName).open_!)
        else
            NodeSeq.Empty
    }

它应该注入当前用户名,但我收到异常:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.scala_tools.maven.executions.MainHelper.runMain(MainHelper.java:105)
at org.scala_tools.maven.executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
Caused by: scala.tools.nsc.symtab.Types$TypeError: type mismatch;
 found   : x$1.lastName.type (with underlying type object x$1.lastName)
 required: com.liftworkshop.model.User#lastName.type
    at scala.tools.nsc.typechecker.Contexts$Context.error(Contexts.scala:352)

到底是怎么回事?

4

1 回答 1

3

这里的问题是 _.lastName 实际上是 MappedString 类型的单例对象,而不是实际的字符串值。要获得 String 值,您应该执行以下操作:

_.lastName .is

于 2009-09-19T04:14:14.423 回答