← Back to Examples

08 Typeclasses

Cure language example demonstrating various features

Source Code

# Example: Typeclass System Demonstration
module TypeclassDemo do
export [main/0]

import Std.Io [println]

# Define a Person record
record Person do
  name: String
  age: Int
end

# Define Show typeclass locally
typeclass Show(T) do
  def show(x: T): String
end

# Show instance for Person
instance Show(Person) do
  def show(p: Person): String = "Person"
end

# Show instance for Int
instance Show(Int) do
  def show(x: Int): String = "Int"
end

# Generic debug function
def debug_value(x: T): T where Show(T) =
  println(show(x))
  x

def main(): Int =
  println("Hello")
  debug_value(42)
  0

end