Recent

Author Topic: [solved] test class type without try except  (Read 1302 times)

CapelliC

  • Jr. Member
  • **
  • Posts: 58
[solved] test class type without try except
« on: June 30, 2020, 03:16:04 pm »
In PParser, a wide range of classes are employed.
I'd like to know if there is a way to avoid this kind of 'pattern matching'

Code: Pascal  [Select][+][-]
  1. function PasElementGetVarType(const AElement: TPasElement): TPasType;
  2. begin
  3.   try
  4.     Result := (AElement as TPasVariable).VarType
  5.   except
  6.     try
  7.       Result := (AElement as TPasArgument).ArgType;
  8.     except
  9.       try
  10.         Result := (AElement as TPasFunction).FuncType.ResultEl.ResultType;
  11.       except
  12.         Result := (AElement as TPasResultElement).ResultType;
  13.       end;
  14.     end;
  15.   end;
  16. end;
  17.  

because it makes almost impossible to debug the code (specifically, Pas2Php),
since every mismatched cast issues two exceptions, so I'm forced to disable them from the debugger UI, just to be left at the end with a truncated file and the impossibility to know where the (rather complex) program has failed.

I had a cursory look into the documentation, but the meta types documentation is rather complex, as it's ought to be for the complex Pascal types, and maybe doesn't provide an idiomatic pattern for beginners (why the section class reference definition (TClassRefDef) is empty?)... Just to provide an hint of what I'd like to obtain, in C++, a dynamic_downcast<Derived*>(Super*) could be a solution.

TIA, Carlo
« Last Edit: June 30, 2020, 03:30:47 pm by CapelliC »

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: test class type without try except
« Reply #1 on: June 30, 2020, 03:28:34 pm »
try using the is operator
Code: Pascal  [Select][+][-]
  1. function PasElementGetVarType(const AElement: TPasElement): TPasType;
  2. begin
  3.   if AElement is TPasVariable then
  4.     Exit(TPasVariable(AElement).VarType);
  5.   if AElement is TPasArgument then
  6.     Exit(TPasArgument(AElement).ArgType);
  7.   if AElement is TPasFunction then
  8.     Exit(TPasFunction(AElement).FuncType.ResultEl.ResultType);
  9.  
  10.   Result := (AElement as TPasResultElement).ResultType;
  11. end;

CapelliC

  • Jr. Member
  • **
  • Posts: 58
Re: test class type without try except
« Reply #2 on: June 30, 2020, 03:30:27 pm »
Much better! Thank you.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: test class type without try except
« Reply #3 on: June 30, 2020, 03:30:47 pm »
You should use is, not only as
Code: Pascal  [Select][+][-]
  1. if AElement is TPasVariable then result := (AElement as TPasVariable).VarType else if //the rest, no exceptions
Not tested, but should do the trick?

Sorry, posts crossed, the solution was already given seconds before.
[edit] and yes, if is works, you can hardcast.
« Last Edit: June 30, 2020, 04:05:19 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: [solved] test class type without try except
« Reply #4 on: June 30, 2020, 04:27:27 pm »
I had a cursory look into the documentation, but the meta types documentation is rather complex, as it's ought to be for the complex Pascal types, and maybe doesn't provide an idiomatic pattern for beginners (why the section class reference definition (TClassRefDef) is empty?)... Just to provide an hint of what I'd like to obtain, in C++, a dynamic_downcast<Derived*>(Super*) could be a solution.

I take it you mean this page? Sorry to disappoint you, but that is about the internals of the compiler.

What you are looking for is Class definitions (at the bottom) in the Language Reference Guide.

CapelliC

  • Jr. Member
  • **
  • Posts: 58
Re: [solved] test class type without try except
« Reply #5 on: June 30, 2020, 08:47:36 pm »
Many thanks, a classic case of RTFM  :-[

 

TinyPortal © 2005-2018