[swift-evolution] Spread Operator as Shorthand for Map

Stephen Celis stephen.celis at gmail.com
Wed Dec 16 13:32:25 CST 2015


On Wed, Dec 16, 2015 at 2:18 PM, Matthew Johnson <matthew at anandabits.com>
wrote:

I believe the dot abbreviation only works for enum cases right now.
>

Nope :)


> I suppose those can be viewed as static members in some sense but they
> really something different than that.  If I am mistaken I would like to be
> corrected.
>

I'll do my best!

It works as I was trying to describe earlier. Any static member that
returns Self. Try the following in a playground/REPL:

    struct Foo {
        static var bar: Foo {
            return Foo()
        }
        static func baz() -> Foo {
            return Foo()
        }
    }
    let bar: Foo = .bar
    let baz: Foo = .baz()

Here's a SwiftStub demonstrating the behavior:
http://swiftstub.com/579553153

Enum cases work with dot abbreviation _because_ they are static members
that return Self. Enumerations aren't special-cased for dot abbreviation.

The shadowing is *current* behavior in the language.  It is not something I
> propose.
>

Is this true? Can you provide a full example that works in the playground
and demonstrates this? The hypothetical you paste already has certain
ambiguous issues (e.g. `var foo` and `func foo()` cannot compile together
because of redefinition).


> This would not refer to either.  It cannot refer to Foo.bar because Foo
> has nothing to do with the array you are mapping over.
>

The example I give works in the playground. Try it! :)


> The syntax matching my example would be:
>
> [1, 2, 3].map(.bar())
>

This would be ambiguous with a static function, `bar()`, that return Self.


> In the enum example here you cannot refer to .Bar in an arbitrary
> context.  It must be known by context that you are referring to a case of
> Foo.  It appears to me like your understanding of the existing dot
> abbreviation for enums is not quite correct and this misunderstanding is
> probably contributing to your misunderstanding and confusion regarding my
> examples.
>

I don't believe that I'm misunderstanding, but I'll take working examples
to the contrary.

Please try the examples I've provided in a playground (and the SwiftStub
link, above). I hope I've clarified how dot abbreviation works! Let me know
if you have any questions.

Stephen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/54c72f16/attachment.html>


More information about the swift-evolution mailing list