Comments (8)
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.
Comment by Fabio Ritrovato (JIRA)
That's exactly my point 🙂
from swift.
Comment by Arthur Sabintsev (JIRA)
Got it. Nice find! 🙂
from swift.
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.
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.
Comment by Justin Guedes (JIRA)
@slavapestov Any update on this bug? 🙂
from swift.
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.
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)
- source compatibility project vapor_routing fails with compile error
- source compatibility project Deferred fails with a compiler error
- source compatibility project violet fails with a compiler error HOT 1
- source compatibility project penny-bot fails with a compiler error HOT 1
- Runtime crash: 'freed pointer was not the last allocation'
- Swift 6 includes swift format sub command but does not list it
- 6.0 conformance regression with abstract type witness HOT 1
- Improve dynamic actor isolation diagnostics HOT 1
- Incorrect `key.modulename` of constructor in Cursor Info
- TaskLocal.withValue typed throws
- False positive warning about closure isolation but only when not using trailing closure syntax
- False positive warning about closure isolation but only when argument label is omitted HOT 1
- Combination of sending generic + isolated(any) results in demangling failure
- Use of TaskLocal seems to defeat sending
- Function argument labels escape when using curried application in SequenceExpr
- Pattern that the region based isolation checker does not understand how to check. Please file a bug
- Appending a writable/sendable key path loses writability HOT 3
- Pattern that the region based isolation checker does not understand how to check. Please file a bug.
- False positive `requires_generic_param_made_equal_to_concrete`
- [stdlib] Update the `gen-unicode-data` package
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift.