Giter VIP home page Giter VIP logo

Comments (8)

swift-ci avatar swift-ci commented on May 21, 2024

Comment by Arthur Sabintsev (JIRA)

You're not supposed to call functions directly on a protocol. A class conforms to the protocol and implements the method.
Depending on if the function is static function or an instance method (in your example, it's the latter), you can call the function on the class or instance of the class.

I think your point was that the compiler is crashing due to this, and rather an error should be thrown, which is the case if you do something like:

protocol MyProtocol {
    static func MyFunc() -> String
}

let f = MyProtocol.MyFunc // error: static member 'MyFunc' cannot be used on instance of type 'MyProtocol.Protocol'

from swift.

swift-ci avatar swift-ci commented on May 21, 2024

Comment by Fabio Ritrovato (JIRA)

That's exactly my point πŸ™‚

from swift.

swift-ci avatar swift-ci commented on May 21, 2024

Comment by Arthur Sabintsev (JIRA)

Got it. Nice find! πŸ™‚

from swift.

slavapestov avatar slavapestov commented on May 21, 2024

We plan on making MyProtocol.someInstanceMethod work. You can already do this for classes, eg,

class Foo {
func f() { ... }
}

let f: Foo -> () -> () = Foo.f

It should have the same behavior for protocols:

protocol Foo {
func f()
}

let f: Foo -> () -> () = Foo.f

I plan on addressing this later.

from swift.

swift-ci avatar swift-ci commented on May 21, 2024

Comment by Jeremy Lew (JIRA)

@slavapestov This has been hanging around for a while, do you know if it's on any roadmap?

from swift.

swift-ci avatar swift-ci commented on May 21, 2024

Comment by Justin Guedes (JIRA)

@slavapestov Any update on this bug? πŸ™‚

from swift.

swift-ci avatar swift-ci commented on May 21, 2024

Comment by Greg Omelaenko (JIRA)

Just wondering, how is

let f = MyProtocol.myFunc

different from

let f = { (x: MyProtocol) in x.myFunc }

? I understand this won't work if MyProtocol has an associatedtype, but it seems like this kind of expression can't reasonably work for those cases anyway. Am I missing something?

from swift.

slavapestov avatar slavapestov commented on May 21, 2024

grego (JIRA User) in fact you've hit the nail on the head; the two forms are equivalent, and the compiler should be able to handle the first just as it can handle the second.

The problem here is that partially-applied method references were emitted using a different code path than normal closures. I'm working on a change to convert these forms into ordinary closures in the type checker, which fixes various odd cases that were not supported (such as this one) and allows simplifying a lot of code in other parts of the compiler.

I've got a WIP PR up: #28698

from swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.