Can struct type be static dispatch in swift?
Asked Answered
L

0

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() 
Lowenstern answered 16/4, 2021 at 12:5 Comment(10)
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"?Gentoo
@Gentoo Okay! I edited 😀Lowenstern
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.Gentoo
@Gentoo 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.Lowenstern
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.Gentoo
@Gentoo 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. 🙏🏻Lowenstern
"struct instance with protocol type (using protocol method) will be dynamic dispatch" No it won't.Anaemia
@Anaemia Could you tell me some example that why it doesn't ?Lowenstern
It just doesn’t. The dispatch is decided at compile time.Anaemia
@Gentoo Hi, Would you mind if I ask you new question ?Lowenstern

© 2022 - 2024 — McMap. All rights reserved.