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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:23:08+00:00 2026-05-26T03:23:08+00:00

Naive question I believe, but all I find is just calling other constructors from

  • 0

Naive question I believe, but all I find is just calling other constructors from constructors. I need to call a method. My class (beginning):

class ScopedIterator[T](val iter : Iterator[T])
{
  private var had_next : Boolean;
  private var value : T;

  moveNext();
  
  ...

so I would like to have a constructor with single argument, and in such constructor call a method moveNext. That’s all.

When I compile the code I get error:

error: abstract member may not have private modifier

private var had_next : Boolean;

and the same for value.

I changed it to:

class ScopedIterator[T]
{
  private var had_next : Boolean;
  private var value : T;
  private var iter : Iterator[T];

  def this(it : Iterator[T]) =
  {
    iter = it;
    moveNext();
  }

  ...

But now I get error on "iter=it":

error: ‘this’ expected but identifier found.

iter = it;

How to write such constructor in Scala?

  • 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-05-26T03:23:09+00:00Added an answer on May 26, 2026 at 3:23 am

    The first problem is that your definitions of had_next and value are abstract: these members have no right-hand side.

    Try instead:

    class ScopedIterator[T](val iter : Iterator[T]) {
      private var had_next : Boolean = _
      private var value : T = _
      ...
    }
    

    Here, _ means “default uninitialized value”. For example, the following works for me in the console:

    class ScopedIterator[T](val iter : Iterator[T]) {
      private var had_next : Boolean = _
      private var value : T = _
    
      init()
    
      def init() : Unit = { println("init !") }
    }
    
    scala> new ScopedIterator(List(1,2,3).toIterator)
    init !
    resN: ScopedIterator[Int] = ...
    

    The second problem (“‘this’ expected…”) comes because in Scala, auxiliary constructors must always call another constructor as their first statement. So your constructor could start with this(), for instance. For more details, see Section 6.7 in Programming in Scala.

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

Sidebar

Related Questions

Naive question with the answer No , I believe, but still would like to
This might be a naive question. However, I am just trying to understand why
This is probably a naive question - but I want to double check to
That might be a naive git question but here it is: Before doing a
Might be my question sounds more naive. But I wanted to know if it
A naive question: As most developers, I periodically have a need to save data
a bit of a naive question, but nonetheless. I am trying to break up
First of all, I believe , it's not a duplicate question. I don't want
I can't think of a good title, but my question is not as naive
This is probably a naive question about XmlReader , but I haven't turned up

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.