Author Topic: type procedure  (Read 615 times)

0 Members and 1 Guest are viewing this topic.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
type procedure
« on: May 06, 2009, 04:32:26 pm »
Code: [Select]
type
  TTEST = record
    myTest: procedure;
  end;
  TTEST2 = record
    myTest: function: integer;
  end;

procedure myTest2();
begin
  WriteLn('myTest2 called');
end;

function myTest(): procedure;
begin
  WriteLn('myTest called');
end;

procedure ActivateServer();
var
  Test: procedure;
  Test1: TTEST;
  Test2: TTEST2;
  Test3: variant;
  _IDispatch: IDispatch;
  _IUnknown: IUnknown;
begin
  try
    StrtoInt(' ');
  except
    WriteLn('Notice: ExceptionProc=' + InttoStr(ExceptionProc)); // does not add to this value when the keywords "procedure" and "function" are used
    WriteLn('Notice: ExceptionPos=' + InttoStr(ExceptionPos)); // how to know where "70" is?
  end;
  try
    Test1.myTest();
  except
    WriteLn('Test1.myTest();');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test2.myTest();
  except
    WriteLn('Test2.myTest();');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test();
  except
    WriteLn('Test();');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test := Test3;
  except
    WriteLn('Test := Test3;');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test3 := Test;
  except
    WriteLn('Test3 := Test;');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test1.myTest := Test3;
  except
    WriteLn('Test1.myTest := Test3;');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test3 := Test1.myTest;
  except
    WriteLn('Test3 := Test1.myTest;');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test3 := IDispatchInvoke(_IDispatch, true, 'myTest2', []);
  except
    WriteLn('Test3 := IDispatchInvoke(_IDispatch, true, ''myTest2'', []): variant;');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
  try
    Test3 := IDispatchInvoke(_IDispatch, false, 'myTest2', []);
  except
    WriteLn('Test3 := IDispatchInvoke(_IDispatch, false, ''myTest2'', []): variant;');
    WriteLn('  failed: ' + ExceptionToString(ExceptionType, ExceptionParam));
    WriteLn('');
  end;
{ Does not compile:
  Test1.myTest := Test; // Type mismatch
  TTEST.myTest(); // Class type expected
  Test := myTest(); // Type mismatch
}
end;
This compiles, but all of the try catch statements catches something...
Output:
Code: [Select]
Notice: ExceptionProc=4
Notice: ExceptionPos=80
Test1.myTest();
  failed: Out of Proc Range

Test2.myTest();
  failed: Out of Proc Range

Test();
  failed: Out of Proc Range

Test := Test3;
  failed: Exception: Type Mismatch

Test3 := Test;
  failed: Type Mismatch

Test1.myTest := Test3;
  failed: Exception: Type Mismatch

Test3 := Test1.myTest;
  failed: Type Mismatch

Test3 := IDispatchInvoke(_IDispatch, true, 'myTest2', []): variant;
  failed: Exception: Nil interface

Test3 := IDispatchInvoke(_IDispatch, false, 'myTest2', []): variant;
  failed: Exception: Nil interface
Is there any use to set the type of something to procedure or function? I was hoping that this could make it easier for some sort of delegate system.
As far as I've experimented with, you can't actually invoke or modify anything with that type without an exception or compilation error. But maybe you guys could find something that I didn't.
I was also curious about the IDispatch and IUnknown types that still compile. The only method I know of that uses IDispatch throws an exception whenever I call it, but I didn't experiment with that as much.
« Last Edit: May 06, 2009, 04:41:08 pm by DorkeyDear »