[fpc-pascal] which "managed" result types require extra work?

David Emerson dle3ab at angelbase.com
Tue Jun 28 01:13:06 CEST 2016


On 06/26/2016 10:51 PM, Jonas Maebe wrote:
 > PS: there is an intrinsic that can help you in this process: the
 > "default" intrinsic. Default(TSomeType) returns an instance of a
 > particular type completely initialised to "empty" (0 for ordinal and
 > floating point types, empty for strings, nil for pointers etc). You
 > can assign this to e.g. your function results on entry, so that even
 > when you add fields (of any type) to records you don't have to add
 > additional initialisation code.

This is very helpful, thank you.

I also tried Initialize, but this does not seem to work effectively in 
all cases; Default seems to work with everything I've thrown at it.

> If a record type does not contain an ansistring or a dynamic array, you
> also have to initialise results of functions returning such a type to
> get them to return a "empty" value. If you add a field that is not a
> managed type, you will also have to go through your code to add
> initialisation code for that field everywhere.
>
> So I'm not sure how this is different from any other type.

The source of my confusion -- and I suspect that of many others -- is 
that I always assumed a function result was treated like a local 
variable within the function, rather than a parameter to the function.

Since managed types are automatically initialized when they are local 
variables, I assumed a function result was the same.

Unfortunately, the documentation does not make this sufficiently clear 
in describing managed types. I guess I should file a documentation bug.

Thanks,
David





More information about the fpc-pascal mailing list