Recent

Author Topic: TurboPascal Fail,TypeOf  (Read 5451 times)

jorgicor

  • New Member
  • *
  • Posts: 11
TurboPascal Fail,TypeOf
« on: June 20, 2015, 09:33:38 am »
Hello,

Reading an old book about Turbo Pascal 6.0 I found some functions to use with objects ('object', not the delphi 'class') that I don't find in Free Pascal:

1. Fail: Only can be used in the constructor. When used there, it seems that the constructor will act as a function returning true or false (false if Fail was called inside the constructor). It is useful as exceptions didn't exist in Turbo Pascal. Available in GNU Pascal, but not documented: http://www.gnu-pascal.de/gpc/Fail.html#Fail

2. TypeOf: Returns a pointer to the virtual method table of an object. Useful to check the type of an object. It is available in GNU Pascal as well. http://www.gnu-pascal.de/gpc/TypeOf.html

I don't find any reference to them in the documentation or in RTL. Are they supported by Free Pascal?

Thank you vey much.

[Additional]

I have found more info about FAIL in an old Turbo Pascal 7 manual:

Quote
Once control arrives at the begin of a constructor's statement part, the object-type instance is guaranteed to have been allocated and initialized successfully. The constructor itself, however, might attempt to allocate dynamic variables to initialize pointer fields in the instance and, in turn, these allocations might fail. If that happens, a well-behaved constructor should reverse any successful allocations and deallocate the object-type instance so that the net result becomes a nil pointer. To make such backing out possible, Turbo Pascal implements the Fail standard procedure that takes no parameters and can be called only from within a constructor. A call to Fail causes a constructor to deallo-cate the dynamic instance that was allocated upon entry to the constructor and causes the return of a nil pointer to indicate its failure. When dynamic instances are allocated through the extended syntax of New, a resulting value of nil in the specified pointer variable indicates that the operation failed. Unfortunately, there is no such pointer variable to inspect after the construction of a static instance or when an inherited constructor is called. Instead, Turbo Pascal allows a constructor to be used as a Boolean func-tion in an expression: A return value of True indicates success, and a return value of False indicates failure due to a call to Fail within the constructor.
« Last Edit: June 20, 2015, 09:54:55 am by jorgicor »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TurboPascal Fail,TypeOf
« Reply #1 on: June 20, 2015, 09:52:54 am »
I don't find any reference to them in the documentation or in RTL. Are they supported by Free Pascal?
No. Do they really exist in TP? Or only GPC implements it?

jorgicor

  • New Member
  • *
  • Posts: 11
Re: TurboPascal Fail,TypeOf
« Reply #2 on: June 20, 2015, 09:58:01 am »
It seems that they exist. Sorry I modified my last message with more info I found in a turbo pascal 7 manual, before seeing your response.

In this manual, page 107 (118 of the pdf):

http://www.textfiles.com/bitsavers/pdf/borland/Turbo_Pascal_Version_7.0_Language_Guide_1992.pdf

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: TurboPascal Fail,TypeOf
« Reply #3 on: June 20, 2015, 10:49:07 am »
TypeOf is replaced by the IS and AS keywords, which are a lot more intuitive and informative.
Fail is replaced by more comprehensive structured exception handling. Although halt(<errorcode>) could and can still be used as well.
Freepascal offers any of them as well.
« Last Edit: June 20, 2015, 10:52:20 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: TurboPascal Fail,TypeOf
« Reply #4 on: June 20, 2015, 11:51:38 am »
Yes. Of course fail and typeof are supported. FPC is generally more TP compatible than GPC ever was:

Actually to my best knowledge, Typeof's main use case is TVision stream registration records, and fail is sometimes used for emulating abstract functions in combination with a log of a traceback iirc. (but TP objects are a really long time ago for me)

Maybe GPC has no FV/TV, so maybe the request that trigger their implementation had to be something else.

Code: [Select]
{$mode tp}
uses objects;

type  t= object(tcollection)
               constructor init;
             end;

constructor t.init;
begin
  fail;
end;

begin
writeln(ptrint(typeof(t)));
end.

compiles fine.

Documentation seems to be absent (though FAIL helper is shown in a piece of assembler in the name mangling paragraph, and the internal fpc_in_typeof helper (that the language construct compiles to) is documented :)

typeof is also used in an streaming example in the manual.

Please file a bug for missing documentation of these two items.
« Last Edit: June 20, 2015, 11:55:45 am by marcov »

jorgicor

  • New Member
  • *
  • Posts: 11
Re: TurboPascal Fail,TypeOf
« Reply #5 on: June 20, 2015, 08:49:56 pm »
Thank you very much for the info.

I'll fill the bug report to request these functions to be documented.


 

TinyPortal © 2005-2018