Path: news.daimi.aau.dk!olevi From: olevi@daimi.aau.dk (Ole Villumsen) Newsgroups: comp.lang.beta Subject: Type checking, safety and templates Date: 24 Oct 1995 12:44:52 GMT Organization: DAIMI, Computer Science Dept. at Aarhus University Lines: 94 Message-ID: <46in44$q42@krone.daimi.aau.dk> References: NNTP-Posting-Host: angers.daimi.aau.dk Peter Bak Nielsen asks: >Not really related to the original question, but I was wondering how >this polymorphic access is typechecked? ... > p: @personList; > l: ^list; > o: ^object; A first comment: It is bad style (but possible) to have a reference to object. A real Beta programmer would use a stronger qualification. > .... > p[] -> l[]; >Now, if I do something like > o -> l.append; >then o must reference a person, thus requiring a run-time typecheck. Correct. >Worse, if I have something like > u: @object; > ... > u -> l.append; >which is clearly a type-error. But this piece of code might be in a >precompiled library, compiled long before we thought about creating >personList. Thus we would still need a run-time typecheck. Right again. The Beta compiler inserts run-time type checks in these two situations. The writer of the library should of course preclude this situation by testing whether l has a correct type, and raise an exception if not. >How is this enforced in Beta? If run-time typechecks are inserted in >general in such situtations, doesn't this mean that most calls to >container classes must be run-time typechecked? Not at all. A simple standard use of a container class would go like: myPersonList: @list (# element::< person #); p: ^person; ... p[] -> myPersonList.insert; ... (if p[] -> myPersonList.has then ... if): ... myPersonList.scan(# where::< (# do current.name = 'Peter Bak Nielsen' #) do current[] -> p[]; #); All assignments in this example can be statically determined to be type-safe, so *no* run-time type checking is needed. (In all assignments, a reference to something that is a specialization of person, e.g. malePerson, may be passed; this causes no problems.) To write code that requires runtime checking, you have to create a somewhat more advanced example. In other words, I don't take *your* examples above to be typical. May this also serve as a reply to Kolbjørn Aambø . He writes: >In the C++ Standard draft the STL part it is claimed that almost all >Template related typechecking is *static*. Codegeneration is dependent >on the types of the parameters given to the templates upond instansiation >done during *Compiletime*. Compared to that BETA's virtual functions and >attrivutes are per definition *DYNAMICALLY* CHECKED! Not so. I claim that the dynamic typechecking in Beta is the exception, the rule being static typechecking, as discussed above. As far as I can see, anything that you can do in C++, you can do in Beta and still have type safety. The dynamic type checking is only introduced to allow you to do assignments that can be meaningful, but yet would be ruled out in C++ completely. >Until now I have only seen C++ compilers with bad error messages related >to wrongly inplemented templates but... That is possibly more caused >by sloppy programming than sloppe language... > >Still I agree that BETA is more understandable and easier to remember... >still not intuitively evident.... > >Have I misunderstood anything? Ole