Here's three recent papers on extensible records:
* https://arxiv.org/pdf/2108.06296 * https://arxiv.org/pdf/2404.00338 * https://dl.acm.org/doi/pdf/10.1145/3571224
I'm not claiming these are definitive in any sense, but they are useful guides into the literature if any reader wants to learn more. (If you are not used to reading programming language papers, the introduction, related work, and conclusions are usually the most useful. All the greek in the middle can usually be skipped unless you are trying to implement it.)
From what I read, gp only asks for a way to express the type, not a way to change/extend the type of something during runtime.
Elm's current implementation does this just fine.
I like the beauty of an expressive type system as much as the next guy, but is there any scenario where:
type FooBar = mergeMaps Foo Bar
Is better for solving real-world problems than type FooBar = { "_foo", Foo, "_bar", Bar }
or whatever?As an aside, Purescript is one of the most fun languages I've ever used for frontend work, and I lament that Elm seems to have overtaken it in the FP community.
[0]: https://hgiasac.github.io/posts/2018-11-18-Record-Row-Type-a...
You made me want to try purescript :)
”The MLs” solved it decades ago. Standard ML has very nice record types.
But for some slightly complex reasons, most language designers find adhoc union types (which are required for this to work) a bad idea. See the Kotlin work related to that, they explicitly want to keep that out of the language (and I've seen that in other language discussions - notice how tricky it can be to decide if two types are "the same" or whether a type is a subtype of another in the presence of generalized unions) except for the case of errors: https://youtrack.jetbrains.com/issue/KT-68296/Union-Types-fo...
But these are obviously not equivalent: the first type is a map where all values are either strings or ints, and the second one is either a map where all values are strings, or all values are ints.
If that's confusing, consider: {foo: 1, bar: "2"}. It satisfies `Map<String, String | int>` but not `Map<String, String> | Map<String, int>`.
(In fact, the latter is a subtype of the former.)
P.S. You also seem to have misunderstood the toplevel comment about modeling record types as maps, as being about typing maps that exist in the language.
I was trying to emphasize how having type "combinations" ends up causing the type system to become undecidable as you end up with infinite combinations possible, but as I haven't really gone too deeply into why, I am having trouble to articulate the argument.
How are these equivalent? Wouldn't the latter result in {foo:1, foo:"two"}, where the former wouldn't?
type foo = < foo:int >
type bar = < bar:int >
type k = < foo; bar >
type u = < k; baz:int >
let f (x: <u; ..>) (\* the type annotation is not needed \*) = x#mActually getting values out of such a module-typed structure does involve some ceremony, however:
let f =
let module M = (val my_foobar) in
M.fooOn the other hand, thanks to the loose "duck typed" nature of templates, you can write functions that act on a subset of the fields of a type (even type checking it with concepts) while preserving the type of the whole object.
type Foo struct {
foo int
}
type Bar struct {
bar string
}
type FooBar struct {
Foo
Bar
} #include <iostream>
#include <string>
#include <type_traits>
// Concept to check for 'foo' field of type int
template <typename T>
concept HasFooInt = requires(T t) {
{ t.foo } -> std::convertible_to<int>;
};
// Concept to check for 'bar' field of type string
template <typename T>
concept HasBarString = requires(T t) {
{ t.bar } -> std::convertible_to<std::string>;
};
// Generic function that accepts T with either 'foo' or 'bar'
template <typename T>
requires HasFooInt<T> || HasBarString<T>
void printField(const T& t) {
if constexpr (HasFooInt<T>) {
std::cout << "foo: " << t.foo << std::endl;
} else if constexpr (HasBarString<T>) {
std::cout << "bar: " << t.bar << std::endl;
}
}
struct Foo {
int foo;
};
struct Bar {
std::string bar;
};
struct FooBar {
int foo;
std::string bar;
};
int main() {
Foo s1 { 42 };
Bar s2 { "hello" };
FooBar s3 { 100, "world" };
printField(s1); // prints: foo: 42
printField(s2); // prints: bar: hello
printField(s3); // prints: foo: 100 (prefers foo due to order of checks)
return 0;
} type Foo interface {
Foo() int
}
type Bar interface {
Bar() string
}
type FooBar interface {
Foo
Bar
}
Then functions that accept a Foo will also happily take a FooBar. Does not solve the problem of passing a FooBar[] to a function that expects Foo[] but that can be solved with generics or a simple function to convert FooBar[] to Foo[]. type HasFooBar s = (HasField "_foo" s String, HasField "_bar" s String) => s type Person = User DESELECT password
type Invoice = Customer JOIN Inv type Person = Omit<User, 'password'>
type Invoice = Customer & { invoice: Inv }
// or
type Invoice = Inv & { customer: Customer } type foo_bar = < _foo : int; _bar : string >
For a family of types matching any object with those methods, I think you can write something like type 'a has_foo_bar = < _foo : int; _bar : string; .. > as 'a
What we want to express here is an object with a map of properties (name to type):
For the OOP minded: And also compose those: But at compile-time, of course.Have any languages achieved this? I know TypeScript can do some of these things, but it's clunky.