Can generics save virtual function calls?

146 views
Skip to first unread message

Amit Lavon

unread,
Apr 5, 2022, 2:05:36 PM4/5/22
to golang-nuts
Hi gophers,

When we use an interface as a type parameter, does it mean that calls to that interface will be replaced with calls to the concrete type?

To demonstrate, consider https://go.dev/play/p/j4N8G_n-3uG .
Will the compiler create a static call to MyDoer.Do instead of a virtual call to Doer.Do?

Ian Lance Taylor

unread,
Apr 5, 2022, 3:05:40 PM4/5/22
to Amit Lavon, golang-nuts
Occasionally, yes. In general, no. It depends, and it will likely
vary between different Go compilers and different releases of the same
Go compiler.

Ian

Amit Lavon

unread,
Apr 6, 2022, 5:05:02 PM4/6/22
to golang-nuts
Thank you!
Reply all
Reply to author
Forward
0 new messages