- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Inline implementation
Tue, 2009-01-06, 04:19
Scala Community,
How do I do an inline implementation of a Java (or Scala) interface? For example, in Scala I am currently using a workaround like this:
Whereas in Java I would normally do something like this:
Thanks,
Sam Reid
How do I do an inline implementation of a Java (or Scala) interface? For example, in Scala I am currently using a workaround like this:
object myRunnable extends Runnable {
def run = println("started")
}
SwingUtilities.invokeLater(myRunnable)
Whereas in Java I would normally do something like this:
SwingUtilities.invokeAndWait( new Runnable() {I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.
public void run() {
System.out.println( "started" );
}
} );
Thanks,
Sam Reid
Tue, 2009-01-06, 04:47
#2
Re: Inline implementation
Thanks for your prompt response, this is an elegant solution.
Sam Reid
James Iry wrote:
Sam Reid
James Iry wrote:
443b2f9d0901051930j5ec4e6bar2c19893787d6adaf [at] mail [dot] gmail [dot] com" type="cite">object MySwingUtilities {
def invokeLater[X](exp : => X) {
import javax.swing.SwingUtilities
SwingUtilities invokeLater (new Runnable() {
def run = exp
})
}
}
object Test {
MySwingUtilities invokeLater {println("started")}
}
On Mon, Jan 5, 2009 at 7:18 PM, Samuel Robert Reid <Reids [at] colorado [dot] edu" rel="nofollow">Reids@colorado.edu> wrote:
Scala Community,
How do I do an inline implementation of a Java (or Scala) interface? For example, in Scala I am currently using a workaround like this:
object myRunnable extends Runnable {
def run = println("started")
}
SwingUtilities.invokeLater(myRunnable)
Whereas in Java I would normally do something like this:
SwingUtilities.invokeAndWait( new Runnable() {I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.
public void run() {
System.out.println( "started" );
}
} );
Thanks,
Sam Reid
Tue, 2009-01-06, 13:47
#3
Re: Inline implementation
Or simply:
implicit def fun2Run[T](x: => T) : Runnable = new Runnable() { def run = x }
SwingUtilities invokeLater println("hoho")
On Tue, Jan 6, 2009 at 4:32 AM, Samuel Robert Reid <Reids@colorado.edu> wrote:
--
Viktor Klang
Senior Systems Analyst
implicit def fun2Run[T](x: => T) : Runnable = new Runnable() { def run = x }
SwingUtilities invokeLater println("hoho")
On Tue, Jan 6, 2009 at 4:32 AM, Samuel Robert Reid <Reids@colorado.edu> wrote:
Thanks for your prompt response, this is an elegant solution.
Sam Reid
James Iry wrote:object MySwingUtilities {
def invokeLater[X](exp : => X) {
import javax.swing.SwingUtilities
SwingUtilities invokeLater (new Runnable() {
def run = exp
})
}
}
object Test {
MySwingUtilities invokeLater {println("started")}
}
On Mon, Jan 5, 2009 at 7:18 PM, Samuel Robert Reid <Reids@colorado.edu> wrote:
Scala Community,
How do I do an inline implementation of a Java (or Scala) interface? For example, in Scala I am currently using a workaround like this:
object myRunnable extends Runnable {
def run = println("started")
}
SwingUtilities.invokeLater(myRunnable)
Whereas in Java I would normally do something like this:
SwingUtilities.invokeAndWait( new Runnable() {I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.
public void run() {
System.out.println( "started" );
}
} );
Thanks,
Sam Reid
--
Viktor Klang
Senior Systems Analyst
Thu, 2009-01-08, 02:17
#4
Re: Inline implementation
You can omit the (). new Runnable { def run = x }
2009/1/6 Viktor Klang <viktor.klang@gmail.com>
2009/1/6 Viktor Klang <viktor.klang@gmail.com>
Or simply:
implicit def fun2Run[T](x: => T) : Runnable = new Runnable() { def run = x }
SwingUtilities invokeLater println("hoho")
On Tue, Jan 6, 2009 at 4:32 AM, Samuel Robert Reid <Reids@colorado.edu> wrote:
Thanks for your prompt response, this is an elegant solution.
Sam Reid
James Iry wrote:object MySwingUtilities {
def invokeLater[X](exp : => X) {
import javax.swing.SwingUtilities
SwingUtilities invokeLater (new Runnable() {
def run = exp
})
}
}
object Test {
MySwingUtilities invokeLater {println("started")}
}
On Mon, Jan 5, 2009 at 7:18 PM, Samuel Robert Reid <Reids@colorado.edu> wrote:
Scala Community,
How do I do an inline implementation of a Java (or Scala) interface? For example, in Scala I am currently using a workaround like this:
object myRunnable extends Runnable {
def run = println("started")
}
SwingUtilities.invokeLater(myRunnable)
Whereas in Java I would normally do something like this:
SwingUtilities.invokeAndWait( new Runnable() {I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.
public void run() {
System.out.println( "started" );
}
} );
Thanks,
Sam Reid
--
Viktor Klang
Senior Systems Analyst
Thu, 2009-01-08, 09:37
#5
Re: Inline implementation
I can also omit " : Runnable", yielding:
implicit def fun2Run[T](x: => T) = new Runnable { def run = x }
On Thu, Jan 8, 2009 at 2:12 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
--
Viktor Klang
Senior Systems Analyst
implicit def fun2Run[T](x: => T) = new Runnable { def run = x }
On Thu, Jan 8, 2009 at 2:12 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
You can omit the (). new Runnable { def run = x }
2009/1/6 Viktor Klang <viktor.klang@gmail.com>Or simply:
implicit def fun2Run[T](x: => T) : Runnable = new Runnable() { def run = x }
SwingUtilities invokeLater println("hoho")
On Tue, Jan 6, 2009 at 4:32 AM, Samuel Robert Reid <Reids@colorado.edu> wrote:
Thanks for your prompt response, this is an elegant solution.
Sam Reid
James Iry wrote:object MySwingUtilities {
def invokeLater[X](exp : => X) {
import javax.swing.SwingUtilities
SwingUtilities invokeLater (new Runnable() {
def run = exp
})
}
}
object Test {
MySwingUtilities invokeLater {println("started")}
}
On Mon, Jan 5, 2009 at 7:18 PM, Samuel Robert Reid <Reids@colorado.edu> wrote:
Scala Community,
How do I do an inline implementation of a Java (or Scala) interface? For example, in Scala I am currently using a workaround like this:
object myRunnable extends Runnable {
def run = println("started")
}
SwingUtilities.invokeLater(myRunnable)
Whereas in Java I would normally do something like this:
SwingUtilities.invokeAndWait( new Runnable() {I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.
public void run() {
System.out.println( "started" );
}
} );
Thanks,
Sam Reid
--
Viktor Klang
Senior Systems Analyst
--
Viktor Klang
Senior Systems Analyst
def invokeLater[X](exp : => X) {
import javax.swing.SwingUtilities
SwingUtilities invokeLater (new Runnable() {
def run = exp
})
}
}
object Test {
MySwingUtilities invokeLater {println("started")}
}
On Mon, Jan 5, 2009 at 7:18 PM, Samuel Robert Reid <Reids@colorado.edu> wrote: