1. RE: The Title Should At Least Mention Strings (was Re: Stupid Newbie-sounding
- Posted by "Patrick Barnes" <mistertrik at hotmail.com> Jun 01, 2004
- 481 views
>From: Pete Lomax <petelomax at blueyonder.co.uk> >Subject: The Title Should At Least Mention Strings (was Re: Stupid >Newbie-sounding questi > >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. My view on this is: IT DOESN'T MATTER! The only place that you would see the underlying integers is in the trace window. The interpreter may not know whether a variable is a string or a list of integers, but you sure should! A variable called "name" is going to contain characters, and a variable called "exam_scores" is going to contain numbers. Unless you are tracing, you should not overly care what the internal structure of the string is.* *Well, maybe if you were trying to cut down on memory usage. This is a different issue. >Can't resist a couple of comments though: Bring 'em on! <SNIP (my original post)> > > > >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... Um, there's nothing to respond to here, I don't know what convinced you. However, My number 1 programming mistake is accidentally forgetting to place braces ( {} ) around a variable when I '&' it to an array. Forcing sequences would help that, (but so would the explicit type.... okay, I agree) > >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). No, it shouldn't be.... Because of "differential type checking" (I just made the term up here) Once a variable has been set, it does not change unless we change it somewhere... So, what are we assigning to text_picture[3] ? If it's being assigned to something that has the type textpict_line already, then we don't need to type check the element, because it has already been type checked when it was last changed. If we assign a "generic sequence" to text_picture[3], then all bets are off. We don't know in advance what the sequence contains, so yes we need to call char() 80 times, as well as checking that the sequence length is 80. Keep in mind that we would need to do this anyway if we wanted to ensure the variable's integrity. At least with differential type checking the full check would only need to be done if "casting" from a generic type. Of course, "casting" one of these "sequence of ..." back to a generic sequence is trivial. >I am fairly convinced that "sequence of <udt> of" is plain asking for >trouble. Do you see what I mean? Um... no. Did I address it above? > >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()? Yes! custom types that have "of" after them should only check aggregate properties of the variable, not the individual elements. The only aggregate property I can think of atm is length(). I really think this would make Euphoria more flexible... Even though I'm talking about being able to add restrictions. If the type system is more powerful, then it reduces the burden on the programmer to check every aspect of a variable passed around. MrTrick