HTH,
Steve
User.currentUser works from within the request cycle (so not in
CometActors)
. It's in the User object.
HTH,
Steve
On Nov 18, 2007 6:06 PM, Oscar Picasso <oscarp...@gmail.com > wrote:
>
> I have created a User class and User object like in the wiki example.
>
> I can sign-up and login.
>
> But how can I get the current logged user in the code, for example if
> I want to display its name?
> >
>
Funny it returns a Can.
I want to print the user name and "You are not logged" if there is no
current user.
I guess I have to open the can to get the actual user. I was thinking
of using openOr but not sure how.
>
> Funny it returns a Can.
Yes, a Can[User]
>
> I want to print the user name and "You are not logged" if there is no
> current user.
>
> I guess I have to open the can to get the actual user. I was thinking
> of using openOr but not sure how.
User.currentUser.map(_.lastName).openOr("Not Logged In")
>
> What is the idiomatic way to do what I want?
>
> On Nov 19, 12:27 am, "David Pollak" <feeder.of.the.be...@gmail.com>
> wrote:
>> On Nov 18, 2007 6:25 PM, Steve Jenson <ste...@gmail.com> wrote:
>>
>>
>>
>>> User.currentUser works from within the request cycle (so not in
>>> CometActors)
>>
>> This limitation is removed in version 381 (just posted.)
>>
>>
>>
>>> . It's in the User object.
>>
>>> HTH,
>>> Steve
>>
>>> On Nov 18, 2007 6:06 PM, Oscar Picasso <oscarpica...@gmail.com>
>>> wrote:
>>
>>>> I have created a User class and User object like in the wiki
>>>> example.
>>
>>>> I can sign-up and login.
>>
>>>> But how can I get the current logged user in the code, for
>>>> example if
>>>> I want to display its name?
>>
>> --
>> lift, the secure, simple, powerful web frameworkhttp://liftweb.net
>> Collaborative Task Managementhttp://much4.us
> >
--
David Pollak
http://blog.lostlake.org
The "map" and "openOr" methods are defined on all Cans, but have
different behavior depending on the type of Can that they are applied
to. When "map" is called on a Full can, it will apply the given
function to the contents of the Can, returning a Can with the result.
When "map" is applied on an Empty can, it returns the Empty can. When
"openOr" is called on a Full can, it returns the contents of the Can.
When "openOr" is called on an Empty can, it returns the argument to
"openOr".
So let's look at David's code:
User.currentUser.map(_.lastName).openOr("Not Logged In")
First, let's desugar the syntax. The "_.lastName" is Scala shorthand
for defining a function. In this case, it's shorthand for something
like:
(u: User) => u.lastName
That is to say, it's a function that takes a User and returns a String
of the user's last name.
So: User.currentUser returns a Can. The Can is Full (and contains the
current logged in user) if there is a current logged in user. The Can
is Empty if no user is logged in.
Then map(_.lastName) will return a Full can (with a string containing
the current logged in user's last name) if there is a current logged
in user. It will return an Empty can if no user is logged in.
Finally, openOr("You are not logged in") will return the contents of
the Can if the Can is Full (in this case, the content is the current
logged in user's last name), or "You are not logged in" if the Can is
Empty.
The Can is just a more advanced version of Scala's Option class. You
might want to read David's blog post about how lift used to use the
Option class. (Now lift uses Can, but the concepts are the same. Just
mentally replace "Some" with "Full", "None" with "Empty", and
"getOrElse" with "openOr".)
http://blog.lostlake.org/index.php?/archives/50-The-Scala-Option-class-and-how-lift-uses-it.html
--j
Yes.
> 2-
> if there is a logged user:
> currentUser returns a Full[User] Can "representing" the user
> we can then call its map method which returns a Full[???] Can
> "representing" the lastname
> which we can open, because it is full, to get the actual lastname
Yes. Here ??? == String. Scala's type inferencer figures out that
since _.lastName returns a String, then
User.currentUser.map(_.lastName) must be a Can[String] (either Full or
Empty).
> if there is no user logged:
> currentUser return an Empty[User] Can "representing" a none (?) user
> we can then call its map method which returns an Empty[???] Can
> "representing" a none(?) lastname
> we call then openOr on it. As this last Can is empty it returns the
> message "You are not logged"
Yes. It'd be a little more accurate to say that currentUser returns an
Empty Can[User], and map returns an Empty Can[String] (technically,
User is parametrizing Can, not Empty, but you have the right idea).
> Is this more or less what happens?
>
> What if I want to display something like :
> "First name: " David
> "Last name: " Pollack
Then you could change the map function. For example:
User.currentUser.map(u => "First: " + u.firstName + " Last: " +
u.lastName).openOr("You are not logged in")
Hope this helps,
--j
I was going to do a "when you don't have a valid value in Java, you
return a null" rant, but it looks like Jorge beat me to the punch and
wrote an excellent explanation of the code fragment.
Like Lisp, the optimal Scala constructs are transformative rather
than flow of control. map is a great transformative tool. Whenever
I have to solve a problem in Scala, I think "how will map, flatMap,
and filter help me with this?" They are transformative.
Thanks,
David
>>>>>> lift, the secure, simple, powerful web frameworkhttp://
>>>>>> liftweb.net
Sage advice...
As I was replying to this thread, a few lightbulbs went off in my
head, and I went back to my own code and sprinkled it with more uses
of map, flatMap, and fliter. I shaved off some lines of code and made
it even prettier :)
--j
val e: Can[Nothing] = Empty
e.map(_.length)
Even though "e" is an Empty Can, if it wasn't Empty, it's contents
would be a Nothing, and Nothings don't have a "length" method defined.
Try the following instead:
val e: Can[String] = Empty
e.map(_.length)
This explicitly declares e to be a Can of type String (as opposed to
Nothing). Since Strings have a "length" method, then we can call
"length" on whatever is inside (if there were something inside).
Hope this makes sense,
--j
The problem is with the declared type.
Use
(Empty: Can[String]).map(_.length)
or
val e: Can[String] = Empty
e.map(_.length)
or
(None: Option[String]).map(_.length)
When you just have "Empty", with no context to indicate the type of
Can, Scala treats it as an empty Can of Nothing.
Very interesting thread, I copy the response from Jorge Eugenio Ortiz into the wiki
http://liftweb.net/index.php/FAQ