-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: checked cast with TypedMatcher #8394
Conversation
2e79cfd
to
360d848
Compare
While searching for other places to use this, most of the shape checks are to validate the type that a value already has statically. So there's no value in the narrowing. For example, a function param We could have some utility type for indicating the soundness of the type annotation. One way is a utility type Going the other way, we could have a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one comment, otherwise LGTM!
packages/internal/src/types.d.ts
Outdated
@@ -18,3 +20,17 @@ export declare class SyncCallback< | |||
|
|||
public isSync: true; | |||
} | |||
|
|||
declare const type: unique symbol; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little weird to me to use the identifier type
here when it's also a TypeScript keyword. Could you please name it something like TypedMatcherParameter
or anything that you think would be better.?
@turadg , while I would still appreciate the verbal explanation we arranged, please don't wait on that to merge this. I understand this well enough that I'd be happy (overjoyed, actually) to see it merged based on @michaelfig 's approval. You can then clear up my remaining confusions post-merge. |
@erights before merging I'd like to include some usages of it in this PR, to validate the design. If you have some in mind feel free to push commits. |
360d848
to
94e2ff6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My memory of this has gotten stale, so perhaps I forgot. But,
Why is this in agoric-sdk? It seems much more natural to include it in endo, either @endo/patterns
or @endo/exo
. Isn't this useful for anything building on those?
That it would be useful in Endo does not seem to me a reason to exclude it from agoric-sdk. I started in in agoric-sdk so that I could evaluate its utility in agoric-sdk and iterate on the design. Once the design and implementation are acceptable,I think the next step is to land it here. Then a separate PR can copy it to Endo and a third PR can make agoric-sdk use that once it's published. |
That seems like 3x as much work, spread out over at least 10x as much time. If we're agreed that the correct final destination is, say, @endo/patterns, why not just do it? I suspect we have conflicting unstated assumptions, so probably best to postpone this to a more general engineering discussion. cc @ivanlei @kriskowal |
|
My assumptions: adding code to Agoric SDK should be frictionless and entirely based on what pragmatically benefits our platform. We should not implement experimental features in Endo until we have gained experience with them and can justify the additional cost of maintaining them for a wider audience. I am in favour of exploring the path laid out in this PR, and committing it to Endo once we have learned its shape better. |
Deploying agoric-sdk with Cloudflare Pages
|
This argument presumes there is some clear conceptual boundary between agoric-sdk and endo, such that we're willing to put experiments on agoric-sdk master that we're not willing to put in endo master. If the argument were to keep an experiment encapsulated within the one package that uses it, that I'd understand. A package is a modularity boundary I understand. But once it gets outside a single package, I don't get it. Most things that start out this way never take the second step, leaving them misplaced for ages. That migration is never the most urgent thing to do right now, so it never gets done. But since the choice seems to be to approve having it start in the wrong place vs not having it at all, I will review this as an agoric-sdk PR. Please do file an issue to migrate it to endo when "ready". Then we'll at least have a better chance at eventually getting around to it. |
I do understand and sympathize with these latency costs. I've paid them many times, with changes that should have taken a day instead taking months (attn @ivanlei @kriskowal ). But it is worth noting that if we did an endo-release-agoric-sdk-sync every night, most of this latency would disappear. So please at least do file that issue. Thanks. |
@turadg , since your endojs/endo#2238 is in flight right now, could you bundle these changes with that? I'd be much happier reviewing them there ;) |
I am proceeding with the review here now. I did not mean to imply otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for the one declaration that I cannot yet review, all the rest LGTM.
the recent |
specimen: unknown, | ||
pattern: P, | ||
label?: string | number, | ||
) => asserts specimen is P extends TypedPattern<any> ? PatternType<P> : unknown; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I understand it, OMG this is cool!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this eventually migrate into endo? Into the @endo/patterns package? I hope so. If you agree, would be good to comment on that expected migration here, or somewhere relevant.
Similarly, should #6160 have been an endo issue rather than an agoric-sdk issue, with the eventual fix also eventually being in endo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you agree, would be good to comment on that expected migration here, or somewhere relevant.
I expect it'll happen when there's some trigger.
should #6160 have been an endo issue rather than an agoric-sdk issue, with the eventual fix also eventually being in endo?
Could have been. Though it's much faster to being useful by making it work in agoric-sdk and moving it to Endo when it's stable.
*/ | ||
|
||
/** | ||
* @template {AssetKind} K |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, which I've written code like this template declaration, I revise it to
* @template {AssetKind} K | |
* @template {AssetKind} [K=AssetKind] |
since the supertype constraint is usually also a fine default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case there's no way the to omit the parameter because it's positional
@@ -0,0 +1,23 @@ | |||
// @ts-check | |||
import { mustMatch as typelessMustMatch } from '@endo/patterns'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we eventually move this into @endo/patterns so the only mustMatch
is the typed one, and the typeless one is never exported? (Modulo the transition costs, which I expect will be painful but tolerable.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think once we've had some stress testing of it so we know it won't change much.
/** | ||
* @import {MustMatch} from '@agoric/internal'; | ||
*/ | ||
import { mustMatch as typelessMustMatch } from '@endo/patterns'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is a layering problem causing this duplication, or could all uses of this one use the @agoric/internal one instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agoric/store
is actually lower than @agoric/internal
, unfortunately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Could it be moved and exported from @agoric/store for now? As a permanent home, that would be weird. But if it's ultimate destination is @endo/patterns, well, @agoric/store still does deprecated reexporting of many things from @endo/patterns, since patterns lived there first.
I leave this to your esthetics. Your status quo is not doing an offensive amount of duplication; just a tiny amount.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that before merging, so I won't merge tonight
packages/time/src/timeMath.js
Outdated
@@ -1,9 +1,16 @@ | |||
import { Nat } from '@endo/nat'; | |||
import { Fail, q } from '@endo/errors'; | |||
import { mustMatch } from '@endo/patterns'; | |||
import { mustMatch as typelessMustMatch } from '@endo/patterns'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I'm confused. This is unlikely to be a layering constraint. Why not use the one from @agoric/internal ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because @agoric/time
currently doesn't depend on anything in @agoric
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason not to change it to depend on @agoric/internal? When it doesn't create a layering problem, that's what I'd do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I defer to @warner. If no objections then I'll do it in a follow-up PR. (probably importing from @agoric/store
which is almost Endo :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it is thematically what @agoric/internal is for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm glad you like the tred visualization ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, using @agoric/store as a staging ground for moving to endo makes sense to me. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I'm excited to start using this! Thanks.
refs: #6160
Description
I ran again into the need for type narrowing with a shape object: https://github.com/Agoric/agoric-sdk/pull/8385/files#diff-b17d46d065ac769cdf1d70471b16d141f28672965c18522d58d39722d4852ad4R329-R331
endojs/endo#1721 approached the general problem of inferring the type from the shape. But until we have that, we can at least move the type description onto the shape object so users of that shape can automatically get the type a match implies.
With this, the code referenced above would be,
Security Considerations
n/a
Scaling Considerations
n/a
Documentation Considerations
Once this settles in agoric-sdk, we may want to move it into Endo. Alternately, Endo waits for more general support.
Testing Considerations
Has a test. Maybe should use tsd instead of Ava
Upgrade Considerations
n/a