Forum > General
Helper functions for Integers and other primitive types
Thaddy:
It is not IDE magic. It is is built into the language. You can use the helpers in any editor
carl_caulkett:
I've just discovered that you can use TStringHelpers on the class itself, rather than just instances of a string... so: "LineStr := string.Create(LineChar, 180);"
This is so cool :D
I see from the docs at https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.html that the members of TStringHelper are split into "class function" and "function" so that explains why my discovery works ;)
TRon:
@carl_caulkett:
Or things 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";}};} --- writeln('remove me and keep this'.Remove(0,9));
carl_caulkett:
I've tried to define my own helper for Integers using inheritance, with this...
--- 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";}};} --- TcaIntegerHelper = type helper(TIntegerHelper) for Integer public function Pred: Integer; end;
and by using multihelpers with this...
--- 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";}};} ---{$modeswitch multihelpers} uses ... Sysutils, ... type TcaIntegerHelper = type helper for Integer public function Pred: Integer; end;
In both cases it's falling over at the TcaIntegerHelper definition with...
--- Code: Text [+][-]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";}};} ---camidimac.pas(17,27) Error: Identifier not found "helper"
What am I doing wrong?
Thaddy:
Demo inherited helpers:
--- 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";}};} ---program helpers;{$if fpc_fullversion < 30301}{$error this program needs trunk}{$endif}{$mode objfpc}{$H+}{$modeswitch typehelpers}uses sysutils;type TMyIntegerHelper = type helper(TintegerHelper) for integer procedure inc(const num:integer = 1); end; procedure TMyIntegerHelper.inc(const num:integer); begin system.inc(self,num); end; var i:integer = 100;begin i.inc; writeln(i);// 101 i.inc(99); writeln(i);// 200end.
Navigation
[0] Message Index
[#] Next page
[*] Previous page