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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:22:57+00:00 2026-06-10T12:22:57+00:00

I wrote a Scala source file called Vegetables.scala. What I am trying to accomplish

  • 0

I wrote a Scala source file called Vegetables.scala. What I am trying to accomplish from this code below is to understand how import clauses work. I typed in this program in Eclipse and started the Eclipse-based REPL.
What I would like to do is create a new object called Spinach that extends Vegetable, assign it to a val and eventually invoke the showColor method of object Vegetable, passing the Spinach object into it. I know the examples sounds absurd, but I simply trying to understand the concepts/mechanics of Scala right now. To this end I did the following in the REPL:

So this is what I did on the REPL and get an error.

import com.att.scala.Vegetables._
import java.awt.Color
val obj = object Spinach extends Vegetable { val name = "Spinach" val color = Color.GREEN }

<console>:1: error: illegal start of simple expression
       val obj = object Spinach extends Vegetable { val name = "Spinach" val color = Color.GREEN }
                 ^

The code for Vegetable.scala is below:

 package com.att.scala
 import java.awt.Color

 trait Vegetable {
  val name: String
  val color: Color
}

  object Vegetables {
    object Asparagus extends Vegetable { 
      val name = "Asparagus"
      val color = Color.GREEN  
  } 

object Carrot extends Vegetable { 
  val name = "Carrot"
  val color = Color.ORANGE 
 }
val veggiePlatter = List(Asparagus, Carrot)

 def showColor(veggie: Vegetable) {
   import veggie._
   println("Entered showColor")
   import veggie._
   println("veggie color is " + color)
  }
}

What might explain this error? Firstly, I am trying to understand what is the right way to make an object on the REPL and then assign it to a val. Once that is out of the way, I hope to pass that val in a parameter. Then, I would like to test the import clause inside the showColor to see if it indeed imports the members of the veggie parameter.

  • 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-10T12:22:59+00:00Added an answer on June 10, 2026 at 12:22 pm

    Defining an object is like defining a static member, or a class. You can’t declare it and assign it to a variable at the same time. (And in fact you don’t really need to, as the object can already be accessed via its name after it’s defined.)

    So your example would work in the REPL as:

    import com.att.scala.Vegetables._
    import java.awt.Color
    object Spinach extends Vegetable { val name = "Spinach" val color = Color.GREEN }
    

    which would define an object called Spinach. After that, you could call Vegetables.showColor(Spinach) to achieve your initial goal.

    If you really wanted to assign it to a variable, you could call val obj = Spinach after you’ve declared the object, which would be valid (albeit not particularly useful – Spinach is already an unambiguous name for that thing, obj would effectively just be an alias.). Doing it on the same line that you declare the object though is illegal syntax.

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

Sidebar

Related Questions

I am trying to understand type members in Scala. I wrote a simple example
I have been trying to write some code in Scala to read a file
I am trying to write a hangman code on scala. So, I wrote the
I wrote a simulation of the Ring network topology in Scala ( source here
I'm just learning scala, and I wrote the hello,world program like this: object HelloWorld
all. I wrote the following simple code to test scala.actors._: // java version 1.7.0_02
I am trying to make a code that reads the lines in file and
This is python code for capturing streaming video from server. but I need to
While playing around with regexps in Scala I wrote something like this: scala> val
This sounds embarrassing. My purpose is to understand how Scala treats package statements, written

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.