[net.lang.c] ranges

chris@umcp-cs.UUCP (Chris Torek) (06/02/86)

In article <393@peregrine.UUCP> mike@peregrine.UUCP (Mike Wexler) writes:
>[...] Another feature I would like to have is a range data type.  I don't
>know of a good syntax for putting this in C but it would be nice to
>use in programs where are know the possible values of a number but
>would like the compiler to figure out an efficient sized slot to put
>it in.

Actually, I know of two uses for range types.  You have stated one;
the other is to catch errors where a variable should never take on
values outside certain range(s).  Pascal ranges seem sometimes to
serve one purpose, sometimes another; and the Berkeley Pascal
compiler includes runtime range checking iff one compiles with
`-C'.  Were I designing a language with ranges, I think I would
separate the two uses.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu

rgh%inmet.uucp@BRL.Arpa (06/10/86)

GVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGV
To: info-c@BRL-SMOKE.ARPA
Subject: Re: ranges
In-reply-to: Your message of 2 Jun 86 06:57:19 GMT.             <1774@umcp-cs.UUCP>
From: Scott Menter <escott@ICS.UCI.EDU>
Return-Path: <info-c-request@BRL.ARPA>
Redistributed: Xerox-Info-C^.x@XEROX.ARPA
Received: from BRL-AOS.ARPA by Xerox.COM ; 03 JUN 86 15:25:11 PDT
Received: from brl-smoke.arpa by AOS.BRL.ARPA id a003921; 3 Jun 86 18:12 EDT
Received: from ics.uci.edu by SMOKE.BRL.ARPA id a006967; 3 Jun 86 18:02 EDT
Received: from localhost by ICS.UCI.EDU id a003249; 3 Jun 86 15:01 PDT
GVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGVGV


Chris Torek writes:
> In article <393@peregrine.UUCP> mike@peregrine.UUCP (Mike Wexler) writes:
>>[...] Another feature I would like to have is a range data type.  I don't
>>know of a good syntax for putting this in C but it would be nice to
>>use in programs where are know the possible values of a number but
>>would like the compiler to figure out an efficient sized slot to put
>>it in.
> 
> Actually, I know of two uses for range types.  You have stated one;
> the other is to catch errors where a variable should never take on
> values outside certain range(s).  

Well, actually it might be kind of nice to have a C compiler with an
option to do the kind of array bounds checking Chris implies, whether
or not a range type is implemented.  I suppose this would be hard in
the case of automatic variables and arguments like:

	int foo( arg )
	char arg[ ];

Still, it might be nice for the easy case of, say:

	static char * strings[ CONSTANT_VAL ];

While we're at it, though, what if we *did* use a range type to
subscript an array.  Maybe I'm neglecting something obvious, but
how would the compiler deal with this situation:

	int foo( )
	{
	char someval[ range ];	/* "range" is, say, 1..10 or something */

	    foo2( someval );
	}

	int foo2( stringarg )
	char * stringarg;

etc...

Seems to me that it would be impossible to detect range errors in
"foo2()", especially if it is in a distinctly compiled unit from
"foo()".  In that case, the error checking that range type
implementation provides would be limited to the scope in which it is
defined, and no further.  Of course, if the argument to "foo2()" read
"char stringarg[ range ]" then I suppose it would work, but then you lose
the ability to say things like "stringarg++".  Or, I guess, you could
pass "range" as another parameter to "foo2()".

Well, I'm more of a C hacker than a C implementor, so I imagine that
some of you implementors out there will set me straight, if that's
required.

------------------------------------------------------------------------
E. Scott Menter                            Internet:  escott@ics.uci.edu
UCI Research Support Group                UUCP: ...!ucbvax!ucivax!escott

"...Say, Foz, about my vocals..."
------------------------------------------------------------------------