Forum > General
Helper functions for Integers and other primitive types
Thaddy:
Demo multihelpers (needs, trunk/main 3.3.1)
--- 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 helpers2;{$if fpc_fullversion < 30301}{$error this program needs trunk}{$endif}{$mode objfpc}{$H+}{$modeswitch typehelpers}{$modeswitch multihelpers}uses sysutils;type TIntegerHelper = type helper for integer procedure inc(const num:integer = 1); end; procedure TIntegerHelper.inc(const num:integer); begin system.inc(self,num); end; var i:integer = 100;begin i.inc; writeln(i); i.inc(99); writeln(i.tostring);end.Both examples need trunk/main, otherwise they won't compile
Thaddy:
Demo for version < fpc 3.3.1:
--- 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 helpers3;{$if fpc_fullversion < 30000}{$error this program needs 3.0.0 or higher}{$endif}{$mode objfpc}{$H+}{$modeswitch typehelpers}uses sysutils;type TMyIntegerHelper = type helper 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); i.inc(99);end.This compiles from version 3.0.0
So if you have a version below trunk, you must use this version and only one helper can be in scope.
carl_caulkett:
No worries, I forgot the line {$modeswitch TypeHelpers}.
carl_caulkett:
--- Quote from: Thaddy on October 07, 2024, 01:31:31 pm ---Demo inherited helpers:
--- End quote ---
Thanks, Thaddy. That's brilliant help 🙏🏽
I'm not explicitly on trunk for either Lazarus or FPC, but both inherited and multi seem to work fine :D
Navigation
[0] Message Index
[*] Previous page