3 ms·
I just want to point out that C actually supports generics in a form of pointer to void.
by bluecalm 12y ago
I just want to point out that C actually supports generics in a form of pointer to void.
- masklinn 12y agoCasting to and from void pointers is very much not generics, and is in fact exactly what you have to do in Go (casting to and from `interface {}`) or pre-generics Java (casting to and from `Object`). Although at least in Go and Java the downcast is checked and memory-safe, not so in C.
- wtf_is_up 12y agointerface{}
- bluecalm 12y agoI was responding to a comment claiming that Go is better than C in discussion about generic programming. I pointed out that C in fact supports generic programming (actually by void pointers and function pointers). Why you (and other downvoters) respond with the way to do it in Go isn't quite clear to me. I didn't claim anything about Go. I claimed something about C.
- pjmlp 12y agoMaybe because Go's approach, although lacking, is still better than C in terms of type safety. When you cast from void* to whatever type, the compiler will happily accept it and bad things will happen if the type is wrong. Maybe not right away, thus the program will bleed with corrupt memory. With Go when you cast to the wrong type, a cast violation will occur and the program will panic right away.
- masklinn 12y agoI think that's memory-safety rather than type-safety: in Go (or Java or C#) the types may not be correct (so it's not type-safe) but it'll fault at runtime rather than possibly corrupt memory as C would.
- pjmlp 12y agoFrom that point of view, at least interface{} is type safe.
- sacado2 12y agoYou'd have been right if you had said "C actually supports generics thanks to #define", but `void*` is the equivalent of interface{}