[fpc-pascal] Do you get type errors?

spir denis.spir at gmail.com
Sun Jun 20 17:34:07 CEST 2010


On Sun, 20 Jun 2010 16:07:08 +0200
Jürgen Hestermann <juergen.hestermann at gmx.de> wrote:

> e:)
> > * What is type checking _actually_ useful for?
> 
> I would be lost without type checking,
> especially when using sophisticated data structures
> like pointers to arrays of records (which again
> contain pointers to other complex structures).
> An example:
> 
> type  ArrayType = array[1..10] of SomeType;
>       PointType = ^ArrayType;
> var   X : PointType;
>       Y : SomeType;
> 
> What happens if you forget a dereferencing ^ in an expression like this:
> 
> Y := X[7]
> 
> where it should have been
> 
> Y := X^[7]

For sure!
Now, it is easy for the compiler to guess such cases. Some languages perform automagic deferencing of pointers to structured types (record/object, array), meaning eg
	p.x
is interpreted as
	p^.x
and
	p[i]
is interpreted as
	p^[i]
There is no ambiguity since the pointer nature of p is known by the compiler before such lookups can be parsed.
Oberon -- also designed by Wirth and thus not to be considered as weakly secured ;-) -- works that way. Also note that all languages holding implicitely referenced object types can only work that way... including fp for Delphi-like OO (and all dynamic languages like python or Lua).

> What does the compiler do without type checking
> in the first case? Does it access X as if it
> was an array (of what element type/size?).
> 
> It often happens that I change a type into a
> pointer to that type. Then I expect that the
> compiler shows me all places where I now get
> a syntax error so that I can change the code
> instead of having the compiler "guessing"
> some meaning (that I may not have intended).

Nice use of the compiler ;-) But only needed since implicite dereferencing is not a feature of the language.

> I also often change a string type into an integer
> and vice versa. That implies a lot of code changes.
> Again with type checking I can lean back and
> rely on that the compiler will show me all places
> where these changes are needed.


Denis
________________________________

vit esse estrany ☣

spir.wikidot.com



More information about the fpc-pascal mailing list