Re: Homogeneous sequence

new topic     » goto parent     » topic index » view thread      » older message » newer message

Juergen Luethje wrote:
> Robert Craig wrote:
> <snip>
> 
> > If you wanted tighter checking for strings you could write:
> > 
> > type char(integer x)
> >     return x >= 0 and x <= 255 
> > end type
> > 
> > type string(sequence of char s)
> >     return TRUE
> > end type
> 
> </snip>
> 
> Currently, there is a problem with code like that. When we e.g. write
> }}}
<eucode>
> ....
> if char(foo) then
> ...
> </eucode>
{{{

> then the program will crash instead of returning FALSE, in case 'foo' is
> not an integer. In order to avoid that, currently we have to define the
> type somehow like this:
> }}}
<eucode>
> type char(object x)
>    return integer(x) and x >= 0 and x <= 255 
> end type
> </eucode>
{{{

> What would that problem mean regarding the proposed extension of the type
> system? Or maybe this behaviour can be changed, when the type system is
> revised? I'd appreciate that.

As you suggest, perhaps when an argument with the incorrect type
is passed to a type routine, the program should not
die on the spot, but rather the type routine should simply return 0,
indicating that the value does not belong to that type.

This will allow the type routine to be used as a function,
just to check yes or no, (1 or 0) whether a value belongs
to that type. If the type routine is being called automatically
by the interpreter at an assignment statement, or when arguments are
passed to a normal *function*, then you will still get a 
type_check failure message (assuming "with type_check" is on),
but the error traceback will start at the assignment statement,
or function declaration line, not at the declaration line of the 
type routine, as happens now. I think this would
be a somewhat better diagnostic message than what we get now. 

In Euphoria, a type routine is declared differently from a 
normal function, so I guess we could be excused in
having slightly different rules in one versus the other.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu