Swift Shared Projects

I’m having a hard time getting my head around shared projects / modules using Swift in Fire.

Let’s suppose I have two hypothetical libraries: Library1 and Library2. Both contain a single class called Analyser and both Analyser classes have a single method called Analyse().

I have a macOS console app that wants to be able to use both libraries and create instances of Library1.Analyser and Library2.Analyser.

In C# I have created two shared projects, one called Library1 and the other called Library2. I have dragged them into the references folder in the navigator in my macOS project in Fire. The navigator looks like this:

I am able to happily do this in the main Program.cs file:

namespace ConsoleApplication
{
    static class Program
    {
        public static Int32 Main(string[] args)
        {
            Library1.Analyser a1 = new Library1.Analyser();
            a1.Analyse();
            
            Library2.Analyser a2 = new Library2.Analyser();
            a2.Analyse();   
        }
    }
}

If I do the exact same thing with Swift nothing will build because the compiler complains: "Analyser.swift, line 2 - (E58) Duplicate method "func Analyse()" with same signature".

It feels to me like Swift is unable to have two modules (?) / shared projects referenced that contain same named classes. Is this the case? I think I must be misunderstanding how to use shared projects in Swift (I’m coming from a C# background).

Can someone please explain to me how I can achieve in Swift what I described as working in C#?

Gary,

the problem is that unlike C#, Swift doesn’t do per-file namespaces, so by default all files in your project live in the same namespace (the RootNamespace set in Project Settings. Files from Shared Projects enter the same project space, so from what the compiler sees, you just have two classes with the same name that define the same method — and of course that can’t be.

There’s two options around that:

(a) you can set a separate RootNamespace in the shared project (say “Shared1” and “Shared2”), and the files from that shared project — while still all being compiled in the same project space — will get that override namespace, instead

(b) instead iff shared projects use regular library projects (with different namespaces set, of course), and then use those from the main project.

—marc

Thanks for replying Marc.

So I deleted Library1 and Library2 and recreated them as a "Multi-Target Library" in Fire. The console app then compiles.

However, I’m not sure how to access the classes in the libraries. I’ve dragged them into the references for the console application but no matter what permutation I try I can’t get the compiler to recognise the classes within them:

Hmm. A multi-target library solves a very different problem than two shared projects would. What’s your goal here? For what you described in the original post, just setting the namespaces for the two shared projects should be the best fix/option…

(b) instead iff shared projects use regular library projects (with different namespaces set, of course), and then use those from the main project.

I thought by “regular library project” you
meant the multi target library. I didn’t see an option under New > Project for a regular library.

I’m just trying to get a feel for how to avoid the risk of class naming clashes in a Swift project as it’s not something I’ve had to deal with in C#.

How do I set a separate root namespace as you suggest in Fire?

OK so I recreated Library1 and Library2 as shared projects and then in the settings for each project I changed the root namespaces:

Now the project compiles and this works in Program.swift:

let a1 = Library1.Analyser()
a1.Analyse()

let a2 = Library2.Analyser()
a2.Analyse()

but there is no autocomplete for the shared projects or the classes contained within in Program.swift. Why’s that?

Hmm, there should be; can I see you’re solution as it is?

Shared projects do Code Completion in the context of a “real” project. with only one real project using them, it should automatically pick that; with multiple real projects, first invocation of CC shoukld present a picker…

Sure, here’s a zipped basic solution illustrating the issue I’m seeing:

Swift Test.zip (770.0 KB)

Pressing Option-Cmd-P in Program.swift doesn’t force the picker like I’ve seen it done before.

It wouldn’t, if theres nothing to pick, FWIW.

and I can reproduce your CC problem. It seems (makes sense, in hindsight, given how this works), that the Idea does now know the override namespaces for the files from the shared projects — it just sees them all as one space. I’ll log to get that fixed, thanx!

1 Like

Thanks, logged as bugs://85682

bugs://E24923 was closed as fixed.