1) Is it possible to use generics to define min() and max() for arbitrary types for which comparison operators exist, i.e. scalars rather than just classes etc.?
A generic can (currently) only use operators that are a "part" of the type in question. Meaning either builtin operators or for records operator overloads that are part of the record. Global operator overloads are not supported.
2) What range of compiler versions support this?
Support for generic routines (aka functions, procedures, methods) is added with 3.2. For older versions you'll have to define a generic class or record with non-generic class functions.
3) Can this be inlined efficiently?
As long as the implementation was already parsed (and the function is declared
inline) the compiler will try to do so. Please note that 3.2 might be better here than 3.0, cause I had changed/improved a few things there when I implemented support for generic routines.
4) Can this be generalised efficiently to support e.g. max([a, b, c]); ?
As long as the types have builtin operators you should be able to do this with an open array parameter ("aVals: array of T").
Please note that FPC does not yet support implicit specializations (there exists a patch for it, but I've yet to review it), thus you need to specialize explicitly:
specialize Max<Longint>([a, b, c]) or for mode Delphi:
Max<LongInt>([a, b, c]) (though there might be situations where FPC does not yet parse an expression correctly as mode Delphi allows overloads of symbols (please report such
)).