[Haskell-cafe] Re: [Haskell] View patterns in GHC: Request?for?feedback

David Roundy droundy at darcs.net
Fri Jul 27 13:51:24 EDT 2007

On Fri, Jul 27, 2007 at 09:02:42AM -0500, Jonathan Cast wrote:
> On Friday 27 July 2007, Jon Fairbairn wrote:
> > ChrisK <haskell at list.mightyreason.com> writes:
> > > Because this is starting to sound like one of the
> > > maddening things about C++.
> > >
> > > Namely, the automatic implicit casting conversions of
> > > classes via their single argument constructors.
> >
> > Unfortunately I'm not sufficiently familiar with C++ to know
> > what this means. Perhaps you could clarify?
> Somebody noticed that, in C, you could mix integers and floats (almost) 
> freely, and in Classic C, you could mix pointers and integers freely, and 
> thought this was /such/ a wonderful idea that C++ has special syntax to 
> declare the conversion functions allowing you to, say, mix pointers and 
> pointer-like classes freely, or to mix char*s and strings freely, etc.  It's 
> what makes

To give a somewhat more mundane example if you define a class Array

class Array {
  Array(int); // ... construct a new array of specified length

Then if you make the mistake of passing an integer constant to a function
that expects an Array, C++ will happily construct a new Array of that size
and pass that to the function.

Even more exciting when you use overloading: if you define multiplication
between two Arrays, then if you accidentally try to multiply an Array by an
integer constant (thinking it'll be a scalar multiply), then a new Array of
that size will be constructed and multiplied--almost certainly resulting in
a runtime error (mismatched Array sizes), but certainly not what you want.

The solution is to add explicit to the constructor for all single-argument
constructors (except perhaps occasionally when you actually want explicit
construction of objects).

The reasoning behind this, of course, is to allow nice interactions of
home-made classes such as complex numbers, or string classes (which you
might want to be automatically constructed from string constants).
David Roundy
Department of Physics
Oregon State University
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070727/12a7dfbf/attachment.bin

More information about the Haskell-Cafe mailing list