20.4 C
London
Monday, September 2, 2024

ios – Swift `in` key phrase that means?


The query of what objective in serves has been well-answered by different customers right here; in abstract: in is a key phrase outlined within the Swift closure syntax as a separator between the perform kind and the perform physique in a closure:

{ /parameters and kind/ in /perform physique/ }

However for individuals who is perhaps questioning “however why particularly the key phrase in?”, this is a little bit of historical past shared by Joe Groff, Senior Swift Compiler Engineer at Apple, on the Swift boards:

It is my fault, sorry. Within the early days of Swift, we had a closure
syntax that was similar to conventional Javascript:

func (arg: -> Kind, arg: Kind) -> Return { ... }

Whereas that is good and common syntax, it’s after all additionally very cumbersome
and awkward when you’re attempting to assist expressive useful APIs,
akin to map/filter on collections, or in order for you libraries to give you the chance
to supply closure-based APIs that really feel like extensions of the
language.

Our earliest adopters at Apple complained about this, and mandated
that we assist Ruby-style trailing closure syntax. That is tough to
match right into a C-style syntax like Swift’s, and we tried many various
iterations, together with actually Ruby’s syntax, however a lot of
them suffered from ambiguities or just distaste and revolt from our
early adopters. We wished one thing that also seemed like different components
of the language, however which may very well be parsed unambiguously and will
span the breadth of use instances from a completely express perform signature
to extraordinarily compact.

We had already taken in as a key phrase, we could not use -> like Java
does as a result of it is already used to indicate the return kind, and we had been
involved that utilizing => like C# can be too visually complicated. in
made xs.map { x in f(x) } look vaguely like for x in xs { f(x) },
and folks hated it lower than the alternate options.

*Formatting and emphasis mine. And because of Nikita Belov’s put up on the Swift boards for serving to my very own understanding.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here