On Tue, Nov 11, 2014 at 5:34 PM, silvioprog <silviop...@gmail.com> wrote:
> On Sat, Nov 1, 2014 at 1:54 AM, leledumbo <leledumbo_c...@yahoo.co.id> > wrote: > >> > Hm... so I'll choose the mode delphi in my new projects to type less >> code >> hehe... >> >> You actually ended up typing much less using objfpc style generics. Think >> of >> how many angle brackets you would need to type in Delphi mode? And how >> many >> if the specialized type changes? ;) >> Delphi generics can actually do the same (create a new type first), but >> objfpc enforces this. > > > Yes, but I had some problems to understand the generic sintax in objfpc > (it is not documented). =( > > For example, how to compile this code using objfpc?: > > unit Core.Singleton; > > {$mode delphi} > > interface > > type > > { TSingleton } > > TSingleton<T: class, constructor> = class(TObject) > public > class function GetInstance: T; > end; > > implementation > > { TSingleton } > > class function TSingleton<T>.GetInstance: T; > begin > { TODO: implement! } > end; > > end. > > If I try to use mode objfpc: > > unit Core.Singleton; > > {$mode objfpc}{$H+} > > interface > > type > > { TSingleton } > > generic TSingleton<T: class, constructor> = class(TObject) > public > class function GetInstance: T; > end; > > implementation > > { TSingleton } > > class function TSingleton<T>.GetInstance: T; > begin > { TODO: implement! } > end; > > end. > > The error is: > > Compile Project, Target: project1.exe: Exit code 1, Errors: 1 > Core.Singleton.pas(11,32) Error: Illegal expression. > > =( > Oh, sorry. Just: unit Core.Singleton; {$mode objfpc}{$H+} interface type { TSingleton } generic TSingleton<T: class> = class(TObject) public class function GetInstance: T; end; implementation { TSingleton } class function TSingleton.GetInstance: T; begin end; end. And compiled like a charm. =) -- Silvio Clécio My public projects - github.com/silvioprog
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal