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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:43:49+00:00 2026-05-22T16:43:49+00:00

Sometimes I’m building a class I want to have a reset function in. For

  • 0

Sometimes I’m building a class I want to have a reset function in. For instance

class DFA(val initialState:State) {
    var states = Map[State,State]()
    var currentState: State = initialState
    reset
    def reset {currentState = initialState}
}

Oops! Didn’t your DRY bells ring? I’m setting currentState to initialState twice. Once in reset and once in the constructor. I can’t just leave the vars uninitialized, or the compiler will complain.

Of course I could

class DFA(val initialState:State) {
    var states = Map[State,State]()
    var evilNullVariableWeMustNeverUse = null
    var currentState: State = evilNullVariableWeMustNeverUse
    reset
    def reset {currentState = initialState}
}

but I think that the downside of this is obvious.

In this simple case, it’s not so bad, but if you have 5 variables, or more complex logic, it becomes obnoxious.

How can I design around this?

  • 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-22T16:43:50+00:00Added an answer on May 22, 2026 at 4:43 pm

    Maybe create a Resettable wrapper?

    class Resettable[T](initial: T) {
      var value: T = initial
      def reset = value = initial
      def :=(other: T) = value = other
    }
    
    object Resettable {
      implicit def resettable[T](initial: T) = new Resettable(initial)
      implicit def fromResettable[T](r: Resettable[T]) = r.value
    }
    

    Then:

    class DFA(initialState:State) {
        var states = Map[State,State]()
        var currentState: Resettable[State] = initialState
        def changeState(other: State)  = currentState := other
    
        def reset = currentState.reset
    }
    

    And:

    val dfa = new DFA(new State)
    val t: State = dfa.currentState
    

    The benefits can be seen with:

    class Something {
       val a: Resettable[Int] = 0
       val b: Resettable[String] = "hi"
    }
    

    There is no need to store 0 and "hi" in another variable in order to reset.

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

Sidebar

Related Questions

Sometimes, in PL SQL you want to add a parameter to a Package, Function
Sometimes in my code I have a function which can take an argument in
Sometimes I have to work on code that moves the computer clock forward. In
Sometimes I have a true colored image, by using dithering algorithm, I can reduce
Sometimes I have come across this issue that whenever I change the order of
Sometimes when reading a socket from the TcpClient class , the stream comes in
Sometimes I want to generate INSERT statements from the contents of a database table.
Sometimes, when I have a multi-case if , or a very simple for ,
Sometimes when you're working with your local copy you might have configuration settings stored
Sometimes, I find some class names including Aware such as ApplicationContextAware and MessageSourceAware (spring).

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.