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

  • SEARCH
  • Home
  • 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 3981530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:25:15+00:00 2026-05-20T05:25:15+00:00

how is keyword precedence defined in scala? Consider this piece of code: for(i <-

  • 0

how is “keyword precedence” defined in scala?

Consider this piece of code:

for(i <- 1 to 10) yield i

This is OK, I get a Seq from 1 to 10, but when i try to match right after:

for(i <- 1 to 10) yield i match {case x => x.head}

There is a compile error: error: value head is not a member of Int.

I can surround for ... yield in parentheses to give it precedence:

{for(i <- 1 to 10) yield i} match {case x => x.head}

But I’m still wondering how is the second example code interpreted. I would expect the second example to work properly as well, without surrounding it with parens.

Can anyone explain it to me or point me to the right chapter in specification?

  • 1 1 Answer
  • 1 View
  • 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-05-20T05:25:16+00:00Added an answer on May 20, 2026 at 5:25 am

    The second example is interpreted as:

    for(i <- 1 to 10) yield { i match {case x => x.head} } // won't compile
    

    The approximate syntax for for is like:

    for (Enumerators) yield Expr
    

    Since i match { case x => x.head } parses as a valid expression (token wise), that’s how the compiler will see it. So if Expr looks like an expression, that’s how it will be treated. By that reasoning, the following statements are valid:

    for(i <- 1 to 10) yield for(j <- 1 to 2) yield (i, j)
    for(i <- 1 to 10) yield if (i % 2 == 0) 'a' else 'b'
    for(i <- 1 to 10) yield try { 1 / (i - 5) } catch { case _ => }
    

    and they are all equivalent to

    for(i <- 1 to 10) yield { for(j <- 1 to 2) yield (i, j) }
    for(i <- 1 to 10) yield { if (i % 2 == 0) 'a' else 'b' }
    for(i <- 1 to 10) yield { try { 1 / (i - 5) } catch { case _ => } }
    

    Note: the Scala Language Specification is available here (first link). The relevant section is on page 161 in the Chapter A (Scala Syntax Summary).

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

Sidebar

Related Questions

keyword = house SELECT * FROM products WHERE description LIKE '%house%' This query also
Using the return keyword in Java code will return execution to the last piece
Isn't var a keyword in C#? But why can I do this: public class
While using keyword ref , calling code needs to initialize passed arguments, but with
When I try this code: a, b, c = (1, 2, 3) def test():
i am using collect keyword to get an input from the user. now inside
The keyword this in C# is slightly different from other languages. I've been googling
I used delete keyword to delete a variable but it doesn't seem to work....
Possible Duplicate: C# 'var' keyword versus explicitly defined variables EDIT: For those who are
Is there any keyword or design pattern for doing this? Please check the update

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.