On Saturday, July 23, 2016 21:33:29 Rufus Smith via Digitalmars-d-learn wrote:
> I am trying to write some general code that works on arbitrary
> types. I need to compare, obviously, as that is relatively basic
> thing on objects.

That's part of of why attribute inferrence works on templates. That way, the
template isn't constrained with regards to which attributes it uses. If it
can be nothrow or @nogc or whatnot, then the compiler will infer it as such,
and it can be used in code that requires those attributes, but if the
template can't have those attributes with a particular set of template
arguments, then they won't be inferred, and the template will still work,
whereas if the attributes had been put on the template, it wouldn't.

Templates are straight-up the solution for dealing with an arbitrary mix
of attributes. Unfortunately, virtual functions are fundamentally
incompatible with templates, so even if the Object is fixed so that it
doesn't have opEquals, opCmp, toHash, and toString on it, the ones that are
declared on derived classes are still going to have to pick a set up
attributes and will restrict what further derived classed do. It's just that
the programmer will then have been able to choose those restrictions in
their base class rather than being forced by what druntime did with Object.
Classes and generic code really don't mix well and can't mix well when the
generic code needs to be part of the class instead of a free function.

- Jonathan M Davis

Reply via email to