🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #3361

Re: Variabel Parameters...

#3361
From: Peter Moylan
Date: Sun, 10 Mar 1996 23:23
86 lines
4817 bytes
cz@ecn.net.au wrote:
> I am wondering if it is possible, in m2, to have a procedure that can
>be called with an un-defined number of parameters, with un-defined
>types (defined ofcourse when the proc is called)... i.e for use with
>porting the printf() function in C over to modula-2...

The short answer is that it isn't possible, and that such a
feature couldn't be introduced without seriously violating
the design goals of Modula-2.  The Modula-2 rules include
a principle that the compiler should be able to compare a
procedure call with the procedure definition, in order to
make sure that the types match.  In contrast, the C printf
function explicitly requires that there be no type checking.

As it happens, the Modula-2 restriction applies only to
the number of the parameters.  It is certainly possible to
write a procedure with parameters of unspecified type, by
using the type-breaking facilities of SYSTEM.  To specify
a parameter of an unspecified type, use one of the following:
 - ARRAY OF WORD (for compilers that conform to PIM3);
 - ARRAY OF BYTE (most present-day compilers);
 - ARRAY OF LOC (the method specified in the standard - unfortunately
   most compilers now in use don't yet support this).
Note also that a formal parameter of type ADDRESS is
compatible with any pointer type.

Of course, once you start doing this sort of thing all bets are
off concerning the safety of your code.  Once you start breaking
the type compatibility rules you greatly increase the risk of
writing buggy code, and having to spend long hours with a
debugger.

As always, there are ways to fake a substitute for what you
want.  For example, you could write a procedure that takes a
single parameter, and make that parameter a linked list.
But I wouldn't do this myself, it's more trouble than it's
worth and leads to code that's hard to read and write.

As an aside, I note that this is a frequently asked question,
and I presume that it's usually asked by people who are
coming to M2 after first having learnt C.  (Which, in my
opinion, is doing things the wrong way around - but that's
a whole nother story.)  The implied suggestion is that the
C printf is a nice feature to have.  Now, I've done a
reasonable amount of C programming, and in my experience
printf is a major trouble-maker in terms of error rates,
time wasted in debugging, etc.  (The problem is precisely that
there's no type checking, so minor typos or misconceptions can
cause major obscure errors.)  So much so, in fact, that I
no longer use printf in my production-quality C code, although
I sometimes use it for quick-and-dirty jobs.  In my opinion
the C language could be greatly improved by removing printf
and replacing it by type-checked functions similar to what
you get with Modula-2.

A further aside: back in the days when Fortran was one of the
dominant programming languages, a frequent criticism of
Fortran was its complicated FORMAT statement.  It was seen as
an ugly feature of the language, and a frequent cause of
programming errors.  It's interesting to see that the C
printf is basically just a copy of the Fortran FORMAT statement
with a changed syntax.

Interestingly enough, Fortran's FORMAT _could_ in theory have
allowed compile-time type checking, although in practice this
was never done.  The C standard confirms this practice, by
saying in effect that type checking _must not_ be done.
Historically this makes sense, because C was designed when
Fortran was still a relatively young language, but in hindsight
C has been burdened with a very nasty mistake from the past.

Pascal manages to get the most of both worlds, by having
a Write statement that gives compile-time checking _and_
a variable number of parameters.  This works because the
Pascal Write is a statement - i.e. it's built into the
language - rather than a library procedure.  This feature
was dropped from Modula-2 because the Pascal Write works
only for the "standard input" device, and is not capable of
being extended (without a language re-design) to I/O with
arbitrary devices.

--
Peter Moylan                           peter@ee.newcastle.edu.au
                  http://www.eng.newcastle.edu.au/ee/Moylan.html
OS/2 freeware list at 
      http://www.eng.newcastle.edu.au/ee/Moylan/os2/os2info.html
Technical Details
Message-ID: <4hvo9l$7jb@seagoon.newcastle.edu.au>
Path: arc1-ca.pugleaf.net!fourier.newcastle.edu.au!peter
References: <4hodo7$flm@sydney1.world.net>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fourier.newcastle.edu.au!peter
From: peter@fourier.newcastle.edu.au (Peter Moylan)
Newsgroups: comp.lang.modula2
Subject: Re: Variabel Parameters...
Date: 10 Mar 1996 23:23:33 GMT
Organization: The University of Newcastle
Message-ID: <4hvo9l$7jb@seagoon.newcastle.edu.au>
References: <4hodo7$flm@sydney1.world.net>
Reply-To: peter@tesla.newcastle.edu.au
NNTP-Posting-Host: fourier.newcastle.edu.au
X-Newsreader: TIN [version 1.2 PL2]