Scala - Passing a block of code to a function in Scala (callbacks) Code Example

The code for Passing a block of code to a function in Scala (callbacks)

// a function that takes a function as an argument
def invokeLater(callback: => Unit) {
  SwingUtilities.invokeLater(new Runnable() {
    def run() {
      callback
    }
  });
}

// create function literals using the above function
def updateUISarahIsSleeping = invokeLater(microphonePanel.setSarahIsSleeping)
def updateUISarahIsSpeaking = invokeLater(microphonePanel.setSarahIsSpeaking)
def updateUISarahIsListening = invokeLater(microphonePanel.setSarahIsListening)
def updateUISarahIsNotListening = invokeLater(microphonePanel.setSarahIsNotListening)

// note: "microphonePanel.setSarahIsSleeping" and those other microphonePanel
// arguments are also functions
Code by IncludeHelp, on August 10, 2022 22:17
Reference: alvinalexander.com

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.