[swift-users] Is this valid Swift 3 (and later) code?

Saagar Jha saagar at saagarjha.com
Fri Jan 20 21:24:15 CST 2017


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 at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170120/fbf18e8c/attachment.html>


More information about the swift-users mailing list