Skip to content

Instantly share code, notes, and snippets.

@mayoff
Last active February 10, 2017 05:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayoff/f11fb7d75bd6bd5ba835c925b335b635 to your computer and use it in GitHub Desktop.
Save mayoff/f11fb7d75bd6bd5ba835c925b335b635 to your computer and use it in GitHub Desktop.
Avoiding duplication of property names
// For https://twitter.com/nicklockwood/status/819938181483233285
protocol Withable {
init()
}
extension Withable {
func with(_ body: (inout Self) -> ()) -> Self {
var copy = self
body(&copy)
return copy
}
init(_ body: (inout Self) -> ()) {
self.init()
body(&self)
}
}
public enum WrapMode { case disabled, beforeFirst }
public enum IndentMode { case indent }
public struct FormatOptions {
public var indent = " "
public var linebreak = "\n"
public var allowInlineSemicolons = true
public var spaceAroundRangeOperators = true
public var useVoid = true
public var trailingCommas = true
public var trailingClosures = false
public var indentComments = true
public var truncateBlankLines = true
public var insertBlankLines = true
public var removeBlankLines = true
public var allmanBraces = false
public var stripHeader = false
public var ifdefIndent = IndentMode.indent
public var wrapArguments = WrapMode.disabled
public var wrapElements = WrapMode.beforeFirst
public var uppercaseHex = true
public var experimentalRules = false
public var fragment = false
}
extension FormatOptions: Withable { }
let options1 = FormatOptions { $0.fragment = true; $0.wrapArguments = .beforeFirst }
let options2 = options1.with({ $0.uppercaseHex = false; $0.trailingCommas = false })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment