Just An Application

November 16, 2016

Java To Swift: Packages, Imports And Access Control

Filed under: Java, Java2Swift, Swift — Tags: , , — Simon Lewis @ 11:30 am


Java has packages and is in the process of acquiring modules.

Swift has modules.

It is tempting to map Java packages on to Swift modules but, amongst other things, that would leave the vexed of question of what to map Java modules on to when they eventually arrive.

Instead I’m going to go with a flat namespace for now.

Everything gets named explicitly using the package prefix, for example,





Swift has an import declaration which is pretty much to Swift modules as the Java import declaration is to Java packages but since I’m not using Swift modules to represent Java packages I don’t really have a use for it.

Rather more useful in this context is the typealias declaration, for example, declaring

   public typealias Object = java_lang_Object

means that I can write

    public final java_lang_Boolean: Object ... 

Not brilliant but its a start.

Given its uniquity being able to declare

   public typealias String = java_lang_String

would be handy but it collides with the Swift’s String so it can’t be done.

Access Control

Java has

  • public
  • protected
  • private

and the access control level with no name, aka package

Now the dust has settled Swift has

  • open
  • public
  • internal
  • fileprivate
  • private

Of these, the first three are module-relative so strictly speaking they are not relevant but public is at least familiar so I’ll use that.

The fileprivate level doesn’t correspond to anything in Java but may be useful for transformation generated artifacts.

Swift’s private which does correspond to Java’s private so I’ll use that as well.

That leaves Java’s protected and package without an equivalent so they will just have to be Swift internal for now.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: