top | item 41794879

(no title)

bluepnume | 1 year ago

You find it weird that a type system doesn't do runtime validation? Is that common in many other languages?

discuss

order

dhruvrajvanshi|1 year ago

Well in most statically typed languages with a VM (Java/C#), there's some sort of runtime validation

In Java Object something = new Map(); String badCast = (Object) something; // This line would throw a ClassCastException because something is not a String

This has the advantage of throwing an exception in the correct place, instead of somewhere down the line.

yen223|1 year ago

Other statically-typed languages do have to deal with the problem of parsing external objects. In my experience, none of them have parsers as good as Zod in terms of ergonomics.

dhruvrajvanshi|1 year ago

> Other statically-typed languages do have to deal with the problem of parsing external objects.

Well that's just blatantly not true. Which languages are you thinking of? I'm sure I'm misunderstanding what you said.

I can't think of a single server side language that doesn't have to parse external untyped objects. That's where these serialization libraries come into play.

For example, in Kotlin, you declare a data class and mark it as @Serializable and it generateds `toJSON/fromJSON` for you. IMO it's a much better experience than Zod.