O'Haskell OOP Polymorphic Functions

Magnus Carlsson magnus@cse.ogi.edu
Tue, 16 Jan 2001 10:23:06 -0800


You can use overloading for the definition of theValue instead:

  class TheValue a where theValue :: a -> Maybe Int

  instance TheValue Base    where theValue _ = Nothing
  instance TheValue Derived where theValue x = Just (x.value)

/M

Ashley Yakeley writes:
 > How do you do OOP-style polymorphic functions in O'Haskell? My first 
 > attempt looked something like this:
 > 
 > struct Base
 > 
 > struct Derived < Base = 
 >      value :: Int
 > 
 > theValue :: Base -> Maybe Int
 > theValue x = Just (x.value) -- problem line
 > theValue _ = Nothing
 > 
 > In the problem line, x is considered to be of type Base, so x.value gives 
 > an error. I tried replacing it with
 > 
 > theValue (x :: Derived) = Just (x.value)
 > 
 > ...but that doesn't work either.
 > 
 > -- 
 > Ashley Yakeley, Seattle WA