Optional explicit `self` parameter declaration in methods

I have mixed feelings. Teaching a semester of intro CS in Python last year, I found the language’s required explicit self was (contrary to popular Pythonic opinion) a source of great vexation: functions declared with n parameters take n-1 parameters when called. Students understandably had ongoing trouble getting their heads around this; it really does not make sense. Having an optional explicit self seems likely to only exacerbate that confusion.

That said, the rationale here is highly compelling. If there are modifiers that apply to parameters and could also apply to self, where else should they go? Surely making self look like a parameter for that purpose is preferable to novel syntax involving the whole function declaration.

In principle, it would be nice if there were some syntax that made self look at the declaration site the same way it looks at the usage site:

  • preceding the function name and
  • separated by a dot.

The result of that line of thought is…not instantly appealing to me:

// If it’s called like this…
widget.frongle(with: baz)

// …should it be declared like this? 😕
func self: Self.frongle(with x: Doodad)

// Do parens help? Or make it worse?
func (self: Self).frongle(with x: Doodad)

This smells funny to me, but it’s hard to see past my own familiarity bias judging it. Maybe I could see a case for the last option.

13 Likes