Can you support a record type in Cooper the same way as it works in Prism? In .NET a record type is treated as a class with self initialized constructor (and copy constructor). This could be hidden under Allow Delphi compatibility syntax option.
It’s a very important Delphi feature that stops us from migrating existing large code to Android/Java platform. Until now we have the same sources for Delphi and .NET platform and would like to keep it for Java. To demonstrate the problem I wrote a sample code that we would like to have supported in Java :
interface part :
// original Delphi
TGIS_Point = record
public
X : Double ;
Y : Double ;
end;
// forced change to Java
TGIS_Point = class
public
X : Double ;
Y : Double ;
public
constructor Create (
) ; overload ;
constructor Create ( const _x, _y : Double
) ; overload ;
end;
implementation
class method ConsoleApp.Main(args: array of String);
var
ptg : TGIS_Point ; // * for Java I have to do := new TGIS_Point();
begin
ptg.X := 10 ;
ptg.Y := 20 ;
System.out.println('Hello World at '+ ptg.X + ’ and ’ + ptg.Y);
end;
How can I use the same code for both Delphi/Prism and Java (without rewriting existing code with record->class replacement and manual initialization of variables (*) ?
Artur,
perhaps you can write an aspect to do the trick. The aspect library could be different for the .NET and Java projects. The .NET aspect would do nothing, but the Java aspect could add the requested constructors.
For more information about aspects, see http://wiki.oxygenelanguage.com/en/Cirrus.
Patrick
I read about cirrus, but cannot find good documentation and examples how to use it in my case. Which interface and aspect is used when declaring a variable? Patrick are you familiar with aspects?
Can you support a record type in Cooper the same way as it works in Prism?
There is a feature request in our issues (#54477). The feature will be added in the neares future. I’ll let you know when it is done so you can test it on your code.
I’m afraid aspects cannot help in your case as far as you still need to chage “record” to “class” and manually initialize vars.
That is a great news. I cannot wait for it Feel free to use my Personal Downloads for sharing new versions for testing. Are there any other features that will improve Delphi compatibility, e.g. unsigned types?
Maybe a bit off topic, but doing today porting of some code I noticed that var/our parameters of methods are not supported too. This is even bigger problem. Are there any other limitations/incompatibility with Delphi that I should be aware of? How var/our issue can be resolved? I just found that passing Object types parameters is via reference.
the Java runtime doesn’t support “var” parameters. The closest workaround would be to pass a wrapper Object with writable properties, or to return a tuple, i’m afraid. (support for .NET/Java-compatible language-native tuples is something we have planned for a release soon).
Tuples are a concept that can be implemented with plain classes (as they are on .NET 4). the JRE doesn’t have any standard ones, but you can easily define them (and let your method return a Tuple<String, String>):
type
Tuple1 = public class
public
constructor(aItem1: P1);
property Item1: P1; readonly;
end;
Tuple2 = public class
public
constructor(aItem1: P1; aItem2: P2);
property Item1: P1; readonly;
property Item2: P2; readonly;
end;
...
(8 should be more than enough)
then a plain tuple class like:
Tuple = public class
class method &New(a: P1): Tuple1;
class method &New(a: P1; b: P2): Tuple1;
class method &New(a: P1; b: P2; c: P3): Tuple1;
class method &New(a: P1; b: P2; c: P3; d: P4): Tuple1;
class method &New(a: P1; b: P2; c: P3; d: P4; e: P5): Tuple1;
class method &New(a: P1; b: P2; c: P3; d: P4; e: P5; f: P6): Tuple1;
class method &New(a: P1; b: P2; c: P3; d: P4; e: P5; f: P6; g: P7): Tuple1;
class method &New(a: P1; b: P2; c: P3; d: P4; e: P5; f: P6; g: P7; h: P8): Tuple1;
end;
...
then you can define your method as:
method Test: Tuple2; // returns a string and double
begin
exit Tuple.New('Hello', 13.5);
end;
I made a simple code demonstrating how to replace var parameter in Java :
namespace consoleapplication5;
interface
uses
java.util;
type
TupleT = public class
private
FValue : T ;
public
constructor(_value: T);
constructor;
property Value: T read FValue write FValue ;
end;
Tuple = public class
public
class function &New(v:T) : TupleT;
class function &New : TupleT;
end;
TBytes = public array of Byte ;
type
ConsoleApp = class
public
class method Main(args: array of String);
class method testTuple( _v : TupleT ) ;
class method testArray( _v : TBytes ) ;
end;
class function Tuple.&New(v: T): TupleT;
begin
Result := TupleT.Create(v);
end;
class function Tuple.&New: TupleT;
begin
Result := TupleT.Create;
end;
class method ConsoleApp.testTuple(_v: TupleT);
begin
_v.Value := Byte(10) ;
end;
class method ConsoleApp.testArray(_v: TBytes);
begin
_v[0] := 10 ;
end;
class method ConsoleApp.Main(args: array of String);
var
b : Byte ;
t : TupleT ;
tb : TBytes ;
begin
t := Tuple.New;
testTuple(t);
b := t.Value ;
System.out.println(b);
tb := new array of Byte(1);
testArray(tb);
b := tb[0] ;
System.out.println(b);
end;
end.
and just wonder if this cannot be done on compiler site when finding var declaration?