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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:29:46+00:00 2026-05-23T14:29:46+00:00

I’m currently learning Scala, and just discovered the way to create custom field getters/setters.

  • 0

I’m currently learning Scala, and just discovered the way to create custom field getters/setters. I have a simple example working:

class Thing(private val a:Int){
  override def toString = "Thing[" + a + "]"
  private var _value = a
  def value = _value
  def value_= (newVal:Int) = _value = newVal
}

On the console I can do:

scala> var t = new Thing(2)
t: dylan.code.Thing = Thing[2]

scala> t.value
res1: Int = 2

scala> t.value = 3

scala> t.value
res2: Int = 3

Now I’m trying to bring this concept to a slightly more complicated example; I’ll try to whittle the code down to what’s relevant:

abstract class CellExpression[Type] extends Publisher[CellUpdateEvent[Type]] with Subscriber[CellUpdateEvent[Type], CellExpression[Type]]{
    protected var cachedValue: Type = recalculateValue() 
    protected def recalculateValue(): Type

    protected def changeValue(newValue: Type):Unit = {
        val oldValue = value()
        if(newValue != oldValue){
            cachedValue = newValue
            publish(new CellUpdateEvent(this, oldValue, newValue))
        }
    }

    def value() = cachedValue
    def notify(pub: CellExpression[Type], event: CellUpdateEvent[Type]) = changeValue(recalculateValue())
}
//....
class CellVariable[Type](private val initialValue:Type) extends CellExpression[Type]{
    cachedValue = initialValue
    protected def recalculateValue() = { cachedValue }
    override def toString = "CellVariable[" + value + "]"
    def value_= (newValue:Type) = {changeValue(newValue)}
}

As far as I can tell, I’ve done what I need to in order to be able to treate value as a field via its getter and setter. But when I try it out in the console, I get:

scala> var i = new CellVariable(2)
i: dylan.code.CellVariable[Int] = CellVariable[2]

scala> i.value = 3
<console>:11: error: reassignment to val
       i.value = 3
               ^

What have I done wrong, and how can I fix it?

  • 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-23T14:29:47+00:00Added an answer on May 23, 2026 at 2:29 pm

    I actually stumbled onto the solution.

    The line where I declare my value function: def value() = cachedValue is the culprit.
    If I remove the parentheses to make the line def value = cachedValue everything seems to work as I expected.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.