Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8460989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:45:25+00:00 2026-06-10T13:45:25+00:00

I find that after chaining Option using orElse, the element type changed, which made

  • 0

I find that after chaining Option using orElse, the element type changed, which made me impossible to use it as expected.

For example:an xml with the type of NodeSeq.

scala> xml1.headOption map { head => None } orElse xml2.lastOption map {last => Some(last)}
res11: Option[Some[ScalaObject with Equals]] = Some(Some(None))

before the orElse, head is of type Node, which is correct, but last should also be of type Node, but the compiler thinks it is of type ScalaObject with Equals, and i cannot pass last to functions take a Node as a parameter.

I can cast it using last.asInstanceOf[Node], is there a way to avoid this cast and still make last of type Node?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:45:26+00:00Added an answer on June 10, 2026 at 1:45 pm

    Try this instead (notice the added parenthesis):

    (xml.headOption map { head => None }) orElse (xml.lastOption map {last => Some(last)})
    

    The problem is that the map is being applied after orElse rather than before orElse. In other words, the expression is parsed like this:

    ((xml.headOption map { head => None }) orElse xml.lastOption) map {last => Some(last)}
    

    Since the expression on the left-hand-side of orElse has type Option[Option[Node]] but the right-hand-side expression has type Option[Node] the lowest common supertype is Option[ScalaObject with Equals].

    Scala’s flexible syntax can make coding very nice at times, but other times it results in crazy things like this. For complex expressions like this it’s a good idea to add parenthesis to make your meaning more explicit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find that after running or debugging my application a few times using eclipse,
I use reflector to decompile a asp.net dll, after that I find the bug
I'm trying to find all element that contains all words in a phrase after
After updating to Mac OSX Lion I find that I m unable to use
Our team has just started developing for the Sitecore CMS. We find that after
After following these steps: http://developers.facebook.com/docs/mobile/android/build/ I find that everything goes well, but when I
I'm trying to find a way to delay all code that takes place after
after reading all posts that come close to this, I still find no solution
I often find that I want an element to adjust its width to the
I find it strange that after looking everywhere I don't find any tool to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.