<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <font face="monospace"><font face="sans-serif">Imagine the following
        data type:</font><br>
      <br>
      data Person = Child { childAge :: Int, school :: School }<br>
                  | Adult { adultAge :: Int, job :: Job }<br>
      <br>
      <font face="sans-serif">Both the alternatives share an "age"
        field, but in order to access it we are obliged to match all the
        constructors:</font><br>
      <br>
      personAge :: Person -> Int<br>
      personAge (Child {childAge = age}) = age<br>
      personAge (Adult {adultAge = age}) = age<br>
      <br>
      <font face="sans-serif">Is there a way to define a common field in
        a data type (somehow like inheritance in the OOP world)?<br>
        It would be practical if we could define "age :: Int" as a
        common field and do something like:</font><br>
      <br>
      personAge (_ {age = age}) = age<br>
      <font face="sans-serif"><br>
        An alternative solution could be extracting the varying fields
        in a different type:</font><br>
      <br>
      data WorkingPerson = Child School | Adult Job<br>
      <br>
      data Person = Person { age :: Int, workingPerson :: WorkingPerson
      }<br>
      <br>
      <font face="sans-serif">Or to make Person a type class (which
        would probably require existential types in order to be useful).<br>
        <br>
        But the first one looks simpler and more natural.<br>
        <br>
        Does this feature exist (maybe in other forms) and, if not, is
        there a reason?<br>
        Would it make sense to have a GHC extension (or even to make a
        Template Haskell hack) for this feature?</font><br>
      <br>
    </font>
  </body>
</html>