Making deinit definitons consistent with non-trailing closure syntax

Current syntax for deinit enforces trailing closure syntax:

deinit {
    print("Hello")
}

I suggest to allow optional empty () for standardization of syntax like so:

deinit() {
    print("Hello")
}

As a new learner no () results in a mental hurdle in adopting this feature of classes. Trailing closure syntax is convenient, but depending on style sometimes having full form available is nice for clarity.

Any opinions on this?

1 Like

Yeah, I can see the arguments both ways. The main reason not to have parens is that they’d always be empty, which makes them not carry any information. The secondary reason I remember from the early design meetings is that deinit is never called—there’s no where you’d see foo.deinit() or even super.deinit() in Swift.

That said, getters don’t use parens either, but setters do, optionally, and neither of them have parens when you access them. subscript uses parens but is accessed with brackets. So it’s not a perfect rule or anything.

6 Likes

My mental model is that deinit is the same idea as a property observer for the object. willSet and didSet support renaming newValue and oldValue in parentheses, but nobody uses that feature.

3 Likes

Thanks guys, I now see the intent with deinit not carrying information. Also, will admit to not using the newValue / oldValue feature much myself.

However from the perspective of a new learner, property observers did seem like a bit of magic until I saw newValue / oldValue in parenthesis.

I also tend to think of deinit most closely to an initializer with no parameters because of there closeness in code.

Lastly, irl I learned some Arabic growing up. Arabic has a feature where you can shorten classes of words. It’s used most commonly to change literary style or tone. However, I think many people are encouraged to write full form words in beginner coursework.

1 Like

I read about this feature a few years ago concerning the etymology for the European term for the sine function. The Arabic mathematicians used the compact form when translating from Hindu mathematicians, but the European mathematician translators un-compact it into the wrong word.

2 Likes

init and deinit also don't have func prepended. They're special. There are also a bunch of rules to learn about how init works.

Remember "A foolish consistency is the hobgoblin of little minds."

Terms of Service

Privacy Policy

Cookie Policy