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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:40:25+00:00 2026-06-13T02:40:25+00:00

In this below class : package patternmatching abstract class Expr { case class Var(name:

  • 0

In this below class :

package patternmatching

abstract class Expr {

  case class Var(name: String) extends Expr
  case class Number(num: Double) extends Expr
  case class UnOp(operator: String, arg:Expr) extends Expr
  case class BinOp(operator: String, left: Expr, right: Expr) extends Expr


}

I define the main class as :

package patternmatching
import patternmatching.Expr.Var

object PatternMain {

      def main(args:Array[String]) {

         val v = Var("x")
    }

}

But I receive a compile time error in PatternMain at line import patternmatching.Expr.Var :

  • object Expr is not a member of package patternmatching Note: class Expr exists, but it has no companion object.

How do I correctly invoke val v = Var("x") against the case class Var ? Am I not importing it correctly ?

  • 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-13T02:40:25+00:00Added an answer on June 13, 2026 at 2:40 am

    Remove the abstract keyword and turn the class Expr into object Expr. As far as your code goes I see no reason to don’t make these changes.

    But, if you indeed want to keep you Expr an abstract class you will have to extend and instantiate it:

    def main(args:Array[String]) {
        val expr = new Expr {} // extending the class - this creates a anonymous class
        val v = expr.Var("x")
    
        // alternatively, since now we do have a object to import
        import expr._
        val v2 = Var("x")
    
        // even another approach, now using a named class
        class MyExpr extends Expr
        val myexpr = new MyExpr
        val v3 = myexpr.Var("x")
    }
    

    Explanations:

    • Only objects and packages can have its members imported
    • Abstract classes have to be extended in order to be instantiated. The idea here is that some “point(s)” in class will is required to be defined by the client while still sharing the rest of interface with other extensions of the abstract class.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to send an email using this class below, but no success, no
I am working with NHibernate and I get this code below: public class User
I have class like this below shown. which contains the shopping items where the
I'm getting this error message with the code below: class Money { public: Money(float
I have this code below. I need to use a class id instead of
This question is related to a WPF application. I have a class as below
I have a class that I am using below. And I am using this
what's the correct way to reference a class, in this case from the same
I have this below proram package com; import java.io.PrintStream; import java.net.URL; import java.net.URLConnection; public
Please have a look at the code below: Class A package generalscenarios; public class

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.