Type Classes
============
```Example from 1 languages: Rust
// https://doc.rust-lang.org/book/ch10-02-traits.html
trait ToTypedJson {
fn to_typed_json(&self) -> String;
}
impl ToTypedJson for i64 {
fn to_typed_json(&self) -> String {
return format!("{{\"type\": \"int\", \"value\": {}}}", self)
}
}
fn print_as_typed_json(x: impl ToTypedJson) {
println!("{}", x.to_typed_json());
}
fn main() {
print_as_typed_json(123);
}
```
```Example from 1 languages: Scala
// https://docs.scala-lang.org/scala3/book/ca-type-classes.html
trait TypedJsonConvertible[A]:
extension (a: A) def toTypedJson: String
given TypedJsonConvertible[Int] with
extension (x: Int) def toTypedJson: String =
s"{\"type\": \"int\", \"value\": ${x}}"
def printAsTypedJson[T: TypedJsonConvertible](x: T): Unit =
println(x.toTypedJson);
printAsTypedJson(123);
```
```Example from 1 languages: Haskell
class ToTypedJson a where
toTypedJson :: a -> String
instance ToTypedJson Integer where
toTypedJson x =
"{\"type\": \"int\", \"value\": " ++ (show x) ++ "}"
printAsTypedJson :: ToTypedJson a => a -> IO ()
printAsTypedJson x = do
putStrLn (toTypedJson x)
main :: IO ()
main = do
printAsTypedJson (123 :: Integer)
```
*
Languages *with* Type Classes include Rust, Scala, Haskell, Coq, Idris
*
Languages *without* Type Classes include JavaScript, C, Python, Java, C++, TypeScript, Kotlin, OCaml
*
View all concepts with or missing a *hasTypeClasses* measurement
http://pldb.info/../lists/explorer.html#columns=rank~id~appeared~tags~creators~hasTypeClasses&searchBuilder=%7B%22criteria%22%3A%5B%7B%22condition%22%3A%22null%22%2C%22data%22%3A%22hasTypeClasses%22%2C%22origData%22%3A%22hasTypeClasses%22%2C%22type%22%3A%22num%22%2C%22value%22%3A%5B%5D%7D%5D%2C%22logic%22%3A%22AND%22%7D missing
http://pldb.info/../lists/explorer.html#columns=rank~id~appeared~tags~creators~hasTypeClasses&searchBuilder=%7B%22criteria%22%3A%5B%7B%22condition%22%3A%22!null%22%2C%22data%22%3A%22hasTypeClasses%22%2C%22origData%22%3A%22hasTypeClasses%22%2C%22type%22%3A%22num%22%2C%22value%22%3A%5B%5D%7D%5D%2C%22logic%22%3A%22AND%22%7D with
*
Read more about Type Classes on the web: 1.
https://en.wikipedia.org/wiki/Type_class 1.
Built with Scroll v178.2.3