Emulating named tuple elements #1832
Replies: 3 comments 1 reply
|
It made me remember NamedType C++ library. Maybe a typealias can help for improving readability (Not sure as I didn't play much with the new frontend). A sample example from above library: template<typename Function>
using Comparator = NamedType<Function, struct ComparatorTag>; |
|
This is a code smell: Specifically, equality constraints involving a concrete type (i.e., not introduced by the signature itself) are often fishy and I would probably warn them in a linter. They just make the type signature more complicated by adding new implicit parameters. Typer will be more compliant if you write this:
I agree. Given that my above example works I suspect the typer just doesn't dealias the ascription when it visit the RHS of the declaration. That should be an easy fix. |
The reason is that tuple labels would make it much more difficult to soundly express tuple consing in the type system, which is useful for writing this in the standard library: In English: given a type It is possible to express labels as types but that is not trivial. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I played around with the new frontend and experimented with emulating named tuple members. I assumed I would use a tag struct that gives me a unique type, and my goal was to arrive at a tuple initializer expression that requires writing the correct tag before corresponding to each part.
When using a similar technique in libraries, we would need to export the name tag, and when conflict arises, qualify it with our module name like
MyModule.x.I remembered that there was a reason why we didn't add named tuples as a language feature, but didn't exactly remember the reason. We can use this thread to record any of those previous concerns & more.
Side note: Some of my commented out naive trials feel they ought to work. Can't we use an implicit base qualification inside a tuple expression when its type is ascribed?
All reactions