3 ms·
The only thing is I wish there was a way to safely lift a row-polymorphic record to a named record type if the compiler can determine it has all the required fi
by daxfohl 1y ago
The only thing is I wish there was a way to safely lift a row-polymorphic record to a named record type if the compiler can determine it has all the required fields. Haven't seen any languages that offer this yet though.
- continuational 1y agoFirefly can construct any named record from any other record (named or anonymous): let point = (x = 5, y = 7) let point3d = point.Point3d(z = 0) We included this feature specifically to make it easy to use named and unnamed records together. More here: https://www.firefly-lang.org/ https://www.firefly-lang.org/
- owlstuffing 1y agoThe manifold project[1] for Java provides that feature as "Structural Interfaces" which supports polymorphic variants. The project also supports tuples that behave similarly. var person = (name: "Joe", age: 35); . . . Person p = person; // tuple's name, age properties satisfy Person record 1. https://github.com/manifold-systems/manifold https://github.com/manifold-systems/manifold
- gsliepen 1y agoWhen is it "safe" though? What if the row-polymorphic record has more fields than the named record? Should those just be discarded? If it has exactly the same fields, then in C++ at least you can use std::get<NamedRecordType>(row_polymorphic_record) if you are using static polymorphism, and dynamic_cast<NamedRecordType*>(row_polymorphic_record) in case of dynamic polymorphism. Note that the compiler cannot determine anything at compile time when you have polymorphic objects, so it's just going to emit code that at runtime will throw an exception.