Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-08-22 00:43:42


Douglas Gregor wrote:

> print_string() and print_int() are function objects that can be invoked given
> an instance of std::string or int, respectively.

        Ah, I see!

> The parsing of the construct:
>
> variant_switch(v)
> .on<std::string>(print_string())
> .on<int>(print_int())
> .on_default(print_unknown());
>
> variant_switch(v) returns an object of type variant_switch_stmt that contains
> a reference to "v".
[]

        Hey, that's clever!

> > If you're using method calls, then, yes, the result
> > would be typesafe (although I don't quite understand
> > how this would work using existing C++ syntax).
> >
> > So:
> > 1. It lacks compile time completion assurance.
>
> This is not in my vocabulary :(.

        I made it up on the spot as having an obvious meaning :-)

> Does this refer to an inability to detect at
> compile time whether all possible tags that could be used with an instance of
> a variant have been accounted for in a case?

        yes.

e.g., given:
>
> type t = X of int | Y of float | Z of float
> match e with
> | X x -> print_int x
> | Y y -> print_float y
>
> we should have a compile-time failure?

        Yes. Ocaml actually just prints a warning.
Sometime annoying, since a 'remake' won't repeat the
warning (have to 'make clean' occasionally :-)
 
> > 2. It would break the open/closed principle:

> It appears that way :(. I don't see how the open/closed principle comes into
> play here. I hope that the above descriptions have made my intentions more
> clear.

        Yes. You're right. I did misunderstand, the technique
you describe seems OK.

> > However, it isn't clear if it is worth 'fixing' C++.
>
> I'm not clear that it is even a fix, per se. I think that it is entirely
> possible to emulate variants in C++ using templates. If so, I don't believe
> there is any use in adding a redundant feature to the language.

        If you can eliminate all possibility of failure
at run time. I think this is possible too, with some
extra template trickery. [Of course, the inheritance
trick introduces a memory management problem which in
principle is independent of the semantic one: the same
problem exists in ML and is 'solved' by GC, so to be fair,
one should ignore that since one expects to manage memory
in C++ manually]

-- 
John (Max) Skaller, mailto:skaller_at_[hidden] 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
http://Interscript.sourceforge.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk