Call an object method: Difference between revisions

→‎{{header|Haskell}}: added solution
m (omit from assembly)
(→‎{{header|Haskell}}: added solution)
Line 650:
L := [cp1]
end</lang>
 
=={{header|Haskell}}==
 
Haskell doesn't have objects in OOP sence (i.e. first-class sitizens
appearing in runtime with incapsulated state and ability to send messages (methods) to other objects).
 
Haskell has first-class immutable records with fields of any type, representing abstraction and incapsulation (due to purity).
Polymorphism is implemented via type-classes, which are similar to OOP interfaces.
 
Dummy example:
<lang haskell>data Obj = Obj { field :: Int, method :: Int -> Int }
 
-- smart constructor
mkAdder :: Int -> Obj
mkAdder x = Obj x (+x)
 
-- adding method from a type class
instanse Show Obj where
show o = "Obj " ++ show (field o) </lang>
 
<pre>*Main> let o1 = Obj 1 (*5)
*Main> field o1
1
*Main> method o1 6
30
*Main> show o1
Obj 1
 
*Main> let o2 = mkAdder 5
*Main> field o2
5
*Main> method o2 6
11
*Main> show o2
Obj 5</pre>
 
=={{header|J}}==
Anonymous user