Re: [swift-users] Can you use @autoclosure in a setter?

2017-09-11 Thread Slava Pestov via swift-users
Oh I see, the property itself does not have autoclosure type.

Slava

> On Sep 11, 2017, at 3:52 PM, Slava Pestov  wrote:
> 
> No, newValue is a closure here, and valueSource will capture this closure. 
> When valueSource is evaluated, the original closure will be evaluated.
> 
> Slava
> 
>> On Sep 11, 2017, at 3:11 PM, Hooman Mehr > > wrote:
>> 
>> But the expression that is assigned to the property will be eagerly 
>> evaluated to produce `newValue`. So this will not accomplish what Nevin is 
>> trying to do. 
>> 
>>> On Sep 11, 2017, at 3:08 PM, Slava Pestov via swift-users 
>>> > wrote:
>>> 
>>> You can have valueSource store a closure that captures the autoclosure 
>>> value. For example,
>>> 
>>> set {
>>>   valueSource = { newValue }
>>> }
>>> 
>>> Slava
>>> 
 On Sep 11, 2017, at 11:04 AM, Nevin Brackett-Rozinsky via swift-users 
 > wrote:
 
 Hi, quick question here:
 
 I have a class with a property that needs to be really *really* lazy. So 
 lazy, in fact, that when you assign to that property, the class actually 
 stores a closure of what you assigned, which is only evaluated if and when 
 you actually attempt to read the property.
 
 Simplified:
 
 class Foo {
   private var valueSource: () -> Bar
   private var valueCache: Bar?
   
   init(_ v: @escaping @autoclosure () -> Bar) {
 valueSource = v
   }
   
   var value: Bar {
 get {
   if let v = valueCache { return v }
   let w = valueSource()
   valueCache = w
   return w
 }
 set {
   /* ??? */
 }
   }
   
   // I want this function's logic to go in the setter above
   func setValue(_ v: @escaping @autoclosure () -> Bar) {
 valueSource = v
 valueCache = nil
   }
 }
 
 The goal is to be able to write things like “someFoo.value = bar1 / bar2” 
 (or even more complex expressions) and not evaluate them until/unless the 
 result is actually needed.
 
 Currently I am using “someFoo.setValue( bar1 / bar2 )”, which is not 
 nearly as ergonomic as the assignment syntax. So, is there a way to make 
 this work?
 
 Nevin
 ___
 swift-users mailing list
 swift-users@swift.org 
 https://lists.swift.org/mailman/listinfo/swift-users 
 
>>> 
>>> ___
>>> swift-users mailing list
>>> swift-users@swift.org 
>>> https://lists.swift.org/mailman/listinfo/swift-users 
>>> 
>> 
> 

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Can you use @autoclosure in a setter?

2017-09-11 Thread Slava Pestov via swift-users
No, newValue is a closure here, and valueSource will capture this closure. When 
valueSource is evaluated, the original closure will be evaluated.

Slava

> On Sep 11, 2017, at 3:11 PM, Hooman Mehr  wrote:
> 
> But the expression that is assigned to the property will be eagerly evaluated 
> to produce `newValue`. So this will not accomplish what Nevin is trying to 
> do. 
> 
>> On Sep 11, 2017, at 3:08 PM, Slava Pestov via swift-users 
>> > wrote:
>> 
>> You can have valueSource store a closure that captures the autoclosure 
>> value. For example,
>> 
>> set {
>>   valueSource = { newValue }
>> }
>> 
>> Slava
>> 
>>> On Sep 11, 2017, at 11:04 AM, Nevin Brackett-Rozinsky via swift-users 
>>> > wrote:
>>> 
>>> Hi, quick question here:
>>> 
>>> I have a class with a property that needs to be really *really* lazy. So 
>>> lazy, in fact, that when you assign to that property, the class actually 
>>> stores a closure of what you assigned, which is only evaluated if and when 
>>> you actually attempt to read the property.
>>> 
>>> Simplified:
>>> 
>>> class Foo {
>>>   private var valueSource: () -> Bar
>>>   private var valueCache: Bar?
>>>   
>>>   init(_ v: @escaping @autoclosure () -> Bar) {
>>> valueSource = v
>>>   }
>>>   
>>>   var value: Bar {
>>> get {
>>>   if let v = valueCache { return v }
>>>   let w = valueSource()
>>>   valueCache = w
>>>   return w
>>> }
>>> set {
>>>   /* ??? */
>>> }
>>>   }
>>>   
>>>   // I want this function's logic to go in the setter above
>>>   func setValue(_ v: @escaping @autoclosure () -> Bar) {
>>> valueSource = v
>>> valueCache = nil
>>>   }
>>> }
>>> 
>>> The goal is to be able to write things like “someFoo.value = bar1 / bar2” 
>>> (or even more complex expressions) and not evaluate them until/unless the 
>>> result is actually needed.
>>> 
>>> Currently I am using “someFoo.setValue( bar1 / bar2 )”, which is not nearly 
>>> as ergonomic as the assignment syntax. So, is there a way to make this work?
>>> 
>>> Nevin
>>> ___
>>> swift-users mailing list
>>> swift-users@swift.org 
>>> https://lists.swift.org/mailman/listinfo/swift-users 
>>> 
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Can you use @autoclosure in a setter?

