首页 > 解决方案 > 在其他单元重新声明类型助手,Free Pascal 编译失败

问题描述

我有这个单位

unit helper;

interface

{$MODE OBJFPC}
{$MODESWITCH TYPEHELPERS}
{$H+}

uses
     sysutils;

type

    THelloWorldHelper = type helper(TStringHelper) for string
    public
         function world() : string;
    end;

implementation

   function THelloWorldHelper.world() : string;
   begin
       result := self + ' world';
   end;
end.

和其他重新声明此类型助手的单元

unit helperalias;

interface

{$MODE OBJFPC}
{$MODESWITCH TYPEHELPERS}
{$H+}

uses

   helper;

type

   THelloWorldHelper = helper.THelloWorldHelper;

implementation
end.

和程序如下

program helloworld;

{$MODE OBJFPC}
{$MODESWITCH TYPEHELPERS}
{$H+}

uses       
   helperalias;

var hello : string;
    world :string;

begin
    hello:='hello';
    world := hello.world();
    writeln(world);
end.

当我跑

fpc helloworld.pas 

它拒绝用 message 编译Error: Objective-C categories and Object Pascal class helpers cannot be used as types

如果在 中helloword.pas,我将helperaliasunit替换为helper,它可以工作。

我读到了助手限制

为什么禁止重新声明类型助手?

标签: freepascal

解决方案


推荐阅读