Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-25 Thread Slava Pestov via swift-evolution
> On Dec 17, 2016, at 8:40 PM, Andy Chou via swift-evolution > wrote: > > I like that structs are value types in Swift, this encourages the use of > immutable data. O'Caml has an operator "with" that allows for copying an > existing struct with a change to one field. I looked at Lenses for th

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-22 Thread Derrick Ho via swift-evolution
I believe variables under private(set) can still be accessed with KVC (At least when the class inherits from nsobject.) On Thu, Dec 22, 2016 at 9:19 AM Andy Chou via swift-evolution < swift-evolution@swift.org> wrote: > Point taken. I think Swift supports both styles reasonably well. Initially > i

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-22 Thread Andy Chou via swift-evolution
Point taken. I think Swift supports both styles reasonably well. Initially it looked like writing the 'with' function would be troublesome but it turned out to be quite simple. I appreciate the point you're making, which is that you get much of the value of purely immutable values with Swift's

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-22 Thread Matthew Johnson via swift-evolution
> On Dec 22, 2016, at 4:54 AM, Jeremy Pereira > wrote: > >> >> On 21 Dec 2016, at 14:07, Matthew Johnson wrote: >> The performance hit is likely a bit larger if you *don't* use a mutable property and instead create a whole new instance. >>> >>> How is >>> >>> let a = So

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-22 Thread Jeremy Pereira via swift-evolution
> On 21 Dec 2016, at 14:07, Matthew Johnson wrote: > >>> >>> The performance hit is likely a bit larger if you *don't* use a mutable >>> property and instead create a whole new instance. >> >> How is >> >>let a = SomeStruct() >>var b = a >> >> not creating a new instance? > > Of c

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-21 Thread Matthew Johnson via swift-evolution
> On Dec 21, 2016, at 4:34 AM, Jeremy Pereira > wrote: > >> >> On 20 Dec 2016, at 13:10, Matthew Johnson wrote: >> >> >> >> Sent from my iPad >> >>> On Dec 20, 2016, at 4:32 AM, Jeremy Pereira via swift-evolution >>> wrote: >>> >>> On 20 Dec 2016, at 07:54, Pierre Monod-Broca via

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-21 Thread Jeremy Pereira via swift-evolution
> On 20 Dec 2016, at 17:32, Derrick Ho via swift-evolution > wrote: > > Jeremy, > > The problem you present is not a mutability problem but rather a cache design > problem. If your hash value truly is expensive then you only want to > calculated it when you need to... > > struct Person: Ha

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-21 Thread Jeremy Pereira via swift-evolution
> On 20 Dec 2016, at 13:10, Matthew Johnson wrote: > > > > Sent from my iPad > >> On Dec 20, 2016, at 4:32 AM, Jeremy Pereira via swift-evolution >> wrote: >> >> >>> On 20 Dec 2016, at 07:54, Pierre Monod-Broca via swift-evolution >>> wrote: >>> >>> But for a struct to be immutable, yo

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-20 Thread Erica Sadun via swift-evolution
Cascading falls under a separate proposal. There's an intermediate form which stagnated here: https://gist.github.com/erica/6794d48d917e2084d6ed As Xiaodi Wu pointed out, you can introduce an implementation of `with` that uses reflection but

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-20 Thread Daniel Leping via swift-evolution
Ok. I'm positive with some sort of case classes. +1 here On Tue, 20 Dec 2016 at 23:02 Derrick Ho via swift-evolution < swift-evolution@swift.org> wrote: > Jeremy, > > The problem you present is not a mutability problem but rather a cache > design problem. If your hash value truly is expensive th

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-20 Thread Derrick Ho via swift-evolution
Jeremy, The problem you present is not a mutability problem but rather a cache design problem. If your hash value truly is expensive then you only want to calculated it when you need to... struct Person: Hashable { var firstName: String {didSet { _hashValue = nil }} var lastName: String {

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-20 Thread Martin Waitz via swift-evolution
Am 2016-12-19 20:44, schrieb Erica Sadun via swift-evolution: https://github.com/apple/swift-evolution/pull/346 -1 I don't like where this is heading. If you want to introduce method cascading, then have a look at Dart. E.g. the example from the pull request could be something like this:

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-20 Thread Pierre Monod-Broca via swift-evolution
Good point. Pierre > Le 20 déc. 2016 à 11:32, Jeremy Pereira a > écrit : > > >> On 20 Dec 2016, at 07:54, Pierre Monod-Broca via swift-evolution >> wrote: >> >> But for a struct to be immutable, you don't need all its properties to be >> let. (I guess that's Derrick's point) > > Yes you

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-20 Thread Matthew Johnson via swift-evolution
Sent from my iPad > On Dec 20, 2016, at 4:32 AM, Jeremy Pereira via swift-evolution > wrote: > > >> On 20 Dec 2016, at 07:54, Pierre Monod-Broca via swift-evolution >> wrote: >> >> But for a struct to be immutable, you don't need all its properties to be >> let. (I guess that's Derrick's

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-20 Thread Jeremy Pereira via swift-evolution
> On 20 Dec 2016, at 07:54, Pierre Monod-Broca via swift-evolution > wrote: > > But for a struct to be immutable, you don't need all its properties to be > let. (I guess that's Derrick's point) Yes you do. Consider struct Person: Hashable { let firstName: String let lastName: String

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Pierre Monod-Broca via swift-evolution
But for a struct to be immutable, you don't need all its properties to be let. (I guess that's Derrick's point) ´´´ struct Person { /* . . . var properties . . . */ } let john = Person() // john is completely immutable ´´´ If a struct has var properties, you can do a mutable copy, change it, ass

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Xiaodi Wu via swift-evolution
More advanced mirroring is one of those things that is on the agenda somewhere. It would stand to reason that a generalizable solution which doesn't need to be created for each struct would become possible when that arrives, no? On Mon, Dec 19, 2016 at 16:08 Andy Chou via swift-evolution < swift-e

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Andy Chou via swift-evolution
Value semantics help reduce the issues around mutability, but they don't go away completely. I would like to create structs that are completely immutable after construction. Turning the properties into vars unfortunately loses this idea. The proposed 'with' function doesn't construct new instan

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Derrick Ho via swift-evolution
That is correct Andy. Let-constant can not be assigned a new value after it gets its initial value. It is unclear why you are against turning your properties into var's. Because you are using structs, all properties are copied by value. struct Person { var name: String } let andy = Person(name

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Miguel Bejar via swift-evolution
+1 on this. Scala also has a similar feature (copy constructor) for its case classes. Right now there's no generic way to do this in Swift, besides resorting to code generation. Would this feature have an impact on the ABI and therefore be considered for Swift 4 part 1? -Miguel On Mon, Dec 19, 2

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Andy Chou via swift-evolution
Thanks Erica, I wasn't aware of that proposal. If I'm reading it right, the proposed 'with' function won't work for let-constants in structures, e.g.: struct Person { let name: String let address: String } @discardableResult public func with(_ item: T, update: (inout T) throws -> Void) r

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Andy Chou via swift-evolution
Thanks Erica, I wasn't aware of that Swift evolution proposal. If I'm reading it right, this wouldn't work with structs with let-variables...? Here's what I get with this example: struct Person { let name: String let address: String } @discardableResult public func with(_ item: T, upda

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Erica Sadun via swift-evolution
https://github.com/apple/swift-evolution/pull/346 Be aware that there's a bug that's being worked on: https://bugs.swift.org/browse/SR-2773 -- E > On Dec 19, 2016, at 12:40 PM, Andy Chou via swift-evolution > wrote: > > Of course. Thanks

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Andy Chou via swift-evolution
Of course. Thanks for pointing out the obvious solution. This preserves the immutability of the struct and doesn't require O(n^2) code for structs with large numbers of fields. I was thinking of a generic solution - perhaps something like a synthetic initializer that does what your solution do

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Rien via swift-evolution
A little more work, but I like this pattern. struct Person { let name: String let address: String let phone: String func name(_ n: String) -> Person { return Person(name: n, address: self.address, phone: self.phone) } func address(_ a: String) -> Pers

Re: [swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Derrick Ho via swift-evolution
struct Person { Var name: String = "" func with(name n: String) -> Person { var a = self a.name = name return a } } let andy = Person().with(name: "Andy") let brandon = andy.with(name: "Brandon") On Mon, Dec 19, 2016 at 10:28 AM Andy Chou via swift-evolution < swift-evolution@swift.org> wrote: >

[swift-evolution] "with" operator a la O'Caml?

2016-12-19 Thread Andy Chou via swift-evolution
I like that structs are value types in Swift, this encourages the use of immutable data. O'Caml has an operator "with" that allows for copying an existing struct with a change to one field. I looked at Lenses for this functionality and it seems like a lot to digest for something so simple. I als