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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:57:23+00:00 2026-06-17T04:57:23+00:00

I have a method that is supposed to return an Int. I am trying

  • 0

I have a method that is supposed to return an Int. I am trying to understand why Eclipse won’t let me compile this, even though it looks apparent to me inside the if statement that I am indeed returning an Int. Is there something I am missing very obvious? I am trying to understand this aspect of Scala before proceeding to write more code.

Here is the method:

def contains1(sfType: TokenType): Int = {
     if (Tokens.KEYWORDS.contains(sfType)) {
      val retVal = TokenTypes.RESERVED_WORD
    }
  }

Eclipse complains on line 2 — ‘type mismatch; found : Unit required: Int”

TokenTypes is - public abstract interface org.fife.ui.rsyntaxtextarea.TokenTypes and RESERVED_WORD is - public static final int RESERVED_WORD = 6;

I have read this post here: found: Unit required: Int – How to correct this? and tried to solve the problem before posting it, but I am still at a loss.

Edit: The method is supposed to return an Int and I had typed in the return type wrongly. My problem remains the same. Eclipse still complains.

  • 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-17T04:57:24+00:00Added an answer on June 17, 2026 at 4:57 am

    I’ll first explain what type Unit is, just in case. Even if you know already, other people having the same kind of problem might well not know it.

    Type Unit is similar to what is known in C or Java as void. In those languages, that means “this does not return anything”. However, every method in Scala returns a value.

    To bridge the gap between every method returning something and not having anything useful to return, there’s Unit. This type is an AnyVal, which means it isn’t allocated on the heap, unless it gets boxed or is the type of a field on an object. Furthermore, it has only one value, whose literal is (). That is, you could write this:

    val x: Unit = ()
    

    The practical effect of this is that, when a method “returns” Unit, the compiler doesn’t have to actually return any value, since it already knows what that value is. Therefore, it can implement methods returning Unit by declaring them, on the bytecode level, as being void.

    Anyway, if you don’t want to return anything, you return Unit.

    Now let’s look at the code given. Eclipse says it returns Unit, and, in fact, Eclipse is correct. However, most people would actually make the error of having that method return AnyVal or Any, not Unit. See the following snippet for an example:

    scala> if (true) 2
    res0: AnyVal = 2
    

    So, what happened? Well, when Scala finds an if statement, it has to figure out what is the type returned by it (in Scala, if statements return values as well). Consider the following hypothetical line:

    if (flag) x else y
    

    Clearly, the returned value will be either x or y, so the type must be such that both x and y will fit. One such type is Any, since everything has type Any. If both x and y were of the same type — say, Int — then that would also be a valid return type. Since Scala picks the most specific type, it would pick Int over Any.

    Now, what happens when you don’t have an else statement? Even in the absence of an else statement, the condition may be false — otherwise, there would be no point in using if. What Scala does, in this case, is to add an else statement. That is, it rewrites that if statement like this:

    if (true) 2 else ()
    

    Like I said before: if you do not have anything to return, return Unit! That is exactly what happens. Since both Int and Unit are AnyVal, and given that AnyVal is more specific than Any, that line returns AnyVal.

    So far I have explained what others might have seen, but not what happens in that specific code in the question:

    if (Tokens.KEYWORDS.contains(sfType)) {
      val retVal = TokenTypes.RESERVED_WORD
    }
    

    We have seen already that Scala will rewrite it like this:

    if (Tokens.KEYWORDS.contains(sfType)) {
      val retVal = TokenTypes.RESERVED_WORD
    } else ()
    

    We have also seen that Scala will pick the most specific type between the two possible results. Finally, Eclipse tells use that the return type is Unit, so the only possible explanation is that the type of this:

      val retVal = TokenTypes.RESERVED_WORD
    

    is also Unit. And that’s precisely correct: statements that declare things in Scala have type Unit. And, so, by the way, do assignments.

    The solution, as others have pointed out, is to remove the assignment and add an else statement also returning an Int:

    def contains1(sfType: TokenType): Int =
      if (Tokens.KEYWORDS.contains(sfType)) TokenTypes.RESERVED_WORD
      else -1
    

    (note: I have reformatted the method to follow the coding style more common among Scala programmers)

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

Sidebar

Related Questions

I have this method that is supposed to take a screenshot and return the
Suppose I have a method that must return an object (say from a database
In Rails 3.0.9 (Ruby 1.9.2, MySQL) I have a method that is supposed to
I have the following method that is supposed to be a generic Save to
I have this method which supposed to get a buffer and write some content
I'm trying to write a method that will return a list of DateTimes representing
Suppose I have a Java method that returns a HashMap object. Because a LinkedHashMap
Suppose I have a static method of my class that returns an object of
Suppose I have a pure virtual method in the base interface that returns to
Suppose that we have a class called class1. The class1 has a method called

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.