2017-09-11 Thread Slava Pestov via swift-users
You can have valueSource store a closure that captures the autoclosure value. 
For example,

set {
  valueSource = { newValue }
}

Slava

> On Sep 11, 2017, at 11:04 AM, Nevin Brackett-Rozinsky via swift-users 
>  wrote:
> 
> Hi, quick question here:
> 
> I have a class with a property that needs to be really *really* lazy. So 
> lazy, in fact, that when you assign to that property, the class actually 
> stores a closure of what you assigned, which is only evaluated if and when 
> you actually attempt to read the property.
> 
> Simplified:
> 
> class Foo {
>   private var valueSource: () -> Bar
>   private var valueCache: Bar?
>   
>   init(_ v: @escaping @autoclosure () -> Bar) {
> valueSource = v
>   }
>   
>   var value: Bar {
> get {
>   if let v = valueCache { return v }
>   let w = valueSource()
>   valueCache = w
>   return w
> }
> set {
>   /* ??? */
> }
>   }
>   
>   // I want this function's logic to go in the setter above
>   func setValue(_ v: @escaping @autoclosure () -> Bar) {
> valueSource = v
> valueCache = nil
>   }
> }
> 
> The goal is to be able to write things like “someFoo.value = bar1 / bar2” (or 
> even more complex expressions) and not evaluate them until/unless the result 
> is actually needed.
> 
> Currently I am using “someFoo.setValue( bar1 / bar2 )”, which is not nearly 
> as ergonomic as the assignment syntax. So, is there a way to make this work?
> 
> Nevin
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Can you use @autoclosure in a setter?

2017-09-11 Thread Joanna Carter via swift-users
Greetings

> I have a class with a property that needs to be really *really* lazy. So 
> lazy, in fact, that when you assign to that property, the class actually 
> stores a closure of what you assigned, which is only evaluated if and when 
> you actually attempt to read the property.
> 
> Simplified:
> 
> class Foo {
>   private var valueSource: () -> Bar
>   private var valueCache: Bar?
>   
>   init(_ v: @escaping @autoclosure () -> Bar) {
> valueSource = v
>   }
>   
>   var value: Bar {
> get {
>   if let v = valueCache { return v }
>   let w = valueSource()
>   valueCache = w
>   return w
> }
> set {
>   /* ??? */
> }
>   }
>   
>   // I want this function's logic to go in the setter above
>   func setValue(_ v: @escaping @autoclosure () -> Bar) {
> valueSource = v
> valueCache = nil
>   }
> }
> 
> The goal is to be able to write things like “someFoo.value = bar1 / bar2” (or 
> even more complex expressions) and not evaluate them until/unless the result 
> is actually needed.
> 
> Currently I am using “someFoo.setValue( bar1 / bar2 )”, which is not nearly 
> as ergonomic as the assignment syntax. So, is there a way to make this work?

Unless I'm missing something, can't you just call the closure that you passed 
to the init ? Or do you want to use a different closure ?

You could always have a var that is a tuple, with one part being the value and 
the other the closure.

Joanna

--
Joanna Carter
Carter Consulting

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Can you use @autoclosure in a setter?

2017-09-11 Thread Nevin Brackett-Rozinsky via swift-users
Hi, quick question here:

I have a class with a property that needs to be really *really* lazy. So
lazy, in fact, that when you assign to that property, the class actually
stores a closure of what you assigned, which is only evaluated if and when
you actually attempt to read the property.

Simplified:

class Foo {
  private var valueSource: () -> Bar
  private var valueCache: Bar?

  init(_ v: @escaping @autoclosure () -> Bar) {
valueSource = v
  }

  var value: Bar {
get {
  if let v = valueCache { return v }
  let w = valueSource()
  valueCache = w
  return w
}
set {
  /* ??? */
}
  }

  // I want this function's logic to go in the setter above
  func setValue(_ v: @escaping @autoclosure () -> Bar) {
valueSource = v
valueCache = nil
  }
}

The goal is to be able to write things like “someFoo.value = bar1 / bar2”
(or even more complex expressions) and not evaluate them until/unless the
result is actually needed.

Currently I am using “someFoo.setValue( bar1 / bar2 )”, which is not nearly
as ergonomic as the assignment syntax. So, is there a way to make this work?

Nevin
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users