Related articles |
---|
Pointers to "why C behaves like that ?" skwong@sun80.acae.cuhk.edu.hk (WONG SAI-KEE) (2002-11-12) |
Re: Pointers to "why C behaves like that ?" Gayev.D.G.=?koi8-r?Q?=3Cdg=C1ev=40mail=2Eru=3E?=@m (2002-11-13) |
Re: Pointers to "why C behaves like that ?" mwotton@cse.unsw.edu.au (Mark Alexander Wolton) (2002-11-15) |
Re: Pointers to "why C behaves like that ?" skwong@sun80.acae.cuhk.edu.hk (WONG SAI-KEE) (2002-11-15) |
Re: Pointers to "why C behaves like that ?" jacob@jacob.remcomp.fr (jacob navia) (2002-11-15) |
Re: Pointers to "why C behaves like that ?" christian.bau@freeserve.co.uk (Christian Bau) (2002-11-17) |
Re: Pointers to "why C behaves like that ?" Gayev.D.G.=?iso-8859-1?Q?=3Cdg=E0ev=40mail=2Eru=3E (2002-11-17) |
Re: Pointers to "why C behaves like that ?" bobduff@shell01.TheWorld.com (Robert A Duff) (2002-11-17) |
Re: Pointers to "why C behaves like that ?" jamesp_spam_me_not@silver-future.com (James Powell) (2002-11-17) |
Re: Pointers to "why C behaves like that ?" thp@cs.ucr.edu (2002-11-17) |
Re: Pointers to "why C behaves like that ?" mwotton@cse.unsw.edu.au (Mark Alexander Wolton) (2002-11-20) |
Re: Pointers to "why C behaves like that ?" thp@cs.ucr.edu (2002-11-20) |
Re: Pointers to "why C behaves like that ?" marcov@toad.stack.nl (Marco van de Voort) (2002-11-20) |
[60 later articles] |
From: | Gayev.D.G.=?iso-8859-1?Q?=3Cdg=E0ev=40mail=2Eru=3E?=@mail.rsl.ru |
Newsgroups: | comp.compilers |
Date: | 17 Nov 2002 22:56:15 -0500 |
Organization: | Russian state library |
References: | 02-11-059 02-11-071 02-11-083 |
Keywords: | C, design |
Posted-Date: | 17 Nov 2002 22:56:15 EST |
WONG SAI-KEE wrote:
> Gayev.D.G.=?koi8-r?Q?=3Cdg=C1ev=40mail=2Eru=3E?=@mail.rsl.ru wrote:
> : WONG SAI-KEE wrote:
>
> : Most modern languages require variables (as well as other objects) to
>
> But, languages like HyperCard, VisualBasic (I was told) do not.
>
> : compiler must know types of all program objects during
> : compilation. Without explicit declarations, the type assigned to
> : variable can only be determined by its usage - unreliable and
> : sometimes rather hard to do.
>
> I am not against the necessary of declaration. In some programming
> books, they don't explain why we need it (or they tell a reason which
> does not explain). So I'm thinking about it may be due to the
> compiler construction and historical reason.
>
> The question is: ``rather hard to do'' is not a good reason in modern
> computing (in the old days, OK, due to lack of resources). Because
> the compiler is written once, and all the end user benefits from it.
> That should always be one of the principle of writing a computer
> program: we want the computer to do the tedious labor intensive work
> (of course, we need to justify the cost and benefit in some cases, but
> I think the user pool is huge for a language like C to be justifiable
> to provide convenience to the user).
>
> I mean we should not sacrifice the easy of using the language due
> to the compiler construction. I can understand when the author
> first evolve the C lang, there are much uncertainty, so DECLARATION
> is a good choice to start with. Then, this becomes the historical
> reason. Other reason like unreliable is a good support, but I then
> want to collect solid examples to show it in the book.
>
> : "Technical" reasons are not less important, that the "ideological" ones.
> Can you clarify this more ? Thanks.
Well, my original statement probably was too strong. Definitely not
all of the modern languages require declarations. Basically and
roughly, all languages may be subdivided into two categories: the
"traditional" ones (like C/C++, Java, descendants of Pascal) and
"typeless" (for the lack of better term) languages, like Vis Basic,
PERL, Python, TCL... most scripting languages of any kind. The
traditional languages are mostly fully-compiled ones; the typeless
ones are mostly interpreted or semi-compiled (e.g. producing some type
of intermediate code to be executed later).
The problem of declarations is someway related to this distinction.
Traditional languages require variables to be declared, because memory
allocation and mapping is performed at compile time, and, in order to
expressions and statements to be compiled correctly, the exact types
assigned to them must be known. In typeless languages both allocation
and ultimate "typisation" of the language objects are done dynamically
at runtime, so variable predeclarations are not much needed. Of cause,
the flexibility of "typeless" languages has some drawbacks: they are
commonly less efficient, than the fully-compiled ones.
Of cause, the historical reasons also must be taken into
account. Since the first "traditional" language (it was Algol-58,
IMHO), the language designers decided what the forward declarations of
variables is a good thing to have. IMHO, they are right :-) Technical
matters for this decision are briefly discussed above, but there are
matters of language design ideology as well. Explicit declarations of
variables provide some additional program discipline and logic, which
is worthy. The compiler easily can catch various errors - simple (as
mistyped variable names) or more complex ones (as usage of variable
inconsistent with its type). And yes, in most languages there is not
always possible to deduce type of variable (not declared previously)
by context.
There are many cases of possible ambiguity - if the variable is first
mentioned in the argument list for the overloaded function, accepting
parameters of various types, for example. And for the variables of
derived types the situation will be even worse - so the explicit
declaration is the best way to avoid such problems. Of cause, I may
be not 100% right (and even completely wrong :), so additions and
comments welcome.
[Fortran and, debatably, Cobol preceded Algol-58. In Fortran, declarations
are optional, in Cobol they're required. Fortran 77 added IMPLICIT NONE to
let you turn off the automatic declarations. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.