Path: news.daimi.aau.dk!news.uni-c.dk!iesd.auc.dk!news From: pnielsen@iesd.auc.dk (Peter Bak Nielsen) Newsgroups: comp.lang.beta Subject: Re: Templates Date: 17 Oct 1995 15:39:22 GMT Organization: Math & CS at Aalborg University Lines: 87 Message-ID: References: NNTP-Posting-Host: havlit.iesd.auc.dk Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit In-reply-to: eernst@fraxinus.daimi.aau.dk's message of 16 Oct 1995 22:26:33 GMT X-Newsreader: GNUS 4.1 >>>>> "Erik" == Erik Ernst writes: Erik> In article Erik> kolbjorn.aambo@ub.uio.no (Kolbjørn Aambø) writes: Erik> C++'s tool for creating tempates seems like a powerful Erik> tool to generate better libraries. Erik> [snip] Erik> Have the originators of Beta experimented with the idea Erik> of introducing templates to the language? Erik> Not in any way claiming to belong to that group of people .. Erik> My point in this article is: the problems solvable by using Erik> (some sort of) templates _are already solved_ in BETA today. Erik> The basic idea is to parameterize a pattern with one or more Erik> types (corresponding to the type arguments to templates) by Erik> using virtuals: Erik> list: (# element:< object; (* this is a "type parameter" *) Erik> append:< (# elem: ^element; enter elem[] do ... #); Erik> ... Erik> #); Erik> personList: Erik> list(# element::< person #); (* usage, i.e. "instantiation" *) Erik> This is the typical way to implement generic containers in Erik> BETA, and it has a number of advantages: Erik> - 'personList' is compile-time (not link-time) type-safe. Erik> - 'personList' is-a 'list', so you can have polymorphic Erik> access to instances of a "template" - this is not possible Erik> with templates, since a template is _nothing_ until you Erik> instantiate it with some concrete type arguments. You Erik> cannot have arguments to routines of type "some_template", Erik> you cannot have references qualified by "some_template" Erik> etc.etc. The template is simply not a full-fledged citizen, Erik> and must always be instantiated before it is useful. In Erik> this way templates are not much more than textual macros. [...other advanteges removed...] Not really related to the original question, but I was wondering how this polymorphic access is typechecked? That is, I have an instance of personList, and a reference to it with the qualification List. Something like (sorry if this is not correct Beta...): p: @personList; l: ^list; o: ^object; .... p[] -> l[]; Now, if I do something like o -> l.append; then o must reference a person, thus requiring a run-time typecheck. 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. 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? /Peter