Jump to content

Eugine Savin

Members
  • Content Count

    15
  • Joined

  • Last visited

Posts posted by Eugine Savin


  1. if I pass set type into MyGenericMethod<T: record> - I get compile type error [dcc32 Error] Project2.dpr(28): E2512 Type parameter 'T' must be a non-nullable value type

    Is it OK or compiler bug ??

     

    program Project2;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils;
    
    
    type
      TMyEnum = (en1);
      TMySet = set of TMyEnum;
    
    type
      TTestClass = class
        class procedure MyGenericMethod<T: record>;
      end;
    
    class procedure TTestClass.MyGenericMethod<T>;
    begin
    //
    end;
    
    begin
      try
        { TODO -oUser -cConsole Main : Insert code here }
        TTestClass.MyGenericMethod<TMySet>();
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.

     



  2.  

    class function TNumbers.FromInteger<T>(AValue: Integer): T;
    type
      PSingle = ^Single;
    begin
      if TypeInfo(T) = TypeInfo(Single) then PSingle(@Result)^ := AValue
      else if TypeInfo(T) = TypeInfo(Integer) then PInteger(@Result)^ := AValue
      else raise Exception.Create('Error Message');
    end;

     


  3. type
      TNumbers = class
      public
        class function One<T>: T;
      end;
    
    { TNumbers }
    
    const
      OneSingle: Single = 1;
      OneInteger: Integer = 1;
    class function TNumbers.One<T>: T;
    type
      PT = ^T;
    begin
      if TypeInfo(T) = TypeInfo(Single) then Result := PT(@OneSingle)^
      else if TypeInfo(T) = TypeInfo(Integer) then Result := PT(@OneInteger)^
      else raise Exception.Create('Error Message');
    end;
      
    ...
    FPen.Width := TNumbers.One<T>;

     


  4. 1 minute ago, Rudy Velthuis said:

    How would you do this in, say, C++?

    In C++ there is not differences between functions and overloaded operators.

    struct MyArray {
    public:
        template <std::size_t N>
        bool operator==(int (&rhs)[N]) { std::cout << N << '\n'; return true; }
    };
    
    int main() {
        // your code goes here
        int arr1 [1] = {0};
        int arr2 [2] = {0, 0};
        MyArray myArr;
        if (myArr == arr1 && myArr == arr2) {}
        return 0;
    }

     

    20 minutes ago, Rudy Velthuis said:

    And don't use operators.

    Can you explain why you allow operators with dyn arrays, and do not allow ones with open arrays ?


  5. 1 minute ago, Rudy Velthuis said:

    it doesn't make sense to give operators open array parameters.

     

    TMyArray = record
    ..
    end;
    
    const Arr1: array[0..1] of Integer = (..)
    const Arr2: array[0..2] of Integer = (..)
    var MyArray: TMyArray ..

     

    I wanna have ability to compare if (MyArray == Arr1) or (MyArray = Arr2)..   How can I get it without open arrays as operator parameters ?


  6. Simple question, why "Multiply" operator works, and "Equal" does not. compiler give error  "[dcc32 Error] Project1.dpr(33): E2015 Operator not applicable to this operand type"

     

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils;
    
    type
      TMyRecord2 = record
      public
        class operator Equal(const a: TMyRecord2; const b: array of Integer): boolean;
        class operator Multiply(const a: TMyRecord2; const b: array of Integer): TMyRecord2;
      end;
    
    class operator TMyRecord2.Equal(const a: TMyRecord2; const b: array of Integer): boolean;
    begin
      Result := True;
    end;
    
    class operator TMyRecord2.Multiply(const a: TMyRecord2; const b: array of Integer): TMyRecord2;
    begin
      Result := a;
    end;
    
    var
      b: boolean;
      r: TMyRecord2;
    begin
      try
        r := r * [10]; // this line is compiled
        b := r = [10]; // this line is not compiled
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.

     

    • Like 1
×