-
Notifications
You must be signed in to change notification settings - Fork 464
Should we have an unknown top-level type #6119
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
Comments
The unknown type is in the compiler, it's called |
I'm advocating to expose the unknown type in the language itself maybe as a keyword like string or int. Definitely not specific to the Core library. And also that the user never needs to call some "make" function. Instead the compiler allows assignment to unknown always because it is always safe. Just like TypeScript. It is awkward to use a make function for that. |
In case I wasn't clear... in my own code I made my own A bunch of functions take a |
It's clear. This is for Core. |
Ok I wish I understood what you're saying. I'm curious how you envision this in Core. Will there be a |
Sorry if I was unclear. Type unknown exists already.
in |
So if I have a function that takes an Unknown.t and I want to call it I will ALWAYS have to do something like this below.
Or can you make the compiler do something special with Unknown, like TypeScript, so anything can be assigned to it without calling the |
Thar's correct. One can add a subtyping rule (so one does not need to call |
If people start using unknown very extensively, then the subtyping rule can be considered. |
Notice a more compelling, but analogous, request would be to automatically promote (certain) values to option so one does not need to use |
(some) people would likely object that it is "too magic" -- basically optimising the expert's use case at the expense of possibly confusing beginners |
It's up to debate, but likely the ecosystem might benefit more from a 10X influx of beginners than 2X influx of experts. |
The beginner will go for Very interesting case of If someone writes a function that should take an I was imagining a future version of unions would have people author modules with the shape like this. Each option in the union would be a module name. So discrimination and pattern matching happens with code the user writes which is totally customizable based on
I agree we have no idea who would use |
HUGE misunderstanding here. Maybe I never actually tried typing Should this be added to the docs as a built-in type? |
Glad it's clear now. |
Please see rescript-lang/rescript-lang.org#670 |
I saw your note saying there is an
unknown
type somewhere in JSError but I couldn't find it or determine if it resolves the issue here. I've been experimenting with some code that makes use of theunknown
type. Below is an example. Notice the syntax error at the very bottom. Is it possible to create anunknown
type that you can safely assign anything to without requiring the developer to do something likeUnknown.make
? In TypeScript there are two top-typesany
andunknown
(see here). In ReScript whenever I see'a
that looks likeany
to me. I've seen ReScript type definitions that use something like<+'a>
and thought maybe that technique would help but I don't understand it.In the example below, I want developers to author instances of
Pattern
. It is safest to author these whenisTypeOf
takes anUnknown.t
because if it were just an'a
then the developer, if not careful, could treat the function parameter like a string or anything when it really isn't, and this can cause run-time crashes. But when using aPattern
I want to be able to pass in anything and not have to manually covert it to unknown usingUnknown.make
which is kind of weird. So from inside theisTypeOf
the parameter should look likeUnknown.t
but when calling it from the outside I want it to accept an'an any
.If this isn't possible, I wonder if we should have a
unknown
top-level type exposed in the language. This would be useful to provide more safe external functions; make them return anunknown
if we don't know exactly what we're going to get. We could use this inObject
module ofCore
when we're getting property values. And maybe genType exports these asunknown
without any shims.The text was updated successfully, but these errors were encountered: