swift - static protocol extensions generates Illegal Instruction compiler error -


i have read on extensions available in swift , wondering if static protocol extensions supported? know instance methods can used in protocol extension.

i wanting create protocol repository, along implementation of repository:

repository protocol

public protocol noterepositoryprotocol {     func getallnotes() -> [note] } 

repository implementation

class noterepository : noterepositoryprotocol {     func getallnotes() -> [note] {         return [note]()     } } 

then in order maintain loose coupling within application, wanted create repository through factory. trying clever , attach static method protocols so:

public extension noterepositoryprotocol {     public static func createinstance() -> noterepositoryprotocol {         return noterepository()     } } 

i know can done if drop static keyword here, wanted static this:

func test_note_repository_returns_a_valid_note_repository() {     let repository = noterepositoryprotocol.createinstance() } 

now when want change repository implementation out, updating protocol extension factory method. other alternative create actual factory handle this, idea of factory method existing on type itself.

when compile this, given following complier error:

command failed due signal: illegal instruction: 4

warning: initialization of immutable value 'repository' never used; consider replacing assignment '_' or removing let repository = noterepositoryprotocol.createinstance() ~~~~^~~~~~~~~~ _ not existential unreachable executed @ /library/caches/com.apple.xbs/sources/swiftlang/swiftlang-700.0.38.1/src/swift/lib/silgen/silgenexpr.cpp:3311! 0 swift 0x0000000106760e0b llvm::sys::printstacktrace(__sfile*) + 43 1 swift 0x000000010676154b signalhandler(int) + 379 2 libsystem_platform.dylib 0x00007fff9440ef1a _sigtramp + 26 3 swift 0x0000000106d5aa2e firsttarget + 60550 4 swift 0x0000000106761346 abort + 22 5 swift 0x000000010671ae21 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 481 6 swift 0x00000001049b503c swift::lowering::silgenfunction::emitopenexistentialimpl(swift::openexistentialexpr*, llvm::function_ref) + 2588 7 swift 0x00000001049c0ba1 swift::lowering::rvalue swift::lowering::silgenfunction::emitopenexistential(swift::openexistentialexpr*, (anonymous namespace)::rvalueemitter::visitopenexistentialexpr(swift::openexistentialexpr*, swift::lowering::sgfcontext)::$_0) + 65 8 swift 0x00000001049b6f00 swift::astvisitor<(anonymous namespace)::rvalueemitter, swift::lowering::rvalue, void, void, void, void, void, swift::lowering::sgfcontext>::visit(swift::expr*, swift::lowering::sgfcontext) + 4864 9 swift 0x00000001049af49f swift::lowering::silgenfunction::emitexprinto(swift::expr*, swift::lowering::initialization*) + 303 10 swift 0x00000001049a0dd8 swift::lowering::silgenfunction::visitpatternbindingdecl(swift::patternbindingdecl*) + 232 11 swift 0x0000000104a021fa swift::astvisitor<(anonymous namespace)::stmtemitter, void, void, void, void, void, void>::visit(swift::stmt*) + 362 12 swift 0x0000000104a02085 swift::lowering::silgenfunction::emitstmt(swift::stmt*) + 21 13 swift 0x00000001049ca136 swift::lowering::silgenfunction::emitfunction(swift::funcdecl*) + 390 14 swift 0x000000010496d3ed swift::lowering::silgenmodule::emitfunction(swift::funcdecl*) + 253 15 swift 0x0000000104a0833c (anonymous namespace)::silgentype::emittype() + 956 16 swift 0x0000000104a07ede swift::lowering::silgenmodule::visitnominaltypedecl(swift::nominaltypedecl*) + 30 17 swift 0x000000010497028b swift::lowering::silgenmodule::emitsourcefile(swift::sourcefile*, unsigned int) + 571 18 swift 0x000000010497106f swift::silmodule::constructsil(swift::moduledecl*, swift::siloptions&, swift::fileunit*, llvm::optional, bool, bool) + 703 19 swift 0x000000010497128b swift::performsilgeneration(swift::fileunit&, swift::siloptions&, llvm::optional, bool) + 123 20 swift 0x000000010477a691 performcompile(swift::compilerinstance&, swift::compilerinvocation&, llvm::arrayref, int&) + 9153 21 swift 0x00000001047780b3 frontend_main(llvm::arrayref, char const*, void*) + 2515 22 swift 0x000000010477428f main + 1983 23 libdyld.dylib 0x00007fff934fb5c9 start + 1 24 libdyld.dylib 0x0000000000000048 start + 1823492736

can not use static methods in protocol extension?

update

i removed unit test assertion improve root of problem in example source. issue compiler doesn't invoking static method on protocol.

a non optional never can nil

public static func createinstance() -> noterepositoryprotocol? {     return noterepository() } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -