Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a "type class" called "type class"?

When diving deeper into Scala I hit the term type class. It had been confusing because a class is a type and a type could be a class in Scala and "type" and "class" are in itself abstract terms.

After reading some examples I got a feeling what a type class could be, but maybe there is an easier approach to get a mental hook (picture, concept) to what a type class is by explaining why it had been named like that.

like image 275
Lutz Avatar asked Sep 09 '11 10:09

Lutz


People also ask

What do you mean by type class?

In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types.

What is a type class in Java?

A type class is an interface that defines some behavior. More specifically, a type class specifies a bunch of functions, and when we decide to make a type an instance of a type class, we define what those functions mean for that type.

What are type classes used for?

Type classes are a powerful tool used in functional programming to enable ad-hoc polymorphism, more commonly known as overloading.

What is type type?

noun. a number of things or persons sharing a particular characteristic, or set of characteristics, that causes them to be regarded as a group, more or less precisely defined or designated; class; category. a criminal of the most vicious type.


2 Answers

In Haskell there are no classes in Java/Scala sense, so this term was free to be used (in the sense "class of types", as Johannes Weiß says).

It isn't used in official Scala documentation, but people coming from Haskell found they could emulate Haskell type classes in Scala and reused the name.

like image 87
Alexey Romanov Avatar answered Sep 20 '22 05:09

Alexey Romanov


Because it classes (together) types "having" a common set operations defined by the type class?

It feels very natural to me.

like image 40
Johannes Weiss Avatar answered Sep 20 '22 05:09

Johannes Weiss