How are interfaces and methods implemented? #1910
-
It is very intriguing how are methods and interfaces implemented? I'm very "polyphormism" addicted man, so I like Golang's interfaces very much. And I did some interface implementations in pure-C as well. So I can imagine many different ways of "interface" implementations. But which way C3 follows? Given one may add dynamic method even to builtin |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I'll try to answer myself.
So, there is not "method tables" in terms of C++ virtual functions or Go's interfaces. Just list of name-pointer pairs, and runtime lookup into. And while C3's interface looks like Go's empty interface ( And there's Well, it is not bad while not super efficient. I believe, it will be changed to at least to hash table with fixed count of buckets in following releases. Next question raises: how it will work with dynamically loaded shared libraries? Who will call And looks like there's no runtime protection from signature mismatch: if I change
|
Beta Was this translation helpful? Give feedback.
I'll try to answer myself.
I use godbolt for that (c3's version is 0.6.2):
struct method { void *funcptr; const char *selector; struct method *next}
(excuse me for using C syntax).selector
is compared as a pointer (to constant string), not as a string itself. (btw, how it will work on Windows? Or LLVM on Windows does immutable string literals as well?)So, there is not "method tables" in terms of C++ virtual functions or Go's interfa…