Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ricardopereira/3394f7d7e1c174f08485d30b341de27e to your computer and use it in GitHub Desktop.
Save ricardopereira/3394f7d7e1c174f08485d30b341de27e to your computer and use it in GitHub Desktop.
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[type.name]?.isEmpty == false { continue } -%>
<%_ _%>git grep -lz 'class <%= type.name %>' | xargs -0 perl -i'' -pE "s/class <%= type.name %>(?=\s|:)/final class <%= type.name %>/g"
<% } %>
  1. Read about dynamic dispatch and performance https://developer.apple.com/swift/blog/?id=27, Whether this will make big difference for you depends on whether you have WMO enabled etc.
  2. Configure Sourcery https://github.com/krzysztofzablocki/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.

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