logoalt Hacker News

Towaway69today at 3:14 PM2 repliesview on HN

If I understand correctly, generics here are “type agnostic” functions in a strongly typed language?

Why not just use a weakly typed language and add type checking were needed?

It seems strange to put in so much effort for type checking then only to throw it overboard by implementing something that ignores type.


Replies

dragonwritertoday at 3:51 PM

> If I understand correctly, generics here are “type agnostic” functions in a strongly typed language?

They are not.

They are applicable to a well-defined range of sets of input types, instead of a single specific type combination, and produce a well-defined output type for each input type combination.

> It seems strange to put in so much effort for type checking then only to throw it overboard by implementing something that ignores type.

Generics do not ignore types. That's kind of the whole point.

tethatoday at 4:17 PM

I'm assuming you meant statically/dynamically type checked languages.

Generic functions to not ignore types. An `inThere::a list -> a -> bool` very much enforces that the list passed in, as well as the element have the same type. With a sufficiently powerful type system, this allows for statically checked code that's not much less flexible than dynamically checked code.

Observing current developments in Python, but also Rust gives me the impression that dynamically typed languages were more a reaction to the very weak type systems languages like C or Java provided back in the day. A lot of Python code has very concrete or rather simple generic types for example - Protocols, Unions, First-class functions and Type parameters handle a lot. The tools to express these types better existed in e.g. Caml or Haskell, but weren't mainstream yet.