Hacker News new | ask | show | jobs
by mapcars 665 days ago
I want my types to be verified before runtime, what should I do with type error at runtime? Yes it will leave me a better error message in logs, but build-time type check will prevent me even rolling this code out.

So yes, its categorically better.

1 comments

> I want my types to be verified before runtime, what should I do with type error at runtime?

It is weird to me you cannot think of uses for types and handling their errors at runtime. Look at projects like FastAPI (which uses pydantic) for an an example where having types at runtime has been handy.

Also, if types are enforced at runtime logging is better, debugging is better, and you have more options for data validation and doing dynamic or metaprogramming likely more easily because you can evaluate these things at runtime. By types being built in, the language gives you a richer set of tools to work with and more options when and how to enforce types.

> Look at projects like FastAPI (which uses pydantic) for an an example where having types at runtime has been handy.

What is it, they have nice error messages? I don't expect them to fix code at runtime when type error happens.

> Also, if types are enforced at runtime logging is better, debugging is better

These can't even happen with type checking in the first place.

> you have more options for data validation

In my experience serializing/deserializing data is pretty much the only place where its useful, and it doesn't have to be built-in on a language level, Pydantic is doing it well enough.

> doing dynamic or metaprogramming likely more easily

This is where you lose me, I have never seen it being useful in reasonable large projects in popular languages. Dynamic or metaprogramming only works in a very specific category of languages (lisp-likes), doing it in any other language will become a problem sooner or later.

> more options when and how to enforce types

Again its only on serialization level, apart from that type checking will cover everything before runtime, it's weird to me how you can think that encountering problem later has any advantage of doing it earlier.

> What is it, they have nice error messages? I don't expect them to fix code at runtime when type error happens.

FastAPI validate both data (serialization) and schemas (used to create APIs and specs). Pydantic can also help with safe casting, not sure if FastApi uses that or not, but it is available

> These can't even happen with type checking in the first place.

I mean when you're debugging any problem or learning an API. It is nice to have rich types built in, especially while in a REPL, where you can inspect types.

> In my experience serializing/deserializing data is pretty much the only place where its useful, and it doesn't have to be built-in on a language level, Pydantic is doing it well enough.

Pydantic relies on Python having types built into the language and available at runtime.