Home »
Code Examples »
Scala Code Examples
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