Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created April 13, 2016 15:41
Show Gist options
  • Select an option

  • Save DonMag/52ca8e8a26764bacf1773cd2dc1d132c to your computer and use it in GitHub Desktop.

Select an option

Save DonMag/52ca8e8a26764bacf1773cd2dc1d132c to your computer and use it in GitHub Desktop.
struct Foo {
var names: Set<String> = []
var numbers: Set<Int> = []
var otherNumbers: Set<Double> = []
func setNames<S: SequenceType where S.Generator.Element == String>(seq: S) -> Foo {
var new = self
new.names = Set(seq)
return new
}
func setNumbers<S: SequenceType where S.Generator.Element == Int>(seq: S) -> Foo {
var new = self
new.numbers = Set(seq)
return new
}
func setOtherNumbers<S: SequenceType where S.Generator.Element == Double>(seq: S) -> Foo {
var new = self
new.otherNumbers = Set(seq)
return new
}
}
var MyFoo = Foo().setNumbers(Set([1, 2, 3])).setOtherNumbers([1.1, 2.2, 3.3])
print(MyFoo.names, MyFoo.numbers, MyFoo.otherNumbers) // output: "[] [2, 3, 1] [3.3, 2.2, 1.1]\n"
// try #1 - I would expect this to modify MyFoo
MyFoo.setNames(["A", "B", "C"])
print(MyFoo.names, MyFoo.numbers, MyFoo.otherNumbers) // ??? output is still: "[] [2, 3, 1] [3.3, 2.2, 1.1]\n"
// try #2
MyFoo = MyFoo.setNames(["A", "B", "C"])
print(MyFoo.names, MyFoo.numbers, MyFoo.otherNumbers) // ok, now, output is: "["C", "B", "A"] [2, 3, 1] [3.3, 2.2, 1.1]\n"
// try #3
MyFoo.names = ["X", "Y", "Z"]
print(MyFoo.names, MyFoo.numbers, MyFoo.otherNumbers) // as expected, output is: "["X", "Z", "Y"] [2, 3, 1] [3.3, 2.2, 1.1]\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment