Splitting swift files into multiple files without the use of extensions

First of all I searched if there are similar discussions like this but I didn't find any but feel free to point me in the right direction :slight_smile:

Situation: Imagine you write a class and as time passes you add more and more functionality. At some point the class is so large that it has over 500 or more lines of code. Wouldn't it be more organized if you could split your class into multiple files to separate functionality i.e upload functionality in one file and download functionality in the other file... You get the idea.

Current workaround: Create a second file like so -> Filename+Upload.swift and use extensions. Thats all nice and good but there are certain limitations. For example you can't override a func declared in an extension. See this post

Solution: Give the developer the option to split classes into multiple files without the use of extensions.

I have no idea how that would be implemented so... :man_shrugging:

1 Like

Would it be though? Is it so often that you would need to specifically spread some class onto multiple files rather than breaking up its functionality into multiple classes?

Well yes there are indeed many cases where breaking up the functionality into multiple classes is the better solution but that's not always suitable. In addition it would give us more flexibility.

From my understanding, part of the limitation with this is the way files are compiled, and how incremental compilation works. If we can’t be sure storage is built when the single file is compiled, then the compiler needs to gain the skills to look across multiple files even in debug builds, which has impacts on incremental builds.

Overrides are a limitation I remember being discussed as being allowed in extensions in the same module as the declaration, which would solve this difficulty.

I’m going to say I don’t think you’re really asking for “extensions without extensions”. Extension is just the idea of a declaration not in the main body. What you’re really asking for is confusing limitations to be lifted so you can do things that seem logical using extensions across multiple files.

Some of these have valid technical reasons which may be somewhat difficult to work around, others are legacy limitations that could be lifted. I too would like to see the extensions limitations lifted wherever possible.

In general when you have classes that get too big refactoring into several classes is the correct solution.

How do other languages address this issue?

The only syntax I can imagine is another keyword that's the same as extension but has similar but not the same rules. OP hasn't really proposed what that would be.

When I run into this issue I either refactor or have a big class file. Swiftlint complains but I don't care.

C# has the ability to split files using the partial keyword like so:


public partial class SomeClass { 
    public SomeClass(string name, int age) 


public partial class SomeClass { 
    public void DoStuffff() 

I would like to add another use case to the discussion. While I'm not completely sold on the advantages of splitting a file within a project, I think splitting up files would definitely make sense for Swift scripting. At the moment, when you write a script in Swift (without a project around it) to run some tasks and that script gets a little bigger and you want to organize it, you have to create a project instead.

While some may say that this is by design, I think it is holding back many cool Swift scripts and is an unnecessary limitation staying in the way of "world domination" or even the goals for Swift 6 to become a more attractive language for cross-platform usage – people from other scripting languages take a possibility to import other Swift files for granted as it's a very common and flexibly useful feature.

You may be interested in these related discussions:

Terms of Service

Privacy Policy

Cookie Policy