Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Swift makes classes final if possible
#!/usr/bin/env bash
<% for type in types.classes { -%>
<%_ if type.attributes["final"] != nil || type.attributes["open"] != nil || types.based[]?.isEmpty == false { continue } -%>
<%_ _%>git grep -lz 'class <%= %>' | xargs -0 perl -i'' -pE "s/class <%= %>(?=\s|:)/final class <%= %>/g"
<% } %>
  1. Read about dynamic dispatch and performance, Whether this will make big difference for you depends on whether you have WMO enabled etc.
  2. Configure Sourcery
  3. Copy above FindFinalClasses.swift to your templates folder with .swiftemplate extension
  4. Run Sourcery
  5. chmod +x YourOutputFolder/FindFinalClasses.generated.swift
  6. Run the file as executable ./YourOutputFolder/FindFinalClasses.generated.swift

All top level classes (not supporting nested ones for now) that aren't open or final already will become final.

Copy link

should extension be swifttemplate instead of swiftemplate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment