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
  • 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-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

i get a Keyword not supported: '192.168.1.1;initial catalog'. error when trying to do this
When I try this code: a, b, c = (1, 2, 3) def test():
keyword = house SELECT * FROM products WHERE description LIKE '%house%' This query also
my SQL query SELECT keyword FROM table WHERE MATCH (keyword) AGAINST ('eco*' IN BOOLEAN
When should I use the this -keyword for properties in code? public class MyClass
i am using collect keyword to get an input from the user. now inside
The yield keyword is one of those keywords in C# that continues to mystify
I saw this keyword for the first time and I was wondering if someone
In C# you can refer to values in a class using the 'this' keyword.
What is the best practise for using the this keyword in Java? For example,

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.