Path: news.daimi.aau.dk!news.daimi.aau.dk!eernst From: eernst@fraxinus.daimi.aau.dk (Erik Ernst) Newsgroups: comp.lang.beta Subject: Re: Templates Date: 16 Oct 1995 22:26:33 GMT Organization: DAIMI, Computer Science Dept. of Aarhus Univ. Lines: 107 Message-ID: References: NNTP-Posting-Host: fraxinus.daimi.aau.dk In-reply-to: kolbjorn.aambo@ub.uio.no's message of 16 Oct 1995 11:18:53 GMT In article kolbjorn.aambo@ub.uio.no (Kolbjørn Aambø) writes: C++'s tool for creating tempates seems like a powerful tool to generate better libraries. [snip] Have the originators of Beta experimented with the idea of introducing templates to the language? Not in any way claiming to belong to that group of people .. My point in this article is: the problems solvable by using (some sort of) templates _are already solved_ in BETA today. The basic idea is to parameterize a pattern with one or more types (corresponding to the type arguments to templates) by using virtuals: list: (# element:< object; (* this is a "type parameter" *) append:< (# elem: ^element; enter elem[] do ... #); ... #); personList: list(# element::< person #); (* usage, i.e. "instantiation" *) This is the typical way to implement generic containers in BETA, and it has a number of advantages: - 'personList' is compile-time (not link-time) type-safe. - 'personList' is-a 'list', so you can have polymorphic access to instances of a "template" - this is not possible with templates, since a template is _nothing_ until you instantiate it with some concrete type arguments. You cannot have arguments to routines of type "some_template", you cannot have references qualified by "some_template" etc.etc. The template is simply not a full-fledged citizen, and must always be instantiated before it is useful. In this way templates are not much more than textual macros. - "instantiation" is not for good: you could very well create a womanList: personList(# element::< woman #); if only (woman## <= person##) holds, whereas an instantiation of a template that produces a normal class is not anymore a template -- we can only instantiate once. - block structure enables very refined "instantiation" strategies, where nested patterns refer to virtuals in enclosing scopes, like editor: (# mouseType:< abstractMouse; mouse: @mouseType; undoList: list(# element::abstractUndo #); buffer: (# outputChannelType:< abstractWindow; output: @outputChannelType; contents: @Text: (* well, _something_ should be simple :*) editOperation: (# undo:< abstractUndo; ... #); onCut: editOperation (# undo::< (# ... enter ... #); ... do mouse.getselection->range[]; range[]->contents.remove->clipboard.set; <> ->(&undo[]).init->undoList.append; output.refresh; #); #); #); I don't claim that this is a nice design ;-) but somehow it demonstrates that type-variables may turn up at different nesting levels in a quite natural fashion. Have the people behind Beta considered to introduce a UNICODE as the characterset in the language. This would be very interesting, indeed! It would be quite enchanting to write code like Øllebrød: vælling (# do ... #); not to mention the possibility of having real, native charater set support for "less-than-or-equal", "not-equal" and similar things. cheers, -- Erik Ernst eernst@daimi.aau.dk Computer Science Department of Aarhus University, Denmark