Top 1K Features Creators Events Podcasts Books Extensions Interviews Blog Explorer CSV

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

*

Read more about Type Classes on the web: 1.

- Build the next great programming language Add Add Prompt Issues About Search Keywords Livestreams Labs Resources Acknowledgements