Forum > General

default method for objects

(1/2) > >>

speter:
G'Day Folks,

Is it possible to have a default method or similar for objects (not classes). For example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  tfoo = object    v : byte;    procedure init;    function getit : byte;  end;procedure tfoo.init;begin  v := 255;end;function tfoo.getit : byte;begin  result := v;end; var  zz : tfoo;  k : byte;begin  zz.init;  k := zz.v;      // this works  k := zz.getit;  // this works  k := zz;        // i'd like to do this - have it return 'v' or call getit()end;
In other words, can I code it so that function getit is called as a default action. ;)

cheers
S.

jamie:
You need to use an operator overload but  you can not do that with OBJECTS, you need to use the ADVANCEDRECORDS mode which gives you everything you are using objects for atm.

 You can define an operator := for the record.

 fpc also supports this outside too but its a little tricky.

speter:
Thanks for the quick answer, jamie.

I tried:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$H+}{$modeswitch advancedrecords} ...type  tfoo = record    v : byte;     procedure init;    function getit : byte;    class operator Implicit(a : byte) : tfoo;  end;
But received the error msg : "Error, it is not possible to overload this operator" (pointing to line 10 above).

So I'm still missing something.  :o

Also, if I understand the implicit operator, it is like:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---zz := 22;rather than

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---k := zz;
cheers
S. :)

Thaddy:
You use the wrong syntax for the assignment operator. You use delphi syntax but you specify mode objfpc.
Here a sketch how it would work with the correct objfpc syntax:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode objfpc}{$H+}{$modeswitch advancedrecords}type  Tfoo = record    v : byte;    class operator initialize(var foo:Tfoo);    class operator :=(a : byte) : tfoo;    class operator :=(a : TFoo) : Byte;  end;   class operator Tfoo.:=(a:byte):Tfoo;  begin    Result.v := a;  end;    class operator Tfoo.:=(a:TFoo):Byte;  begin    Result := a.v;  end;    class operator TFoo.Initialize(var Foo:Tfoo);  begin    Foo := Default(TFoo);    Foo.v :=255;  end;  var   f,f2:Tfoo;  b:byte;begin  writeln(f.v);  f2 := 10;  writeln(f2.v);  b := f;  writeln(b);end.
This should get you on the right track.

speter:
Thanks very much for your help Thaddy (and jamie)!

My original question was: can I do something like:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  zz : tfoo;  k : byte;begin  zz.init;  k := zz;        // i'd like to do this - have it return 'v' or call getit()end;
is this possible?

cheers
S. :o

Navigation

[0] Message Index

[#] Next page

Go to full version