Skip to content

Instantly share code, notes, and snippets.

@krzysztofzablocki
Last active August 22, 2021 12:12
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krzysztofzablocki/8c9a6c428cf5fcf19effca223a5548cd to your computer and use it in GitHub Desktop.
Save krzysztofzablocki/8c9a6c428cf5fcf19effca223a5548cd 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.

@NickSuglobov
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