🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.modula2
7 messages
7 total messages Started by cz Fri, 08 Mar 1996 04:45
Variabel Parameters...
#3346
Author: cz
Date: Fri, 08 Mar 1996 04:45
6 lines
599 bytes
 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...

Thanks.
Re: Variabel Parameters...
#3350
Author: Jeffrey E Stoner
Date: Fri, 08 Mar 1996 18:04
15 lines
1052 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...

I seem to recall a discussion about this very topic some time ago.
Can anyone get their hands on some Usenet archives?

Jeff

-- 
   *  internet-->  jesst63+@pitt.edu             * This space reserved  *
   *  http://www.pitt.edu/~jesst63               * for something much   *
   * "I am outcast, unclean." - Thomas Covenant  * better to come along *
Re: Variabel Parameters...
#3351
Author: "Mark Morgan Lloyd "
Date: Fri, 08 Mar 1996 20:00
8 lines
571 bytes
Not within the language, although some implementations (e.g. TopSpeed) 
can be tweaked.

Mark Morgan Lloyd
mark_tbu@cix.compulink.co.uk

[Opinions above are the author's, not those of his employers or 
colleagues]
Re: Variabel Parameters...
#3359
Author: Dmitry V. Leskov
Date: Sat, 09 Mar 1996 08:47
40 lines
1788 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...

It's not possible in the 'standard' language. Some implementations provide
extensions to the languages (XDS) or can be deceived using pragmae and code
procedures (TopSpeed).

Here's an XDS example:

<*+ m2extensions *>                -- turn language extensions ON
DEFINITION MODULE FormStr;

IMPORT  SYSTEM;

PROCEDURE print (VAR str: ARRAY OF CHAR;   -- This procedure works like ANSI C 
                     fmt: ARRAY OF CHAR;   -- sprintf function
                 SEQ arg: SYSTEM.BYTE);


In TopSpeed, you should write:

(*# save, call(var_arg => on, c_conv => on) *)
(*# call(reg_param => (), o_a_copy => off)  *)

PROCEDURE print(VAR str: ARRAY OF CHAR;
                    fmt: ARRAY OF CHAR);

(*# restore *)

and do some tricks in the implementation.

======================================================================
Dmitry V. Leskov                      Institute of Informatics Systems
Programmer                                         Novosibirsk, Russia

snowman@iis.nsk.su
Re: Variabel Parameters...
#3361
Author: 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
Re: Variabel Parameters...
#3360
Author: Graham Perkins
Date: Mon, 11 Mar 1996 12:15
28 lines
1784 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)

Nope.  That would be completely counter to the strong typing flavour of 
the whole language.  You may be able to get the effect you want, without
compromising safety, using a single parameter and the array, record, or
set constructors.  But you'd need an ISO compliant compiler.  Otherwise,
use an open array.

> ... i.e for use with
> porting the printf() function in C over to modula-2...

One of the great advantages of printf() in C is that ordinary compilation
will not detect mismatch in number of format descriptors in format string
and number of subsequent parameters.  Another great advantage is that
the types of the subsequent strings do not have to match the types in 
the descriptors.

These features of variable, untyped parameter lists give the programmer
huge flexibility in creating a variety of runtime errors.
-- 
person: Graham Perkins         paper: School of Computing
voice:  +44 (0)1908 834936            De Montfort University
dots:   +44 (0)1908 834948            Milton Keynes MK7 6HP
bits:   grp@dmu.ac.uk                 United Kingdom
Re: Variabel Parameters...
#3363
Author: Peter Moylan
Date: Mon, 11 Mar 1996 22:50
14 lines
1055 bytes
Jeffrey E Stoner (jesst63+@pitt.edu) wrote:

>I seem to recall a discussion about this very topic some time ago.
>Can anyone get their hands on some Usenet archives?

Old news articles are available at http://www.dejanews.com,
although I'm not sure how best you could use their search
engine to track down this particular topic.

--
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads