5 ms·
Compare it with F# type providers where you don’t need to generate any struct and you can access the JSON object in a type safe way with autocompletion in your
by tigershark 9y ago
Compare it with F# type providers where you don’t need to generate any struct and you can access the JSON object in a type safe way with autocompletion in your IDE and maybe you will understand what is frustrating...
- deforciant 9y agoHey, I found this tool very useful https://mholt.github.io/json-to-go/ https://mholt.github.io/json-to-go/ when working with large JSON structs :)
- IshKebab 9y agoHow is that possible? You either need to specify the types up front (like Go) or give up on static type safety (like Python). It's impossible to have both.
- actsasbuffoon 9y agoActually, you can have both. Many languages in the ML family (Haskell, F#, etc.) can ensure complete type safety without having to define the shape up-front. Your data comes out of the JSON structure wrapped in a functor, and you can only access it in ways that deal with missing data. It’s hypothetically possible to recreate this approach in Python, but I don’t think it would be Pythonic.
- IshKebab 9y agoThat sounds like how you have to access JSON data dynamically in any statically typed language.
- actsasbuffoon 9y agoBut these other languages wouldn't necessarily remain type-safe. The functor approach doesn't involve any type casting, and null pointer exceptions are impossible.
- IshKebab 9y agoSo, like this sort of thing: int JsonValue::getAsInt(int defaultIfError); Or, in Go: func (JsonValue*) AsInt() (int64, error) Or are you just talking about it returning the value as a Rust-style Result sum type?
- 013a 9y agoIts hypothetically possible to recreate this approach in any language. Grab the item in the map at that key -> check that it exists -> check that the type is what you expect. Its not idiomatic in most mainstream languages. Except, that's how most JSON libraries which validate against a static structure are implemented, they just abstract the reflection away via some interface.
- actsasbuffoon 9y ago> Its hypothetically possible to recreate this approach in any language. Yes, I didn't mean to imply that I was providing an exhaustive list of languages where this approach could be implemented. I merely mentioned Python because it had been brought up several times in the thread. > Grab the item in the map at that key -> check that it exists -> check that the type is what you expect. I'd argue with that phrasing. You don't check for the key, nor do you check the type. That would imply something like an if-statement to ensure the presence of a key, and switching over a type. In such a system, you could forget to check for null, or make some other mistake that causes the program to fail at run time. The functor approach does not rely on the developer checking anything. It is 100% type safe, enforced by the type system, cannot crash at run time, and does not require you to provide a data type to extract the data into.
- tigershark 9y agoYou can in F#. https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/type-providers/index https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/typ...
- IshKebab 9y agoThat says you need a JSON schema file, which is deciding the type up front and basically how Go works too.
- lenkite 9y agoYou don't necessarily need a schema file. You can take a sample document as input for an inferred type. This is compile type meta programming https://fsharp.github.io/FSharp.Data/ https://fsharp.github.io/FSharp.Data/ https://medium.com/@maximcus/magic-of-f-type-providers-225b1169c7a0 https://medium.com/@maximcus/magic-of-f-type-providers-225b1...