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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:27:18+00:00 2026-06-14T19:27:18+00:00

I want to run the class Expr below. I’ve taken this code from http://andymaleh.blogspot.ie/2008/04/scalas-pattern-matching-visitor-pattern.html

  • 0

I want to run the class Expr below. I’ve taken this code from http://andymaleh.blogspot.ie/2008/04/scalas-pattern-matching-visitor-pattern.html

Here is what I am trying :

import Expr.Sum

object Main {

    def main(args:Array[String]) {

      var expr1 = new Sum(new Num(1), new Prod(new Num(2), new Num(3)))
      print(expr1)
  }

}

abstract class Expr {

  case class Num(n: Int) extends Expr
  case class Sum(l: Expr , r: Expr) extends Expr
  case class Prod(l: Expr, r: Expr) extends Expr

  def evalExpr(e: Expr): Int = e match {
    case Num(n) => n
    case Sum(l, r) => evalExpr(l) + evalExpr(r)
    case Prod(l, r) => evalExpr(l) * evalExpr(r)
  }

  def printExpr(e: Expr) : Unit = e match {
    case Num(n) => print(" " + n + " ")
    case Sum(l, r) => printExpr(l); print("+"); printExpr(r)
    case Prod(l, r) => printExpr(l); print("x"); printExpr(r)
  }

}

But line

import Expr.Sum

is causing a compile time error : ‘not found: object Expr’ . How can I import the class Expr ?

  • 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-14T19:27:19+00:00Added an answer on June 14, 2026 at 7:27 pm

    You can only import members of instances. So your code would be:

    object Main {
        def main(args:Array[String]) {
          val expr = new Expr {} // now we have a instance.
    
          import expr._
          var expr1 = new Sum(new Num(1), new Prod(new Num(2), new Num(3)))
          print(expr1)
      }
    }
    

    A simple example explains why you can’t import non-instances members:

    class A(val x:Int) {
      object Print { def print = println(x) }
    }
    

    If you could import A.Print, which value would be bound to x? Now if we do:

    val a = new A(5); // we have a value bound to x.
    import a._
    Print.print
    

    This is one reason. The other reason that new A(5).Print != new A(5).Print, not only in equality, but also in type: val (a1, a2) = (new A(5), new A(5)); implicitly[a1.Print <:< a2.Print] wouldn’t compile. This is what Scala calls path-dependent types.

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

Sidebar

Related Questions

Why this code don't work,when i want run this code vwd 2008 express show
I want to run this same code for several mailers in an ActionMailer class.
When I want to run my java code, I have this problem : Exeption
I want to run single test class from command line using Maven and TestNG
I want to run some code before an object is removed from a has_many
I want to run some lines of applescript code from within my application. The
I have a question concerning this code which I want to run on QNX:
I want to run a cyc (opencyc) Java class, but when I run this
I want to automatically run a class method defined in a base class on
I want to run the constructor of the Main.class in the package Test2, located

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.