Is there a way to use Object.factory with templated classes? Or some way to construct templated classes given RTTI of an instance?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Sep 27 09:58:25 UTC 2018


On Thursday, September 27, 2018 2:44:25 AM MDT Chad Joan via Digitalmars-d-
learn wrote:
> The spec seems to have the homogeneous cases covered: classes
> with classes or structs with structs.  What I'm more worried
> about is stuff like when you have a class compared to a struct or
> builtin type, or maybe a struct compared to a builtin type
> (especially more complicated builtin types like arrays!).  The
> homogeneous cases are important for making a type consistent with
> itself, but the other cases are important for integrating a type
> with everything else in the ecosystem.
>
> Notably, "alias this" is awesome and has more or less solved that
> for me in the pedestrian cases I tend to encounter.  I can write
> a struct and alias this to some reference variable that will be
> representative of my struct's "nullness" or other states of
> existence.
>
> But I wouldn't be surprised if there are corner-cases I haven't
> encountered yet (actually I think I just remembered that this bit
> me a little bit once or twice) where having a single alias-this
> isn't sufficient to cover all of the possible things my
> struct/class could be compared to (ex: if the type's null-state
> corresponded to int.max for ints and float.nan for floats, and
> you can't just use opEquals, such as when the type is a class and
> could be precisely null).

For two types to be compared, they must be the the same type - or they must
be implicitly convertible to the same type, in which case, they're converted
to that type and then compared. So, as far as D's design of comparison goes,
there is no need worry about comparing differing types. At most, you need to
worry about what implicit type conversions exist, and D isn't big on
implicit type conversions, because they tend to cause subtle bugs. So, while
they definitely affect comparison, they don't affect anywhere near as much
as they would in a language like C++. In general, you're not going to get
very far if you're trying to make it possible to compare a user-defined type
against other types in D without explicitly converting it first.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list