Is this valid Swift 3 (and later) code?


(Ethin Probst) #1

Is the following code valid swift 3 (and later) code? If not, is it
translatable to swift 3? This was translated using Elements Oxidizer
(http://elementscompiler.com/elements/oxidizer.aspx) using the Hello
World Java Swing application
(https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java
and https://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java).
It will run in RemObjects Silver in Visual Studio, but is it valid
Swift 3 code? You don't have to try and compile it--as Elements is the
only platform that compiles Swift to the JVM (I think?) but does it
follow the Swift 3 language specification (or the latest version,
whatever that may be)?
Code:
[start code]
import javax.swing

public class HelloWorldSwing {
  private static func createAndShowGUI() {
    //Create and set up the window.
    var frame: JFrame! = JFrame("HelloWorldSwing")
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    //Add the ubiquitous "Hello World" label.
    var label: JLabel! = JLabel("Hello World")
    frame.getContentPane().add(label)
    //Display the window.
    frame.pack()
    frame.setVisible(true)
  }

  public static func main(_ args: String![]) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(class Runnable {
      func run() {
        createAndShowGUI()
      }
    })
  }
}
[end code]

···

--
Signed,
Ethin D. Probst


(Saagar Jha) #2

This will fail to compile; it’s a bit too close to Java to work. In particular, in Swift you can’t pass a class into a function like invokeLater does; instead, Swift uses closures, which Java lacks and must make up with anonymous classes.

Saagar Jha

···

On Jan 20, 2017, at 4:16 PM, Ethin Probst via swift-users <swift-users@swift.org> wrote:

Is the following code valid swift 3 (and later) code? If not, is it
translatable to swift 3? This was translated using Elements Oxidizer
(http://elementscompiler.com/elements/oxidizer.aspx) using the Hello
World Java Swing application
(https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java
and https://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java).
It will run in RemObjects Silver in Visual Studio, but is it valid
Swift 3 code? You don't have to try and compile it--as Elements is the
only platform that compiles Swift to the JVM (I think?) but does it
follow the Swift 3 language specification (or the latest version,
whatever that may be)?
Code:
[start code]
import javax.swing

public class HelloWorldSwing {
  private static func createAndShowGUI() {
    //Create and set up the window.
    var frame: JFrame! = JFrame("HelloWorldSwing")
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    //Add the ubiquitous "Hello World" label.
    var label: JLabel! = JLabel("Hello World")
    frame.getContentPane().add(label)
    //Display the window.
    frame.pack()
    frame.setVisible(true)
  }

  public static func main(_ args: String![]) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(class Runnable {
      func run() {
        createAndShowGUI()
      }
    })
  }
}
[end code]
--
Signed,
Ethin D. Probst
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(alex) #3

Java has support for lambdas since Java 8 (2014).

···

On Fri, Jan 20, 2017 at 7:24 PM, Saagar Jha via swift-users < swift-users@swift.org> wrote:

closures, which Java lacks and must make up with anonymous classes.