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

  • Home
  • SEARCH
  • 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 8887263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:45:48+00:00 2026-06-14T21:45:48+00:00

I recently started learning Scala and started a little project to create a simple

  • 0

I recently started learning Scala and started a little project to create a simple roguelike game. However, I’m stuck at trying to implement the observer pattern.
This answer touches the subject, but I can’t figure out how to make it work. Below is the code from the answer linked above. I’m mostly puzzled by the “this: S =>” part of the code, I suppose I should have some kind of a function there, but I’m not sure. I would like to have it return a tuple from the class that will extend the Subject trait.

trait Observer[S] {
 def receiveUpdate(subject: S);
}

trait Subject[S] {
 this: S =>
 private var observers: List[Observer[S]] = Nil
 def addObserver(observer: Observer[S]) = observers = observer :: observers

 def notifyObservers() = observers.foreach(_.receiveUpdate(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-06-14T21:45:49+00:00Added an answer on June 14, 2026 at 9:45 pm

    See Steve’s answer about the self type and for another code example.

    Here is some sample code using an observer. The ObservedAccount is the Subject that is observed by an AccountReporter observer.

    trait Observer[S] {
        def receiveUpdate(subject: S);
    }
    
    trait Subject[S] { 
        this: S =>
        private var observers: List[Observer[S]] = Nil
        def addObserver(observer: Observer[S]) = observers = observer :: observers
    
        def notifyObservers() = observers.foreach(_.receiveUpdate(this))
    }
    
    class Account(initialBalance: Double) {
        private var currentBalance = initialBalance
        def balance = currentBalance
        def deposit(amount: Double)  = currentBalance += amount
        def withdraw(amount: Double) = currentBalance -= amount
    }
    
    class ObservedAccount(initialBalance: Double) extends Account(initialBalance) with Subject[Account] {
        override def deposit(amount: Double) = {
            super.deposit(amount)
            notifyObservers()
        }
        override def withdraw(amount: Double) = {
            super.withdraw(amount)
            notifyObservers()
        }
    }
    
    
    class AccountReporter extends Observer[Account] {
        def receiveUpdate(account: Account) =
            println("Observed balance change: "+account.balance)
    }
    

    Let’s see it in action:

    scala> val oa = new ObservedAccount(100.0)
    oa: ObservedAccount = ObservedAccount@3f947e20
    
    scala> val ar = new AccountReporter
    ar: AccountReporter = AccountReporter@6ea70a98
    
    scala> oa.addObserver(ar)
    
    scala> oa.deposit(40.0)
    Observed balance change: 140.0
    
    scala> oa.withdraw(40.0)
    Observed balance change: 100.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started learning unix and I am trying out some simple programs related
I recently started learning JavaFX. At the moment I can't build any JavaFX project
Recently I have started learning rails and was a little surprised that the default
I've recently started learning OpenGl through the SB fifth edition, unfortunately got stuck right
I just recently started learning the Scala language and would like to do it
I've recently started learning scala, and I've come across the :: (cons) function, which
I've recently started learning Scala and was disappointed (but not surprised) that their generics
So I've recently started learning Scala and have been using graphs as sort of
I've just recently started learning HTML/CSS and I've been trying to teach myself sound
I have recently started learning wpf and am trying to use mvvm. My understanding

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.