On Fri, Aug 10, 2018 at 1:24 PM Simon King <simon.k...@uni-jena.de> wrote:
>
> Hi Erik,
>
> On 2018-08-10, Erik Bray <erik.m.b...@gmail.com> wrote:
> > I've never liked that there are constructors in Sage that are
> > functions but look like class names.  I'd rather NumberField itself
> > were a class, with NumberField.__new__ acting as the factory.  I
> > suspect there is a reason it wasn't done that way but I was never
> > clear on that.
>
> I can think of two reasons:
> 1) Perhaps NumberField predates the development of ClasscallMetaclass?

Yes, that may be true.

> 2) Perhaps NumbarField(data) returns instances of distinct classes
> depending on the data?

I don't think that's a problem.  When you implement the __new__ method
on a class it may return instances of a different class depending on
what the arguments were (just as the NumberField *function* acts as a
factory for instances of different classes, so to could a hypothetical
NumberField.__new__.  There's no direct need for overriding
type.__call__ (i.e. ClasscallMetaclass) in order to have this
behavior, although there may be other subtle reasons for it that I'm
not thinking of.

Instead, to take a different example, Sage has this odd pattern of
having things like `def ComplexField` but then `class
ComplexField_class`.  This also breaks things like `isinstance(obj,
ComplexField)`, resulting in the need for `is_ComplexField`.

If I were doing this from scratch my first inclination would be to write:

class ComplexField(ring.Field):
    def __new__(...):
        <contents of the original "def ComplexField">

One small wrinkle with this is that the way type.__call__ is
implemented is such that the arguments to a class's __new__ must be
the same as the class's __init__.  For some factory functions we may
not always want this to be the case, which is where having something
like ClasscallMetaclass comes into play.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to