A
arktype•5w ago
frolic

codecs

with morphs, I am thinking of arktype types as sort of input/output functions for const t = type(...) t.allows(input) tells if you if input is the right shape t(input) takes input and turns it into output (or errors) is there a way to extract a def/type of the input type (pre-morph) vs output type (post-morph)?
171 Replies
frolic
frolic•5w ago
I saw t.in and t.out but I don't think they give me what I want, or at least their types don't if a type has no morphs, then in/out would be ~equivalent
frolic
frolic•5w ago
I am prob misunderstanding arktype internals but my assumption was that the 6th assertion would be false
No description
frolic
frolic•5w ago
might better describe the behavior
No description
Unknown User
Unknown User•5w ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•5w ago
I actually made this improvement recently to help show the discrepancy bewteen the type information that is available at runtime and what we can statically determine. @frolic I'm guessing in your case, .out is typed as unknown. That's because if you just apply an arbitrary transformation, we can infer the return statically, but at runtime there's no way to know what you expect your function to return, so .out is just unknown. If you want a validated output type, you can .pipe to another type or .to(def), then it will infer the validated output and make it available at runtime
frolic
frolic•5w ago
I am rethinking my API around this because I don't think I can get what I want with just a single type with morphs
ssalbdivad
ssalbdivad•5w ago
Well yeah if you need the validated output you'd need a type for it just like the input
frolic
frolic•5w ago
oh I see what you're saying now would it make sense to allow describing the output type of morphs? wondering if its a small enough API change that enables a runtime validator for output
ssalbdivad
ssalbdivad•5w ago
You're misunderstanding, this is already possible you just have to provide one Pipe to a type You can pipe from an input, to a morph, then to a type Then it will be available and inferred
frolic
frolic•5w ago
at a property level?
ssalbdivad
ssalbdivad•5w ago
Types are composable, so yeah. You could do it with nested tuple expressions although like I said this is the sort of territory where chaining is usually more natural
frolic
frolic•5w ago
like string.integer for example, string is the input type and it morphs to an integer, but does it also pipe to number?
ssalbdivad
ssalbdivad•5w ago
The builtin keyword doesn't right now because I don't want to perform redundant validation and it's relatively rare people need to extract in/out if they already have the inferred type. That said I do want to add a feature that allows you to essentially "cast" the output type of a morph so you can introspect it without adding validation steps
frolic
frolic•5w ago
okay another question/approach: can I validate that a type def has no morphs? as in no chance of anything inside resolving to unknown
ssalbdivad
ssalbdivad•5w ago
Yeah there is a .internal.includesMorphs()
Want results from more Discord servers?
Add your server