Skip to content

Instantly share code, notes, and snippets.

@alonecuzzo
Created March 2, 2016 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonecuzzo/a543ce0f2fd270051f00 to your computer and use it in GitHub Desktop.
Save alonecuzzo/a543ce0f2fd270051f00 to your computer and use it in GitHub Desktop.
Some notes for sals cr
var str = "Hello, playground"
protocol TestProtocol {
typealias T
}
protocol AnotherProtocol {}
extension TestProtocol where Self.T == String {}
protocol Concatenable {
typealias T
func concatenate(lhs: T, conjunction: T, rhs: T?) -> T
}
extension Concatenable where Self.T == String {
func concatenate(lhs: T, conjunction: T, rhs: T?) -> T {
guard let rhs = rhs else { return lhs }
if rhs.characters.count == 0 { return rhs }
return lhs
}
}
struct StringConcatenator: Concatenable {
typealias T = String
}
let x = StringConcatenator().concatenate("left", conjunction: "+", rhs: "right")
x
@alonecuzzo
Copy link
Author

I was thinking of creating a concatenatable protocol and extending it with generic constraints, depending on the generic type, you could have default implementations of the concatenate function.

I still would like an extension on string, and maybe some custom operators. "mystring".concat("+", nil)

@alonecuzzo
Copy link
Author

look at concatenation in other libraries, maybe that string helper class, but then we'd want something for array

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