struct - Mutating in Structure Swift Same function Name -
still trying out swift, , came across problem (not sure if classifies one)
so have protocol, , structure inherits it.
protocol exampleprotocol { var simpledescription: string { } func adjust() } struct simplestructure : exampleprotocol{ var simpledescription = "a simple structure" mutating func adjust() { simpledescription += " (adjusted)" } func adjust() { //i created second method conform protocol } } var b = simplestructure() b.adjust() //this generates compiler error mentioning ambiguity (correct)
question how call mutating adjust() not adjust protocol. i.e. know if declare b protocol , initialized struct call adjust protocol, how call first adjust ? or not possible? or using wrongly ?
cheers,
your code doesn't compile, error in redefining adjust
method adding mutating
attribute - doesn't create overloaded version of adjust
.
in opinion correct code:
protocol exampleprotocol { var simpledescription: string { } mutating func adjust() } struct simplestructure : exampleprotocol{ var simpledescription = "a simple structure" mutating func adjust() { simpledescription += " (adjusted)" } }
which means: have define adjust
function mutating
in protocol.
Comments
Post a Comment