[fpc-pascal] Constants in generics

Sven Barth pascaldragon at googlemail.com
Wed Nov 28 17:25:33 CET 2018


Am Mi., 28. Nov. 2018, 15:27 hat Ryan Joseph <ryan at thealchemistguild.com>
geschrieben:

>
>
> > On Nov 28, 2018, at 7:26 PM, Sven Barth via fpc-pascal <
> fpc-pascal at lists.freepascal.org> wrote:
> >
> > Because that is not supposed to work. Generic routines are *routines*,
> not types. You can't define aliases for routines either, not to mention the
> problems with scoping as generic methods exist as well.
>
> It seems natural you could specialize a generic procedure because you can
> do this for all other generic types. Maybe a type isn’t the right way but
> it feels like there should be some way to specialize the procedure header
> outside of a begin/end block. I know you can't make aliases to procedures
> but generics are more of “instantiating a template” which falls outside the
> scope the language in general.
>

You're not wrong, but a type alias is definitely the wrong way.
But that is a completely different topic, one that I'm not willing to
discuss right now, aside from "we don't support something like that".


> >
> > Generic routines however have a different boon in Delphi that I plan to
> add as well: inference of the type parameters based on the parameters the
> user passed. In that case the type parameter clause as well as the
> "specialize" in non-Delphi modes would not need to be used and it would
> look like a normal, non-generic call.
>
> That’s probably the best way (Swift does this and I always wondered why
> FPC doesn’t) but it won’t work unless the parameters contain the generic
> parameter, which is a minority of generics functions anyways.
>
> Just to be sure, you mean like this?
>
> generic procedure DoThis<T>(msg:T);
> begin
> end;
>
> begin
>
>         DoThis(‘hello’); // compiles as “specialize
> DoThis<string>(‘hello’)” because param types match generic parameter types
>
> If all that’s involved is comparing params types to generic types then
> that looks like a relatively simple solution.  Const generic params may
> have complicated this some however.
>

Yes, all generic parameters need to be part of the routine's parameters and
constant parameters indeed won't work in that case.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20181128/3a494142/attachment.html>


More information about the fpc-pascal mailing list