0

I think that I almost understand what will be static or dynamic dispatch.

As far as I know, struct instance with struct type will be always static dispatch.

struct A {
    func yo() {
        print("A")
    }
}

var a: A = A()
a.yo()

But, struct instance with protocol type (using protocol method) will be dynamic dispatch without using protocol & extension.


protocol Foo {
    func yo()
}

struct A: Foo {
    func yo() {
        print("A")
    }
}

var a: Foo = A()
a.yo()

So, I just wonder that "Can struct type be dynamic dispatch?" If will, Could you tell me some example? ⬇️ ?? is anything instance

var a: structType = ?? 
a.yo() 
HyunSu
  • 155
  • 7
  • Can you use some code to illustrate what you mean by "struct instance with protocol type (using protocol method) will be dynamic dispatch without using protocol & extension" and "struct type with struct instance"? – Sweeper Apr 16 '21 at 12:22
  • @Sweeper Okay! I edited – HyunSu Apr 16 '21 at 12:26
  • Doesn't the second line answer the question? "struct instance with struct type will be always static dispatch." That said, method dispatch is a rather niche topic, so I don't think there's many people interested. – Sweeper Apr 19 '21 at 05:46
  • @Sweeper oh, okay, It looks I answer this question. I would want to change question like that "Can **struct type** be **dynamic dispatch**?". I think **struct type** will be always **static dispatch**. but I don't have confidence. – HyunSu Apr 19 '21 at 06:04
  • I don’t know if there are any special cases (pretty sure there isn’t, but I can’t find any documentation saying that there is/isn’t). Anyway, you can start a bounty if you want to get attention to this question. – Sweeper Apr 19 '21 at 06:12
  • @Sweeper thank you so much. I need just confidence by more professional developer like you. Although you and I may have distinct time difference, you always quickly answer to me anytime. I really appreciate with you. – HyunSu Apr 19 '21 at 06:20
  • "struct instance with protocol type (using protocol method) will be dynamic dispatch" No it won't. – matt Apr 19 '21 at 06:48
  • @matt Could you tell me some example that why it doesn't ? – HyunSu Apr 19 '21 at 06:56
  • It just doesn’t. The dispatch is decided at compile time. – matt Apr 19 '21 at 07:59
  • @Sweeper Hi, Would you mind if I ask you new [question](https://stackoverflow.com/questions/67192198/when-to-use-dynamic-linking-library-in-ios-and-what-is-advantage-of-using-dyna) ? – HyunSu Apr 21 '21 at 09:07

0 Answers0