This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Inline implementation

5 replies
Samuel Robert Reid
Joined: 2008-12-17,
User offline. Last seen 1 year 22 weeks ago.
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() {
                public void run() {
                    System.out.println( "started" );
                }
            } );
I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.

Thanks,
Sam Reid
James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Inline implementation
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() {
                public void run() {
                    System.out.println( "started" );
                }
            } );
I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.

Thanks,
Sam Reid

Samuel Robert Reid
Joined: 2008-12-17,
User offline. Last seen 1 year 22 weeks ago.
Re: Inline implementation
Thanks for your prompt response, this is an elegant solution.

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() {
                public void run() {
                    System.out.println( "started" );
                }
            } );
I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.

Thanks,
Sam Reid

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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:
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() {
                public void run() {
                    System.out.println( "started" );
                }
            } );
I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.

Thanks,
Sam Reid




--
Viktor Klang
Senior Systems Analyst
Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Inline implementation
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() {
                public void run() {
                    System.out.println( "started" );
                }
            } );
I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.

Thanks,
Sam Reid




--
Viktor Klang
Senior Systems Analyst

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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:
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() {
                public void run() {
                    System.out.println( "started" );
                }
            } );
I'd like to get rid of the temporary variable myRunnable in the first example, please let me know what you think.

Thanks,
Sam Reid




--
Viktor Klang
Senior Systems Analyst




--
Viktor Klang
Senior Systems Analyst

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland