Hello,
How is the production type-inquiry used?
var p : integer;
type q = type of p;
How would it be possible that inquiring as to the type of p is useful? Under what circumstances would the type of p not be already known?
Even if the type of p were not known, how would q be usable if its type were not fixed?
The only reference I can find is in the GNU-Pascal Manual.
function CompilerAssert( condition : boolean, result : Anytype ) : type of Anytype;
What would be returned? Unless Anytype is restricted to types that CompilerAssert is aware of.
Regards,
Paul Isaacs
On 21/01/17 20:19, Paul Isaacs wrote:
Hello,
How is the production type-inquiry used?
var p : integer;
type q = type of p;
How would it be possible that inquiring as to the type of p is useful? Under what circumstances would the type of p not be already known?
Even if the type of p were not known, how would q be usable if its type were not fixed?
The only reference I can find is in the GNU-Pascal Manual.
function CompilerAssert( condition : boolean, result : Anytype ) : type of Anytype;
What would be returned? Unless Anytype is restricted to types that CompilerAssert is aware of.
Regards,
Paul Isaacs
Gpc mailing list Gpc@gnu.de https://www.g-n-u.de/mailman/listinfo/gpc
Hi Paul,
Perhaps it is some sort of equivalent to overloaded functions? You might find the ISO spec handy, though it is not an easy read! www.pascal-central.com/docs/iso10206.pdf
6.4.9 Type-inquiry
A type-inquiry shall denote a type, bindability, and initial state.
type-inquiry = 'type' 'of' type-inquiry-ob|ect . type-inquiry-ob|ect = variable-name | parameter-identifier .
The type denoted by a type-inquiry shall be the type possessed by the variable-identifier or parameter- identifier contained by the type-inquiry. The bindability denoted by a type-inquiry shall be the bindability possessed by the variable-identifier or parameter-identifier contained by the type-inquiry.
The initial state denoted by a type-inquiry shall be the initial state possessed by the variable-identifier or parameter-identifier contained by the type-inquiry. A parameter-identifier in a type-inquiry-object shall have its defining-point in a value-parameter-specification or variable-parameter-specification in the formal-parameter-list closest-containing the type-inquiry-object.
Example: procedure p(var a : VVector); var b : type of a; {parameter a and variable b will have the same type}
If you can figure out what that means, please let me know!
Regards, Peter
Paul Isaacs wrote:
How is the production type-inquiry used?
var p : integer;
type q = type of p;
How would it be possible that inquiring as to the type of p is useful? Under what circumstances would the type of p not be already known?
Even if the type of p were not known, how would q be usable if its type were not fixed?
It seems that if you want you can replace type-inquiry by explicit definition. However, type-inquiry may be preferable:
- for several purposes it does not matter which type you use, it matters that types are the same. The simplest case is swaping variables: you need temporary of the same type. - type-inquiry may be useful if you generate program via textual maniputation. Then "the same" program fragment may be better reusable, in a sense adapting to types from outside.
In other words, it looks like feature supporting specific style, if you do not like this style do not use it.
Paul Isaacs wrote:
On 22/01/17 11:09 AM, Waldek Hebisch wrote:
It seems that if you want you can replace type-inquiry by explicit definition.
Hello Waldek,
Thanks for the reply. I just assumed, incorrectly, that type-inquiry was a reflective construct of some sort.
It _is_ a reflective construct. Just most of time you can live without it. Maybe an example:
var a : record i : integer end;
Note that if you write:
var b : record i : integer end;
then b will be of different type than a (because each 'record' introduces entirely new type). You can solve the problem using named type:
type t = record i : integer end; var a : t;
var b : t;
Or you can use type-inquiry:
var b : typeof(a);
If you can modify declaration of 'a' you probably prefer first version. But in real life you may be unable to modify declaration of 'a'...
Note: in GPC 'typeof' is only implemented for object types (which are absent in Extended Pascal) so this does not work.