// 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);
}// 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);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.