You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We'd like to implement a function that decodes an mcl string of json data, and turns it into the correct mcl type. This is a difficult challenge, but definitely doable.
Design
mcl is a statically typed language (like golang) and since it's impossible to guarantee the "shape" (type) of the input data, then we need to employ some tricks. Let's start off with the function signature and an example:
$mcl_value = json_decode("[]str", $data)
The first arg is a string in the mcl type format. This can be parsed with types.NewType(...)
$data should be a string of json data.
Internal API's
Since we build functions of different types depending on the input type, we'll need the fancier function signature of:
which, before type unification, lets you look at what we happen to already know statically, and build a function that fits that expectation! Of course if the type isn't known, or if it's an invalid type, we just error early, and your program won't compile!
Bad input data?
If the $data doesn't match the expected type, or if it changes at runtime, to a new version that doesn't match the expected type, then it's perfectly legal to fail. As a fancy bonus, if it happens that the data is known statically at compile time, then we should definitely parse it early. This will happen for more and more data as the compiler gets more clever about knowing what data is static.
Hints
A rough POC to show this is possible is here. This should hint that the function should be recursive. If it's possible to combine it with one of the existing functions in lang/types/ then that's even better.
bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null
Future
If it turns out to be possible, we could also consider accepting a single arg variant of the function which requires that it can statically read the json data at compile time to determine the type. I don't know if this is possible with the golang library, we'd have to figure that out first.
Sugar
Obviously if the type argument changes at runtime, then we would have to error/shutdown. For this reason, it may be worth adding this with some compiler sugar so that it's not possible to do so.
Questions?
Don't be shy, let me know if you have questions.
The text was updated successfully, but these errors were encountered:
Is there any situation where inferring types dynamically would be acceptable in mcl?
Need more information about your question:
I think the function graph needs to have all static types, but there are situations where we decide a type at compile time, but allow a different type to be a runtime error... For example, theoretically we might allow this to be func(str, str) str but the actual type represented by the first arg (string representation) and the json, could be variable and it works as long as they match.
We'd like to implement a function that decodes an mcl string of json data, and turns it into the correct mcl type. This is a difficult challenge, but definitely doable.
Design
mcl
is a statically typed language (like golang) and since it's impossible to guarantee the "shape" (type) of the input data, then we need to employ some tricks. Let's start off with the function signature and an example:The first arg is a string in the mcl type format. This can be parsed with types.NewType(...)
$data should be a string of json data.
Internal API's
Since we build functions of different types depending on the input type, we'll need the fancier function signature of:
which, before type unification, lets you look at what we happen to already know statically, and build a function that fits that expectation! Of course if the type isn't known, or if it's an invalid type, we just error early, and your program won't compile!
Bad input data?
If the $data doesn't match the expected type, or if it changes at runtime, to a new version that doesn't match the expected type, then it's perfectly legal to fail. As a fancy bonus, if it happens that the data is known statically at compile time, then we should definitely parse it early. This will happen for more and more data as the compiler gets more clever about knowing what data is static.
Hints
A rough POC to show this is possible is here. This should hint that the function should be recursive. If it's possible to combine it with one of the existing functions in lang/types/ then that's even better.
Testing
You'll need a few tests for this patch, including the obvious cases listed here:
https://pkg.go.dev/encoding/json
Future
If it turns out to be possible, we could also consider accepting a single arg variant of the function which requires that it can statically read the json data at compile time to determine the type. I don't know if this is possible with the golang library, we'd have to figure that out first.
Sugar
Obviously if the type argument changes at runtime, then we would have to error/shutdown. For this reason, it may be worth adding this with some compiler sugar so that it's not possible to do so.
Questions?
Don't be shy, let me know if you have questions.
The text was updated successfully, but these errors were encountered: