收录日期:2020/06/02 19:42:39 时间:2009-08-07 16:25:16 标签:scala,lift,code-snippets

I'm starting to learn Lift and I'm stuck. I have problem with simple snippet:

class Util {

    def in(html: NodeSeq) : NodeSeq ={

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

It should inject current User name, but I'm receiving exception:

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)

What is going on?

The problem here is that _.lastName is actually a singleton object of type MappedString and not the actual string value. To get at the String value you should do:

_.lastName.is