[Haskell-cafe] no dynamic binding

Tomasz Zielonka t.zielonka at students.mimuw.edu.pl
Mon Sep 20 05:57:19 EDT 2004


On Sun, Sep 19, 2004 at 02:46:12PM -0400, Abraham Egnor wrote:
> You can use exisential types to do what you'd like.  From memory, so
> there are probably errors:
> 
> newtype ServerCommand = forall a. ServerCommandClass a => ServerCommand a

This can't be newtype, you must use 'data'.

> instance ServerCommandClass ServerCommand where
>         toString (ServerCommand c) = toString c
> 
> commands :: [ServerCommand]
> commands = [ServerCommand $ DashCommand ..., ServerCommand $ MoveCommand ...]

As it was pointed to me some time ago, you can also achieve this without
existential types, simply by keeping the partially applied methods in
your ServerCommand data type, eg.

    data ServerCommand =
	ServerCommand
	{ scToString	:: String
	}

or, if you want to avoid memoizing the toString result in this case

    data ServerCommand =
	ServerCommand
	{ scToString	:: () -> String
	}

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links


More information about the Haskell-Cafe mailing list