Then icon indicating copy to clipboard operation
Then copied to clipboard

`do` returns T

Open zuccoi opened this issue 8 years ago • 3 comments

I want do to return arbitrary value. When you don't return anything in block of do, it's usage is same as before.

zuccoi avatar Jun 23 '17 04:06 zuccoi

Use then instead if you'd like to use such like that :-)

devxoul avatar Jun 23 '17 05:06 devxoul

then's primary use case is to customize newly created instance. So, then returns the instance of type Self.

On the other hand, do is not restricted to Self. do can return nothing, and instance with type of T (arbitrary type).

If you allow do to return T, you can do something like below

let firstDateOfNextMonth: Date = Calendar(identifier: .gregorian).do {
  var date = $0.date(bySetting: .day, value: 1, of: Date())!
  date = $0.date(byAdding: .month, value: 1, to: date)!
  return date
}

zuccoi avatar Jun 23 '17 22:06 zuccoi

Hello @zuccoi Do you still want to merge this pull request?

RomanPodymov avatar Feb 25 '21 09:02 RomanPodymov