Swift-Native Alternative to KVO

I have the terrible feeling something is wrong with my posts so that they get caught by spamfilters or similar…

But as others stated as well:
defer has a use case that is a little bit different from what you want to archive.

Why not use a solution that is widely used and better?

I'm curious:
Which languages have this "always" construct?

Deferring at the end of the function removes the ability to defer actions
on variables introduced in an inner scope.

···

On Sat, Jan 2, 2016, 1:57 PM Tino Heth via swift-evolution < swift-evolution@swift.org> wrote:

I have the terrible feeling something is wrong with my posts so that they
get caught by spamfilters or similar…

But as others stated as well:
defer has a use case that is a little bit different from what you want to
archive.

> Why not use a solution that is widely used and better?
I'm curious:
Which languages have this "always" construct?
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I don’t think it is worth changing from defer to the more traditional try finally block, both have pros and cons. Just work with what we have. You can always, as a matter of style, put a single defer block at the end of scope instead of multiple defers throughout the block.

···

On 5 Jan 2016, at 1:17 AM, Jeremy Pereira via swift-evolution <swift-evolution@swift.org> wrote:

On 2 Jan 2016, at 16:49, Maury Markowitz via swift-evolution <swift-evolution@swift.org> wrote:

On Jan 2, 2016, at 9:38 AM, Nicky Gerritsen <nickygerritsen@me.com> wrote:

Defer is used to make code *always* run, even if the function terminates early. Imagine:

Which is precisely why I called it 'always'. So in your example:

  func doSomethingWith(x: Int) {
  print(“1”)
  defer { print(“2") }
  if x > 3 { defer { print(“yay”); return }
  print(“3”)
  }

I would say:

  func doSomethingWith(x: Int) {
  print(“1”)
  print(“3”)
      always {
          print(“2")
          if x > 3 { print(“yay”) }
      }
  }

This is functionally identical, but both the syntax and program flow are greatly improved.

No your example is not functionally identical to Nicky’s (notwithstanding the missing closing brace in the original). “defer" defers the closure to the end of the current scope. In this instance , that is the end of the if block. The “yay” must come before the “2” because the if scope exits before the function scope. Also, in the following:

func doSomethingWith(x: Int) {
   print("1")
   defer { print("2") }
   if x > 3 { defer { print("yay") } }
   print("3")
}

doSomethingWith(4)

“yay” comes before “3” for the same reason.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

I don’t think it is worth changing from defer to the more traditional try finally block, both have pros and cons. Just work with what we have. You can always, as a matter of style, put a single defer block at the end of scope instead of multiple defers throughout the block.

For what it’s worth, the error-handling rationale discusses the trade-offs of the defer syntax. I think we’re very happy with the current syntax.

John.

···

On Jan 4, 2016, at 12:41 PM, Howard Lovatt via swift-evolution <swift-evolution@swift.org> wrote:

On 5 Jan 2016, at 1:17 AM, Jeremy Pereira via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On 2 Jan 2016, at 16:49, Maury Markowitz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Jan 2, 2016, at 9:38 AM, Nicky Gerritsen <nickygerritsen@me.com <mailto:nickygerritsen@me.com>> wrote:

Defer is used to make code *always* run, even if the function terminates early. Imagine:

Which is precisely why I called it 'always'. So in your example:

  func doSomethingWith(x: Int) {
  print(“1”)
  defer { print(“2") }
  if x > 3 { defer { print(“yay”); return }
  print(“3”)
  }

I would say:

  func doSomethingWith(x: Int) {
  print(“1”)
  print(“3”)
      always {
          print(“2")
          if x > 3 { print(“yay”) }
      }
  }

This is functionally identical, but both the syntax and program flow are greatly improved.

No your example is not functionally identical to Nicky’s (notwithstanding the missing closing brace in the original). “defer" defers the closure to the end of the current scope. In this instance , that is the end of the if block. The “yay” must come before the “2” because the if scope exits before the function scope. Also, in the following:

func doSomethingWith(x: Int) {
   print("1")
   defer { print("2") }
   if x > 3 { defer { print("yay") } }
   print("3")
}

doSomethingWith(4)

“yay” comes before “3” for the same reason.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution