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)
- Missing diagnostic when capturing self in deinit Task
- Missing diagnostic when capturing self in deinit Task
- In main.swift, variables can be used before they are declared, leading to undefined behavior HOT 3
- Incorrect type inference for `switch <#Never#> {}` expression
- Existential type conforming to `AnyObject` can't satisfy a generic parameter which conforms to `AnyObject`
- Failed to produce diagnostic for expression with wrong question mark in result builders HOT 1
- Swift CI: error: no matching function for call to 'atomic_compare_exchange_strong_explicit' HOT 2
- Failed to reconstruct type for $sSq11OpenCombineE8OCombineV9PublisherVy7ElementSTQz__GD HOT 2
- A LoadableByAddress compiler crash with error "<unknown>:0: note: Unimplemented definition" HOT 1
- suppress-remarks option does not work
- The default parameters of the Swift addtarget method do not take effect
- Opaque return type sendability checking hole
- Compiler crash with dynamic generic HOT 2
- Region isolation diagnoses async iterator transfer despite `nonisolated(unsafe)` HOT 1
- Declaration macros canβt introduce local variables HOT 1
- Unexpected ambiguous type error when assigning to conforming primary associated type
- Runtime EXC_BAD_ACCESS crash in libswiftCore.dylib`__swift::__runtime::llvm::hash_combine<__swift::__runtime::llvm::hash_code, __swift::__runtime::llvm::StringRef>:
- [Autodiff] `valueWithPullback` operator does not take functions with inout parameters HOT 2
- Another `(srcAddr->getType() == destAddr->getType()), function createCopyAddr` assertion failure. HOT 2
- Misleading error message when comparing generics that vary in types HOT 1
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.