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 7053321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:29:27+00:00 2026-05-28T03:29:27+00:00

Often times I have a desire to create variables scoped to an if statement.

  • 0

Often times I have a desire to create variables scoped to an if statement. Some computations only relate to a particular ‘if’ statement – to pollute the outer scope with temporary variables smells bad.

What I would like to do:

val data = (whatever)

if (val x = data*2+5.4345/2.45; val y = data/128.4; x*y < 10) 
  x * y
else
  x * 2

println(x) //ERROR!

One alternative is rather messy:

val data = (whatever)

if (data*2+5.4345/2.45*data/128.4 < 10) 
  data*2+5.4345/2.45*data/128.4
else
  data*2+5.4345/2.45 * 2

The obvious alternative I’m trying to avoid:

val data = (whatever)
val x = data*2+5.4345/2.45
val y = data/128.4

if (x*y < 10) 
  x*y
else
  x * 2

println(x) //OK

Is something like this possible in Scala? Is there a decent workaround? If not, what other languages support an idea like this?

  • 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-28T03:29:28+00:00Added an answer on May 28, 2026 at 3:29 am

    Since if in Scala is an expression, i.e. it returns a value, normally you’d be setting some value to the result of your if expression. So your third alternative is just fine: put it in a code block, i.e.

    val data = (whatever)
    val myValue = {
      val x = data*2+5.4345/2.45
      val y = data/128.4
    
      if (x*y < 10) 
        x*y
      else
        x * 2
    }
    

    None of the vals declared within the block are available outside it.

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

Sidebar

Related Questions

Duplicate: Should a function have only one return statement? Often times you might have
Have a n-tire web application and search often times out after 30 secs. How
Often times a developer on my team will create a new Visual Studio project
I have many many XML files that often contain nodes mutiple times (each time
I often times have the need to import 3D points (x,y,z data) into the
Often times, a class would not have a direct output; it manipulates its dependencies,
I have this very strange issue with my MVC 2 project. Often times, I'll
Often times applications will need some database code tables (aka reference tables or domain
Often times in applications I have a property that would normally .animate(), but every
I have a page with textboxes and buttons. When the page loads, often times,

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.