> On Jan 4, 2016, at 12:41 PM, Howard Lovatt via swift-evolution
> wrote:
> 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
> Unless I missed something obvious, wouldn't placing "code that always has to
> run at the end" actually *at the end* not make more sense? Like this…
In most cases, you use defer for cleanup tasks - so it make more sense to keep
it at the source of the "problem":
file.open(); defer {
Le 2 janv. 2016 à 13:29, Maury Markowitz via swift-evolution
a écrit :
> No, they don't. With the exception of "Go", I'm unfamiliar with any other
> language that implements this feature *in this fashion*.
D has `scope (exit)`, which is exactly the same as `defer`
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:
> On Jan 2, 2016, at 9:38 AM, Nicky Gerritsen 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”)
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
> On Jan 2, 2016, at 12:56 PM, Tim Hawkins wrote:
>
> Again my 2 cents
>
> Other languages use "deffer", and have very simular semantics
No, they don't. With the exception of "Go", I'm unfamiliar with any other
language that implements this feature *in this fashion*.
> On Jan 2, 2016, at 1:26 PM, Javier Soto wrote:
>
> How would always behave if the function has an early return?
Exactly the same way as 'defer' would behave if the function has an early
return.
___
swift-evolution mailing
(Sorry if you get this email twice, I realized I first sent it from an email
address that is not the one I used to subscribe to this list)
Not only that, but placing defer at the end of a scope, where any other code
may never get executed if there’s an early return, kind of violates the whole
> On Jan 2, 2016, at 8:37 AM, Tino Heth via swift-evolution
> wrote:
>
>>
>> Unless I missed something obvious, wouldn't placing "code that always has to
>> run at the end" actually *at the end* not make more sense? Like this…
> In most cases, you use defer for
-1. `defer` doesn’t exist just to execute code at the end of blocks, it exists
to allow resource cleanup when you have a function with multiple return points
or non-trivial scoping. For example, let’s add an if statement to your code:
func clear() {
print("1")
print("3")
if
Not only that, but placing defer at the end of a scope, where any other code
may never get executed if there’s an early return, kind of violates the whole
concept of control flow.
func f() throws {
let o = file(path: "")
o.openFile()
do {
try o.write(self.data)
}
12 matches
Mail list logo