Skip to content

Instantly share code, notes, and snippets.

Embed
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[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

This comment has been minimized.

Copy link

@NickSuglobov NickSuglobov commented Feb 9, 2018

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
You can’t perform that action at this time.