by glittershark · 5y ago · 0 comments
struct Person<F> {
pub name: F<String>,
pub age: F<u32>,
}
where you can then have `Person<Option>` be a person that may or may not have all fields filled, and `Person<Box>` be a person with all fields definitely filled. This is something I find myself reaching for surprisingly frequently when writing rust, and I feel like it's a missed benefit of implementing higher-kinded types.[0]: https://reasonablypolymorphic.com/blog/higher-kinded-data/
All that said, I'm really excited to have const generics land! Props to all the amazing work by withoutboats and the entire rust team.
type Person = {
name: string;
age: number;
}
type SomeFieldsMissing = Partial<Person>;
type AllFieldsRequired = Required<Person>; // No real change in this case
Where Partial and Required are defined like: type Partial<T> = { [P in keyof T]?: T[P] | undefined; }
type Required<T> = { [P in keyof T]-?: T[P]; }
What would you think of something like this ("mapped types")? They can get fairly powerful: https://www.typescriptlang.org/docs/handbook/2/mapped-types.... struct Person<F> {
pub name: F<String>,
pub age: Option<u32>,
}
With `Person<Box>` still leaving `age` as optional. type RequiredKeys<O, K extends keyof O> = Omit<O, K> & Required<Pick<O, K>>
type Person = {
name?: string
age?: number
}
type RequiredName = RequiredKeys<Person, 'name'>
The opposite (making some of the fields optional, without affecting the rest) would be written like this: type OptionalKeys<O, K extends keyof O> = Omit<O, K> & Partial<Pick<O, K>>If one caller passes our type narrower with {age:F<u32>} then the return value of the function will be narrowed to that type, but a broader criteria can still be specified for the generic instance.
(Also, it shouldn't be Box; you'd want a bog-standard generic newtype for this, not an extra heap allocation.)
Edit, for that matter, how do you deal with a `Person f` that includes a `f (Maybe Something)` without confusing "the user didn't specify a `Maybe Something`" with "the user specified a `Maybe Something` with value `Nothing`" (or vice versa)?
type Maybe<T> = {hasValue: false} | {hasValue: true, value: T}
type Shoehorn<T> = { [P in keyof T]-?: Maybe<Maybe<P>> }`Compose Maybe Maybe T` (equivalently `Maybe (Maybe T))`), has N + 2 values (where N in the probably-infinite number of values T has):
. N values consisting of a T
. 2 different values that do not include a T
It's also equivalent (via 1 + (1 + N) = (1 + 1) + N) to `Either Bool T`.
And yes, `Person f` contains values of type `f Whatever`.
> it either is undefined or isn't.
Values are never undefined. They might be defined as a object that's called "undefined", but that is a specific, defined, value. And, apropos of the above, it's a single value, out of the two distinct non-T values being represented. Which is part of why the example at https://news.ycombinator.com/item?id=26277598 looks horrible - it seems like it would confuse the two different non-T values with each other if `Person f` includes a `f (Maybe T)`.
Edit: actually, clarified the previous comment a bit, too.
https://github.com/tim-group/higher-kinded-lifecycle/blob/ma...
(in this code, "idea" is a domain concept from the firm i worked for at the time - basically a recommendation to buy a stock, which is 'opened' on a certain date, and 'closed' when it no longer seems like a good recommendation)
My collegues didn't like it, and stuck to using separate types for objects in different stages of the lifecycle!
https://rustyyato.github.io/type/system,type/families/2021/0...
What's a practical use case for it? It's cool in theory, but I struggle to come up with something that doesn't use at most 2 implementations (just copy the struct then), or some API where the types don't mix and can be generated via a macro instead.