5 ms·
> Why would you expect the above to work? It is a pretty clear violation of the type rules on any sane language. It doesn't violate structural typing rules, so
by naasking 10y ago
> Why would you expect the above to work? It is a pretty clear violation of the type rules on any sane language.
It doesn't violate structural typing rules, so I disagree.
- gpderetta 10y agoYou are actually right that it will work on a structurally typed language. Have an upvote.
- kazinator 10y agoBy the way, in the original C language by Dennis R. struct member names were in a kind of global namespace of all such names. So for instance if we had a struct stat { ... int st_ino; ... } struct dirent { ... int d_ino; ... } d; It was possible to access: d.st_ino; The compiler would just use the st_ino offset from struct stat, and generate an access into the struct bar b. This is the main reason members have funny prefixes in Unix structures. Once upon a time they had to, to prevent clashes.
- naasking 10y agoIndeed, it depends how one should interpret struct declarations. If you consider all structs structurally typed in C, then that should pass. If you only consider anonymous structs to be structurally typed, and named structs are the equivalent of newtype in Haskell (which is how C currently behaves I think), then the sample as written should fail, though using an anonymous struct for the alias would pass. I think the latter semantics makes for a better language because you can exploit the type checker to greater effect, but the former is certainly not unequivocally wrong.
- gpderetta 10y agoI think that at least for standard C and certainly for C++, the type system is meant to be nominal. Unnamed structures are better understood as having a unique but unnameable type. Anonymous structures are just weird and probably better understood as being just a syntactic aggregation without any type level connotation (C++ doesn't have anonymous structures; it does have anonymous unions though).
- naasking 10y agoI agree it's mostly nominal, but anonymous structs using structural typing semantics are incredibly useful. Consider for instance this general function pointer macro [1], which you can use with any number of arguments like so: fn(int, char, double) f = &functionMatchingSignature; and which you can invoke with just: double d = f.call(arg0, arg1); and it type checks correctly and ensure the correct argument types are applied. Way better than the typical, double(*f)(int, char). You can go even further and implement full closures in C11, curried and non-curried, and it's mostly all type safe using anonymous structs (see the rest if the repo for the work in progress). [1] https://github.com/naasking/cclosures/blob/typed-clo/closure/meta.h#L105 https://github.com/naasking/cclosures/blob/typed-clo/closure...
- gpderetta 10y agoI appreciate any form of clever preprocessor abuse, but I fail to see how the above, by itself, is an improvement to the fn pointer declaration syntax and why do you need the unnamed (not anonymous btw) struct. Couldn't you make the macro espand to a function pointer instead of one wrapped in a struct? Is it because you want to manipulate all such function pointers under some sort of static polymorphism and you require a uniformly named field?
- naasking 10y agoEvery other value declaration in C is <type> <name>, but function pointer values are different because the name to which its bound is right in the middle of the type. I understand the historical reasons, but it's unnecessarily obtuse, and this fixes that. Declarations of function pointer values are now just like any other value. The macro can't expand to a raw function pointer type without including the name to which it's bound in the macro arguments, once again introducing this obtuseness. Even worse actually, because the bound name is now indistinguishable from the types, where the ordinary syntax, while noisy, clearly distinguishes names from types. This style also matches the closure declaration style, ie. clo(T0, T1, ...), thus providing a seemless transition from function pointer values to actual function values which can carry environments.
- kazinator 10y agoStructs with the same structure being equivalent for the correctness of access, while still denoting incompatible types, is useful. Let's face it, if we have this: void some_library_api(foo *object, int arg); we want a diagnostic if we do bar b; some_library_api(&b, 42); otherwise we are almost back to C with no prototypes. We probably want that diagnostic even if it happens that both struct foo and struct bar are in scope and look identical. Maybe they are accidentally identical. The program then compiles. A new version of the API comes out and things break. Needless tensions arise between the users and the API provider.