Correct use of a shared project with Silver & Fire

I have a solution containing two Swift projects. Both projects are console applications (one for Windows, one for macOS). I have added a third Swift project (a shared project). I have named the shared project “Possum”:

I dragged the Possum shared project onto the “References” node for both the Windows and macOS console projects:

The only file in the shared project is VM.swift. It contains a single test class:

public class VM {
    init() {
        print("From the VM")
    }
}

In both console application projects, they have this simple code in the program.swift file:

import Foundation

let vm = VM()

print("The magic happens here.")

This works as expected.

What I don’t understand is how Possum is namespaced. I assumed this would be valid:

let vm = Possum.VM

but the compiler it’s an “unknown identifier”. I see from this page in the Elements docs about namespaces it says for swift that:

Language Notes

The Swift language does not currently have support for declaring namespaces. All types you > define using the Swift language will become part of the default namespace configured for your > project in Project Settings. Swift > does have full support for referring to types in different namespaces, both using fully qualified > names and by importing namespaces to be in scope via the import keyword.

I thought that by creating a shared project named Possum I would be creating a namespace called Possum that could be used in other projects? I understand that I can access the VM class now from the two console projects but what if I want to create another class within those projects called VM. Won’t there be a name clash since I can’t reference the VM class in the Possum shared project with the Possum.VM identifier?

All shared projects do is inject the shared files into the projects that use them — essentially as if you just added the same file to both projects manually and had two icons show up in the tree for it. Given how Swift does name spacing, for each project the file will compile so that its types go into the default namespace (aka root namespace) set for that project.

Welcome to the wonderful world of Swift, a language designed by people who should not be designing languages and do not understand basic concepts such as why namespaces make sense or why it should be possible to have more than one in then same project ;).

Unfortunately that’s a limitation, yes. not of our compiler, but of what Swift’s syntax allows, as there’s no way to specify a different namespace per file.

One thing you could try is musing. a fully qualified name inside the source file, when declaring the class. That;s a loophole we have in Oxygene for overriding the file’s namespace — and i‘m not sure if we enabled the same in Swift (if not, we maybe should):

i.e.

public class Possum.VM {
    init() {
        print("From the VM")
    }
}

(again, not sure if she support that currently or not).

Other than than, your only option would be to, rather than just a shared project, build the shared coded into a separate library (which can have its own namespace set), or use a different language.

i’ll look at two tings next week:

(a) to support the above syntax, if we don’t already
(b) if it’s feasible to let a shared project override the root namespace of the files it injects into the main project.

Thanks, logged as bugs://84402

Confirmed that this does indeed work:

public class Class1 {

}

public class Test.Class1 {

}

var x = ClassLibrary7.Class1()
var y = Test.Class1()
1 Like

bugs://84402 got closed with status fixed.