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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:53:06+00:00 2026-06-09T07:53:06+00:00

i need help with this code. object test { var list : Vector[MyType] =

  • 0

i need help with this code.

object test {
  var list : Vector[MyType] = null
}

object foo extends MyType { // Mytype is a trait
    println("TEST ")
    test.list.:+(foo)
    def myfunc() { //need to define this as this is there in the trait
       // i do some operations
     } 
  }


object Bar extends MyType { // Mytype is a trait
    println("TEST ")
    test.list.:+(Bar)
    def myfunc(){
      // i do some operations
    } 
  }

now i want to go through the list and call myfunc() for all the objects that are extending MyType.

test.list foreach( t2 => t2.myfunc() )

the value’s are not getting added to the list. Can someone let me know what i am doing wrong. Its not working. Is there a way to get that print statement working?

  • 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-09T07:53:08+00:00Added an answer on June 9, 2026 at 7:53 am

    Your problem is, that the object is not constructed as a class, so that the code is called automatically. You could do two things. Either you extend App and call main or you write a function.

    trait X
    
    object test {
      var list = Vector.empty[X]
    }
    
    object Foo extends App with X {
      test.list :+= Foo
      override def toString() = "Foo"
    }
    
    object Bar extends X {
      def add() {
        test.list :+= Bar
      }
      override def toString() = "Bar"
    }
    
    Foo.main(null)
    Bar.add()
    test.list foreach println
    

    This code prints:

    Foo
    Bar
    

    Extending App only adds a main methode to an object, containing all the code in the object.

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

Sidebar

Related Questions

Kind of need help understanding what this code actually outputs. Does it out put
Sorry to bother again, but I really need help transforming this Python2 code into
i have this code, and need help with the logic end. I would like
Please i need help, this code below works fine on my localhost, php5.3+ but
I'm not good at t-sql, so I need help. I have this code I
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need help to use jConfirm with this existing code (php & Jquery &
I need help with this code: #Set the starting directory to C:\Users Set-Location C:\Users\
I need help to refactor some parts of this code example from where if
I really need help this time - Joomla 2.5 and latest K2. I'm using

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.