swift - Why are where clauses only valid on functions with generic parameters? -


it seems absurd method signature not compile in swift 4:

class bar<valuetype> {      func version() throws -> string valuetype == [string: any] { ... } }    

(error: clause cannot attached non-generic declaration)
compiles fine:

class bar<valuetype> {     func version<t>(_ foo: t? = nil) throws -> string valuetype == [string: any] { ... }   }  

anyone have insight why case?

because valuetype has nothing method (in first example). wrong put such method in type (class/struct/enum), since it's not true member of type. it's conditionally member of type, depending on truth value of where clause.

to achieve this, want put method in extension of type, where clause want. e.g.

extension yourtype valuetype == [string: any] {     func version() throws -> string { ... } } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -