The Title Should At Least Mention Strings (was Re: Stupid Newbie-sounding question.)
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jun 01, 2004
- 461 views
On Tue, 01 Jun 2004 09:08:16 +1000, Patrick Barnes <mistertrik at hotmail.com> wrote: >My approach to this problem: Patrick, this is an interesting idea, but it is doomed to failure, on the original question, anyway. equal({74,111,104,110},"John") returns true, so this cannot help. Can't resist a couple of comments though: > >We need a new keyword; "of". >Usage: > >type char ( integer c ) >-- return 1 valid character, 0 otherwise >end type > >--myStr is a string >sequence of char myStr > >It is an extension to the type checking system on sequences, and is applied >whenever an element of the sequence is changed. The existing type checks are >slow because they have applied to the whole sequence whenever a single >element is changed. This "of" typing would only check the elements that are >changed in the sequence. > >If you have a complex data structure list, you could have: > >type myDataStructure( sequence s) > --return 1 if valid myDataStructure, 0 otherwise >end type > >--master_list is a 1-dimensional array of myDataStructures >sequence of myDataStructure master_list > >This would not break any existing code, as "sequence" without an 'of' would >mean no restrictions. > >Multi-dimensional arrays are also possible: > >--2d array of integers >sequence of sequence of integer 2d_array > I was once convinced that "sequence of sequence" was the wrong way to go; that we should force the programmer to define an explicit type and follow it with "sequence of <udt>." In my head the jury is still out on that one... >type textpict_line( sequence s) > if length(s) = 80 then return 1 > else return 0 > end if >end type > >--2d array where the length of each line is fixed, and consists of >characters >sequence of textpict_line of char text_picture > One of the (deeply technical) problems I have with this approach is that if text_picture[3] is updated, and textpict_line(text_picture[3]) returns true, should we then call char() 80 times? In the example you post, yes, in general, no. (And it is the no case that should clearly be pursued to enhance performance). I am fairly convinced that "sequence of <udt> of" is plain asking for trouble. Do you see what I mean? >Rules: <big snip, reserving the right to comment later> >3. If using a custom type as the base, eg: textpict_line of char text_line, >the textpict_line type checking will only be invoked when the length of the >sequence changes. (If the of keyword is not used, it will be invoked as >normal) ? because the textpict_line type only relies on length()? Pete