From: Maciej Sobczak <no.spam@no.spam.com>
Subject: Re: Type safety, C++ and code generation
Date: Fri, 28 Apr 2006 07:57:09 +0200
Date: 2006-04-28T07:57:09+02:00 [thread overview]
Message-ID: <e2sark$65f$1@sunnews.cern.ch> (raw)
In-Reply-To: <445101bf$0$11061$9b4e6d93@newsread4.arcor-online.net>
Georg Bauhaus wrote:
>>type ranged_type<int, 0, 250> Speed;
>>Speed s1, s2, s3; // with some values
>>s1 = s2 + s3; // OK
>>s1 = s2 * s3; // not OK
>>
>>The addition is fine, but the multiplication should not be provided,
>>because speed multiplied by speed is not a speed. Can you extend your
>>class so that the compiler will refuse to compile the second operation
>>above?
>>(Ada, anyone? :) )
>
>
> Just so it is visible:
>
>
> procedure useop is
>
> s1, s2, s3: SPEED; -- with some values
>
> begin
> s3 := s1 * s2;
> end useop;
>
> 8. s3 := s1 * s2;
> |
> >>> cannot call abstract subprogram "*"
>
> where
>
> package Op is
>
> type SPEED is range 0 .. 250;
>
> function "*"(a, b: SPEED) return SPEED is abstract;
>
> end Op;
I like it, although there is some potential problem with this approach.
It uses the "negative logic" - in other words, specifies what is
forbidden, not what is allowed - so it's more prone to errors than the
"positive logic", where you specify what *is* supported instead. In the
"positive logic" approach you can start with the default setting (no
operations) and the compiler will point you to each new operation usage
in your code - then, you can either consciously extend the type
definition to cover the new use case or reject it if it was a bug. It's
much more cumbersome with the "negative logic", where the default
setting provides no protection at all.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
next prev parent reply other threads:[~2006-04-28 5:57 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-04-27 10:35 Type safety, C++ and code generation Maciej Sobczak
2006-04-27 13:19 ` REH
2006-04-27 14:08 ` Maciej Sobczak
2006-04-27 14:33 ` REH
2006-04-27 15:16 ` Maciej Sobczak
2006-04-27 15:39 ` Georg Bauhaus
2006-04-28 5:57 ` Maciej Sobczak [this message]
2006-04-28 12:16 ` Georg Bauhaus
2006-04-28 12:37 ` brian.b.mcguinness
2006-04-28 17:04 ` Georg Bauhaus
2006-04-28 18:50 ` Jeffrey R. Carter
2006-04-28 19:07 ` Randy Brukardt
2006-04-28 19:46 ` Robert A Duff
2006-04-28 19:55 ` Dmitry A. Kazakov
2006-04-29 21:28 ` Florian Weimer
2006-04-27 16:05 ` REH
2006-04-28 6:17 ` Maciej Sobczak
2006-04-30 14:40 ` adaworks
2006-04-30 15:23 ` REH
2006-04-30 18:14 ` Martin Krischik
2006-05-02 6:27 ` Maciej Sobczak
2006-05-02 13:33 ` REH
2006-05-02 15:05 ` Maciej Sobczak
2006-05-02 15:54 ` REH
2006-05-02 16:50 ` Martin Krischik
2006-05-02 19:50 ` REH
2006-04-27 14:47 ` REH
2006-04-27 15:10 ` REH
2006-04-28 6:22 ` Maciej Sobczak
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox