Functional Objects are non-mutable objects. When you modify an object it returns the modified object instead of doing it in place.

Goals

Test various programming languages ability to code functional objects.

The test objects are Point2d and Point3d. No interface inheritance is needed, but it can help.

Goal #1
Point2d and Point3d must have methods: and you must have external access to Point3d'z:

Goal #2
Same as goal #1, but adding the (binary) method: Adding a Point2d and a Point3d should cause a compile-time error or runtime-error. This imply an absence of subtyping relationship between Point2d ad Point3d.

Goal #3
Same as goal #2, but having subtyping (or simulating it) between Point2d and Point3d for "add" method. This means the following combinaisons should be allowed: Note: p1.add(p2) returning a Point2d even if both p1 and p2 are Point3d's doesn't fulfill goal #3.

Implementations

Goal #1
Common Lisp
Dylan
Python
Ruby
Javaneed some downcasting back from Point to Point2d or Point3d
C++no runtime error
Haskellno runtime error (uses existential types)
OCamlno runtime error
merdno runtime error
niceno runtime error

Goal #2
Common Lisp
Dylan
Pythonexplicit "add" argument runtime type checking
Rubyexplicit "add" argument runtime type checking
Javaexplicit "add" argument runtime type checking
need some downcasting back from Point to Point2d or Point3d
C++explicit "add" argument runtime type checking
Haskellno runtime error
but can't make a list of points having the ability to use "add"
Haskellexplicit "add" argument runtime type checking
uses unpacking from existential type back to real type
OCamlno runtime error
but can't make a list of points having the ability to use "add"
merdno runtime error
niceno runtime error

Goal #3
Common Lisp
Dylan
Pythonexplicit runtime dispatching
Rubyexplicit runtime dispatching
Javano runtime error
explicit runtime dispatching
need some downcasting back from Point to Point2d or Point3d
C++no runtime error
explicit runtime dispatching
Haskellno runtime error
explicit runtime dispatching
uses unpacking from existential type back to real type
Haskellno OO, using a variant for unifying Point2d and Point3d
OCamlno OO, using a variant for unifying Point2d and Point3d
merdno runtime error
niceno runtime error

Remarks


Pixel
This document is licensed under GFDL (GNU Free Documentation License).

Release: $Id$