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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:59:25+00:00 2026-05-16T23:59:25+00:00

case class MyInt(val i : Int) { private def factorial(a : Int) : Int

  • 0
case class MyInt(val i : Int) {
    private def factorial(a : Int) : Int = a match {
        case 0 => 1
        case n => (n) * factorial(n-1)
    }
    def ! = factorial(i)
    override def toString = i.toString
}

object MyInt {
    implicit def intToMyInt(x : Int) = MyInt(x)
    implicit def myIntToInt(x : MyInt) = x.i
}
import MyInt._

object Factorial {  
    def main(args: Array[String]): Unit = {
        val a = 5
        val aFact = a!
        println("factorial of " + a + " is " + aFact)

  }
}

If I don’t put a semicolon or an empty line before println it fails to compile:

recursive value aFact needs type

  • 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-16T23:59:26+00:00Added an answer on May 16, 2026 at 11:59 pm

    All this talk about recursive function and type is a red-herring. Scala’s grammar does not allow for postfix operators in any other place than the end of an expression. This is the grammar we are talking about: the syntax of things without any semantics. Here is the relevant grammar from the specs:

    Expr        ::= (Bindings | [‘implicit’] id | ‘_’) ‘=>’ Expr
                  | Expr1
    Expr1       ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] else Expr]
                  | ‘while’ ‘(’ Expr ‘)’ {nl} Expr
                  | ‘try’ ‘{’ Block ‘}’ [‘catch’ ‘{’ CaseClauses ‘}’]
                    [‘finally’ Expr]
                  | ‘do’ Expr [semi] ‘while’ ‘(’ Expr ’)’
                  | ‘for’ (‘(’ Enumerators ‘)’ | ‘{’ Enumerators ‘}’)
                  | {nl} [‘yield’] Expr
                  | ‘throw’ Expr
                  | ‘return’ [Expr]
                  | [SimpleExpr ‘.’] id ‘=’ Expr
                  | SimpleExpr1 ArgumentExprs ‘=’ Expr
                  | PostfixExpr
                  | PostfixExpr Ascription
                  | PostfixExpr ‘match’ ‘{’ CaseClauses ‘}’
    PostfixExpr ::= InfixExpr [id [nl]]
    

    The only two places where PostfixExpr appears beside these are after the if on a case statement and before : _* on an argument list. So, looking at that, we see that the only things that can appear on the right side of a postfix expression’s method name is a type ascription or a match.

    So, what end expressions? Well, expressions appears in a lot of places in the grammar, so there’s a lot of things that could end it. In this particular example, the expression is a BlockStat inside a Block, so it must end with a semi-colon, which may be inferred or not.

    To infer this semi-colon, it is necessary that the next line must not be something that could be parsed as another kind of expression. In this particular case, we have this:

        val aFact = a!
        println("factorial of " + a + " is " + aFact)
    

    Now, let’s rewrite that from the point of view of the compiler:

        val id = id id
        id ( stringLit id id id stringLit id id )
    

    These literals and identifiers are parsed like this:

        val id = id id id ( expr )
        val Pattern2 = SimpleExpr1 id SimpleExpr1 ArgumentExprs
        val Pattern2 = InfixExpr
        val Pattern2 = Expr
        val PatDef
        PatVarDef
        Def
        BlockStat
    

    So that looks like a valid infix expression to the compiler as he parsed your program. Afterwards, it noticed the types didn’t match, but it’s too late to go back and see if a semi-colon could be inferred.

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

Sidebar

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.