Velocity Reviews > Putting logical limitations on types

# Putting logical limitations on types

Ido Yehieli
Guest
Posts: n/a

 01-25-2008
Hi,
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?

For example: a bound_int which can only be in a certain range, so
don't i don't have to have a bound checking function like this one:

int in_range(int i, int min, int max){
----if(i<min)
--------return min;
----if(i>max)
--------return max;
}

Or a prime_number type, and so on.

Thanks,
Ido.

Eric Sosman
Guest
Posts: n/a

 01-25-2008
Ido Yehieli wrote:
> Hi,
> can I define types with a limitation on the data the type contain,
> rather than just the size it will have in memory?
>
> For example: a bound_int which can only be in a certain range, so
> don't i don't have to have a bound checking function like this one:
>
> int in_range(int i, int min, int max){
> ----if(i<min)
> --------return min;
> ----if(i>max)
> --------return max;
> }
>
> Or a prime_number type, and so on.

A variable of type T can store any value in T's range,
without restriction. There is no way you can limit the
values to a subrange or subset:

int hour; /* 0 through 23, please */
hour = 42; /* nothing prevents this */

int odd; /* odd numbers only, please */
odd = 42; /* nothing prevents this */

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

Ben Pfaff
Guest
Posts: n/a

 01-25-2008
Ido Yehieli <(E-Mail Removed)> writes:

> can I define types with a limitation on the data the type contain,
> rather than just the size it will have in memory?

Not in C.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

Walter Roberson
Guest
Posts: n/a

 01-25-2008
In article <(E-Mail Removed)>,
Ido Yehieli <(E-Mail Removed)> wrote:

> can I define types with a limitation on the data the type contain,
>rather than just the size it will have in memory?

>For example: a bound_int which can only be in a certain range, so
>don't i don't have to have a bound checking function

No, not in C.

Note: C enumerations ("enum") are suggestive of what you are looking
for, but they do NOT prevent other values from being stored.
Effectively they just give names for integral constants, rather
than controlling the data type.
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)

Ido Yehieli
Guest
Posts: n/a

 01-25-2008
On Jan 25, 11:12 pm, Eric Sosman <(E-Mail Removed)> wrote:
> int hour; /* 0 through 23, please */
> hour = 42; /* nothing prevents this */

That's unfortunate. Thanks anyway.

Keith Thompson
Guest
Posts: n/a

 01-25-2008
Ido Yehieli <(E-Mail Removed)> writes:
> can I define types with a limitation on the data the type contain,
> rather than just the size it will have in memory?
>
> For example: a bound_int which can only be in a certain range, so
> don't i don't have to have a bound checking function like this one:
>
> int in_range(int i, int min, int max){
> ----if(i<min)
> --------return min;
> ----if(i>max)
> --------return max;
> }
>
> Or a prime_number type, and so on.

No, C doesn't provide a standard way to do that (and I've never heard
of an implementation that provides it as an extension).

You can define an abstract data type, implemented as a struct, or a
void*, or whatever, and enforce full control of what can be done with
the type. But this is tedious. If you want addition, for example,
you have to provide your own add() function (C doesn't support
user-defined operator overloading).

If you really want this, you might consider using a language other
than C (for example, a C++ class can do much of this kind of thing
more conveniently than you can do it in C). If you choose a C
implementation that provides extensions such as operator overloading,
be aware that you're also in effect using a language other than C;
your code won't be portable to other C implementations. That may or
may not be a problem for you.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Eric Sosman
Guest
Posts: n/a

 01-25-2008
Ido Yehieli wrote:
> On Jan 25, 11:12 pm, Eric Sosman <(E-Mail Removed)> wrote:
>> int hour; /* 0 through 23, please */
>> hour = 42; /* nothing prevents this */

>
> That's unfortunate. Thanks anyway.

Perhaps. But if you think about it for a while, you may
realize that the notion of a range-restricted sub-type would
complicate the language enormously. For example, what ought
to happen in the second line of the example above (assuming
the first line somehow expressed the 0..23 range limitation)?
Should the program silently store 23, or should it store 18
(42 mod 24), or should it crash, or ...? Should it be legal
to use an ordinary int* to point at a (0..23)int variable?
(If yes, how do you get scanf() to respect the range limit?)
Should it be legal to assign a (0..23)int* to a (-42..42)int*?
What rules should govern the division of a (-42..42)int by a
(1..16)int: For example, what is the type of the quotient?

C as it stands allows you to write any predicates you like
and to choose the behavior if a predicate isn't satisfied.
However, it requires that you write these things as functions;
C will not insert them for you at every reference to a variable.

--
Eric Sosman
(E-Mail Removed)lid

Flash Gordon
Guest
Posts: n/a

 01-25-2008
Ido Yehieli wrote, On 25/01/08 21:56:
> Hi,
> can I define types with a limitation on the data the type contain,
> rather than just the size it will have in memory?
>
> For example: a bound_int which can only be in a certain range, so
> don't i don't have to have a bound checking function like this one:
>
> int in_range(int i, int min, int max){

<snip>

Not in C you can't. If you want to do that sort of thing you will have
to move to another language and consider what you want to happen if an
attempt is made to assign an illegal value. Pascal certainly has some of
the features you want, as does Modula 2 I believe. You might be able to
implement something of the sort in C++ using operator overloading, but
that would be a question for comp.lang.c++ to answer.

If you want to discus the best alternatively language for your
requirements comp.programming *might* be an appropriate place.
Alternatively, if you want to explain more about what you want to
achieve it might be that someone can suggest a completely different
approach using C to solve your problem.
--
Flash Gordon

Ido Yehieli
Guest
Posts: n/a

 01-25-2008
On Jan 25, 11:55 pm, Flash Gordon <(E-Mail Removed)> wrote:
> If you want to discus the best alternatively language for your
> requirements comp.programming *might* be an appropriate place.
> Alternatively, if you want to explain more about what you want to
> achieve it might be that someone can suggest a completely different
> approach using C to solve your problem.

Never mind, I'll just use a function to check the bounds when needed.
I thought there might be a more appropriate way of doing it.

-Ido,

Ben Bacarisse
Guest
Posts: n/a

 01-26-2008
Ido Yehieli <(E-Mail Removed)> writes:

> can I define types with a limitation on the data the type contain,
> rather than just the size it will have in memory?
>
> For example: a bound_int which can only be in a certain range

<snip>
> Or a prime_number type, and so on.

You've had the C answer: no, but it is worth pointing out that a
language that the full version of what you are hinting at with a "prime
number" type would either have to abandon compile time types or the
compiler would need a complex theorem prover in it (and the compiler
might end up not guaranteed to terminate). That is far cry from C.

--
Ben.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Tim Streater HTML 4 06-15-2012 06:13 PM Soren Kuula XML 2 12-01-2005 07:51 PM Sathyaish ASP .Net 2 05-22-2005 07:32 PM Saurabh Java 6 05-30-2004 02:44 PM Jeremy Cowles C++ 5 08-19-2003 05:33 PM

Advertisments