Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
Am 24.11.2015 08:04 schrieb "Michael Schnell": > > Is this syntax candy for automatically creating multiple functions (completely dedicated code for each type (combination) it is called with) (in fact optimizing for CPU usage), or is a single code sequence generated that takes different branches at certain locations (in fact optimizing for code size) ? It's the first one. This is also the case for generic types by the way, so nothing new here... (And before someone asks: yes, right now there are situations where this leads to code bloat, but my plan is to sometime(TM) add a link time optimization or WPO pass that combines code of specializations with the same types in different units again) Regards, Sven ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
Hi all, If type inference is not planned, then do you have an alternative to support linq like expression methods ? If type inference is not used then the syntax is very verbose if not ugly. Also I though that type inference is kind of important for proper lambda support but I might be wrong. Anyways congratulations, very nice feature. Thank you Chriss Kalogeropoulos On Nov 22, 2015 11:32 AM, "Sven Barth"wrote: > On 22.11.2015 00:04, Anthony Walter wrote: > >> Sven, first off, congratulations and we thank you for so much good work. >> in the future do you see a time when type inference could be used? >> >> S := Add('hello ', 'world'); >> I := Add(1, 2); >> > > I honestly don't think that type inference should become part of Pascal, > it's simply not part of the language philosophy and it would also not play > well with the need for "specialize" in ObjFPC mode and as long as Delphi > doesn't introduce it I won't add it only for mode Delphi either. > > Which then brings to mind, will it be possible to get use references to >> generic routines? >> > > If you have read the limitations part of my mail then you should know that > this is a known limitation and to be addressed in the future. > > type >>TTransform = function(const A, B: T): T; >> > > FPC already supports this, though the syntax is > > === code begin === > > type > TTransform = function(const A, B: T): T; > > === code end === > > Or with added "generic" keyword in case of non-Delphi modes. > > var >>Transform: TTransform; >> > > This is not a valid type declaration and never will be, because "T" is not > defined. The correct one is > > === code begin === > > var > Transform: TTransform; > > === code end === > > begin >>Transform := Add; >>WriteLn(Transform('hello', 'world')); >>Transform := Min; >>WriteLn(Transform('hello', 'world')); >>Transform := Max; >>WriteLn(Transform('hello', 'world')); >> end; >> > > This would work once assignments of routines work. In non-Delphi modes the > syntax will be > > === code begin === > > Transform := @specialize Add; > Writeln(Transform('hello', 'world')); > > === code end === > > function Print(const A, B: T; Transform: TTransform); >> begin >>WriteLn(Transform(A, B)); >> end; >> >> begin >>Print('hello', 'world', Add); // type inference >>Print('hello', 'world', Min); >>Print('hello', 'world', Max); >> end; >> > > No type inference. > > Regards, > Sven > > ___ > fpc-devel maillist - fpc-devel@lists.freepascal.org > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel > ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
Am 23.11.2015 13:12 schrieb "Chriss Kalogeropoulos": > > Hi all, > > If type inference is not planned, then do you have an alternative to support linq like expression methods ? If type inference is not used then the syntax is very verbose if not ugly. > > Also I though that type inference is kind of important for proper lambda support but I might be wrong. Type inference and support for lambda expressions are completely orthogonal. Type inference is merely a syntactic sugar to decrease the amount of typing needed. Regards, Sven ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
Is this syntax candy for automatically creating multiple functions (completely dedicated code for each type (combination) it is called with) (in fact optimizing for CPU usage), or is a single code sequence generated that takes different branches at certain locations (in fact optimizing for code size) ? -Michael ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
Sven, fair enough. I'll test out what you've published and if I find anything interesting (strange compiler errors, edge cases, useful scenarios) I'll post replies. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
2015-11-21 19:09 GMT+01:00 Sven Barth: > The compiler will error out like this: > > === output begin === > > Target OS: Linux for i386 > Compiling ./fpctests/tgenroutines.pp > tgenroutines.pp(9,47) Fatal: Syntax error, ")" expected but "+" found > Fatal: Compilation aborted Maybe bug #24098 is related to mentioned error: http://bugs.freepascal.org/view.php?id=24098 -- Best regards, Maciej Izak ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
On 21.11.2015 20:01, Maciej Izak wrote: 2015-11-21 19:09 GMT+01:00 Sven Barth>: The compiler will error out like this: === output begin === Target OS: Linux for i386 Compiling ./fpctests/tgenroutines.pp tgenroutines.pp(9,47) Fatal: Syntax error, ")" expected but "+" found Fatal: Compilation aborted Maybe bug #24098 is related to mentioned error: http://bugs.freepascal.org/view.php?id=24098 Indeed it is. It has the same cause. Regards, Sven ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Feature announcement: Generic functions, procedures and methods
2015-11-21 18:32 GMT+01:00 Sven Barth: > Hello together! > > I'm pleased to finally announce the addition of generic functions, > procedures and methods (collectively called "routines") to Free Pascal > which allows writing type safe methods that can be used for multiple types. > Congratulations :D... It forces me to update Generics.Collections :P Because of the missing "specialize" keyword that mark specializations > complex expressions *do not* work in mode Delphi yet. So assignments of > function results are okay, but other than that you'll likely encounter > compiler errors. > Could you please show some example of limitations? -- Best regards, Maciej Izak ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[fpc-devel] Feature announcement: Generic functions, procedures and methods
Hello together! I'm pleased to finally announce the addition of generic functions, procedures and methods (collectively called "routines") to Free Pascal which allows writing type safe methods that can be used for multiple types. Syntax: The syntax for declaring a generic routine is similar to declaring a normal routine and also includes support for class methods: generic [class] (procedure|function) IDENTIFIER[(PARAMETERLIST)][: RESULTTYPE]; MODIFIERS; For the TYPEARGLIST the same rules apply as for generic types. Type parameters declared in the TYPEARGLIST might be used in the PARAMETERLIST, the RESULTTYPE and of course the body of the routine. Generic routines can be overloaded both by TYPEARGLIST and PARAMETERLIST. To call a generic routine you use the following syntax: specialize IDENTIFIER[(PARAMETERS)] For the TYPELIST the same rules apply as for specializing a generic type. If the routine is part of a type or variable or you're using a unit name to distinguish a generic routine you need to put that before the "specialize": TYPENAME.specialize IDENTIFIER[(PARAMETERS)] VARIABLE.specialize IDENTIFIER[(PARAMETERS)] UNITNAME.specialize IDENTIFIER[(PARAMETERS)] Calls to generic routines are normal factors so they can be used as such as the following example shows: === example begin === {$mode objfpc} generic function Add(aLeft, aRight: T): T; begin Result := aLeft + aRight; end; begin Writeln(specialize Add('Generic ', 'routines') + specialize Add(' with ', 'Free Pascal')); end. === example end === Delphi compatibility: Of course this future is also implemented in a Delphi-compatible way for the Delphi modes. There the syntax for declaring a generic function is like this: [class] (procedure|function) IDENTIFIER[(PARAMETERLIST)][: RETURNTYPE]; MODIFIERS; So merely the "generic" keyword is missing. This is analogous when calling a generic routine: IDENTIFIER[(PARAMETERS)] Because of the missing "specialize" keyword that mark specializations complex expressions *do not* work in mode Delphi yet. So assignments of function results are okay, but other than that you'll likely encounter compiler errors. Please note that unlike Delphi we *do* support global generic functions/procedures even in Delphi mode. Limitations/ToDos: The feature is not yet finished and there are some limitations that yet need to be overcome or parts that simply don't work yet, this includes, but is not necessarily limited to: - support for complex expressions in Delphi modes (applies to type specializations as well) - support for pointers to generic routines (this will currently result in errors at the best case and internal errors or exceptions at the worst) - support for the return value in modes without support for "Result" - support for nested generics, most importantly generic methods inside generic classes So please test and report any bugs in the bug tracker. Questions can of course be asked here on the mailing list (no, this feature won't be part of FPC 3.0.0). Regards, Sven ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel