SRL Archive - Lape

Static Functions?


m34tcode

2014-03-17 18:11:41

Currently, it seems as 'static' function cause access violations, I wrote this script to test that but it doesn't run either.

program new;

function Integer.One() : Integer;
begin
Result := 1;
end;

begin
Writeln(ToStr(Integer.One()));
end.

The Code I have for a 'static' method:


Type
Translation = record
x, y : integer;
end;

function Translation.Create(x, y : Integer) : Translation;
begin
Self.X := Y;
Self.y := Y;
end;

var Trans : Translation;
begin
Trans := Translation.Create(0,1);
end;

Kasi

2014-03-17 18:23:30

@m34tcode;


Type
Translation = record
X, Y : integer;
end;

//change self:
procedure Translation.Create(x1, y1 : Integer); // variable names.
begin
Self.X := x1; // shouldnt you be returning it? (also typo here, should be := x, not Y?)
Self.y := y1;
end;

// like this:
// return self.
function Translation.Return(x1, y1 : Integer) : Translation; // variable names.
begin
Result.X := x1;
Result.Y := y1;
end;

var Trans : Translation;
begin
//Trans := Translation.Create(0,1); // you cant change a type that isnt a variable, if that makes sense.

Trans.Create(0, 1); // or at least be using it like this?
//or
Writeln(Trans);
Trans := Translation.Return(1, 3);
Writeln(Trans);
end;

slacky

2014-03-17 18:31:32

The way to call a function like that is to call it with an instance of the type… Calling with an instance is almost a must, else you will be calling it without allocating memory first, so whenever you try to use "self" lape tries to access some memory which do not "exist" or lape does not own, so you will get an "Access Violation".

EG:

function Integer.One(): Integer;
begin
Result := 1;
end;

var myint:Integer;
begin
WriteLn( myint.One() );
end.


Your second piece of code then becomes:

Type
Translation = record
x, y: integer;
end;

procedure Translation.Create(iX, iY: Integer);
begin
Self.X := iX;
Self.y := iY;
end;

var Trans : Translation;
begin
Trans.Create(0,1);
WriteLn(Trans);
end;

Edit: I noted that Create used X, and Y in parms-field AND in self.. which causes a scoping issue. Updated to reflect it.

m34tcode

2014-03-17 18:32:12

Type
Translation = record
x, y : integer;
end;

function Translation.Create(x, y : Integer) : Translation;
begin
Self.X := Y;
Self.y := Y;
Result := self;
end;

var Trans : Translation;
begin
Trans.Create(0,1);
Writeln(Trans);
end;


Output is 0,0 somehow? It's like the Compiler ignores assignment to self for some reason, since when Self is assigned to result, the value is still unchanged.

I tried this:
Type
Translation = record
x, y : integer;
end;

function Translation.Create(x, y : Integer) : Translation;
begin
Result.X := X;
Result.Y := Y;
end;

var Trans : Translation;
begin
Trans := Translation.Create(0,1);
Writeln(Trans);
end;[/code

And It's back to the access violation. It seems that you cannot call a function through a type, unless there is some other way to do this?

It still doesn't work if I call .Create from a variable of the Translation Type

@Slacky, when you run that code, it still outputs 0,0 :-(

Frement

2014-03-17 18:36:08

Thats because there is a scoping issue.
Type
Translation = record
x, y : integer;
end;

function Translation.Create(lx, ly : Integer) : Translation;
begin
Self.X := lY;
Self.y := lY;
Result := self;
end;

var Trans : Translation;
begin
Trans.Create(0,1);
Writeln(Trans);
end;

m34tcode

2014-03-17 18:38:52

Ah, that was it. It is working, thanks. :-)

Brandon

2014-03-17 19:36:11

Closest you will get to static functions:


{$F ON}
{$X ON}

type struct = record
X: Integer;
Y: Integer;
end

Function struct.static: struct;
var
tmp: struct;
begin
Result := tmp;
end;

begin
struct.static().X;
end.

Still.. it does indeed create an instance. :l