Discussion:
Reflection and meta-types
Kaylen Wheeler
2016-03-08 19:24:40 UTC
Permalink
I've been looking through the various core.typed types, and there doesn't
seem to be a type for types. I know that it's possible to use something
like java.lang.Class, but this isn't consistent across platforms, and also
doesn't include user-defined tags that may be used as part of the :type
metadata.

It may be useful to have the Type type be parameterized as well, so you
could add a notation like the following to clojure.core.typed:


(ann clojure.core.typed (All [x] [x -> (Class x)]))


Are there any plans in the future to introduce something like this? If
not, do you think it would be a difficult feature to contribute?
Kaylen Wheeler
2016-03-08 19:25:17 UTC
Permalink
correction: I should have put (Type x) instead of (Class x)
Post by Kaylen Wheeler
I've been looking through the various core.typed types, and there doesn't
seem to be a type for types. I know that it's possible to use something
like java.lang.Class, but this isn't consistent across platforms, and also
doesn't include user-defined tags that may be used as part of the :type
metadata.
It may be useful to have the Type type be parameterized as well, so you
(ann clojure.core.typed (All [x] [x -> (Class x)]))
Are there any plans in the future to introduce something like this? If
not, do you think it would be a difficult feature to contribute?
Ambrose Bonnaire-Sergeant
2016-03-08 19:44:18 UTC
Permalink
TFns allow us to construct type functions, but I don't understand what your
proposing. Can you elaborate please, preferably with a specific example?

Thanks,
Ambrose
Post by Kaylen Wheeler
correction: I should have put (Type x) instead of (Class x)
Post by Kaylen Wheeler
I've been looking through the various core.typed types, and there doesn't
seem to be a type for types. I know that it's possible to use something
like java.lang.Class, but this isn't consistent across platforms, and also
doesn't include user-defined tags that may be used as part of the :type
metadata.
It may be useful to have the Type type be parameterized as well, so you
(ann clojure.core.typed (All [x] [x -> (Class x)]))
Are there any plans in the future to introduce something like this? If
not, do you think it would be a difficult feature to contribute?
Kaylen Wheeler
2016-03-08 22:02:38 UTC
Permalink
I think I've mentioned this in a previous post and included a Scala
example, which may have not been that well understood.

The reason I'm looking to do this is that I'm implementing an
entity-component-system where entities are maps of components. Each
component type would be a subtype of "Component", and an entity may have
one component of each type. Also, components are indexed by their types.

For example, an entity in a simple game might have a component for
Position, Velocity, Sprite, etc. You can access these by looking them up
by their runtime type tags. So, if you want the Position component of an
entity, you do something like this:

(get-component entity Position)


Here is how a component would be added to an entity:

(let [c <some_component>
e <some_entity>]
(assoc e (type c) c))

If I wanted to define a type for entities, it could be something like this:

(defalias Entity (Map java.lang.Class Component))

However, this definition doesn't impose the constraints that I would like
to see. In particular, I would like it to be something more like this:

(defalias Entity
(All [[x :< Component]]
(Map (Type x) x)))

Where (Type x) represents the result of (clojure.core/type
<some_instance_of_x>).

Also, I would like to implement a strongly-typed accessor function,
get-component:

(ann get-component (All [[x :< Component]] [(Type x) -> (Option x)]))
(defn get-component [entity t]
(entity t))

This way, if we call (get-component entity Sprite), we can be sure that the
resulting value will be (Option Sprite).

Does this make sense?
Post by Ambrose Bonnaire-Sergeant
TFns allow us to construct type functions, but I don't understand what
your proposing. Can you elaborate please, preferably with a specific
example?
Thanks,
Ambrose
Post by Kaylen Wheeler
correction: I should have put (Type x) instead of (Class x)
Post by Kaylen Wheeler
I've been looking through the various core.typed types, and there
doesn't seem to be a type for types. I know that it's possible to use
something like java.lang.Class, but this isn't consistent across platforms,
and also doesn't include user-defined tags that may be used as part of the
:type metadata.
It may be useful to have the Type type be parameterized as well, so you
(ann clojure.core.typed (All [x] [x -> (Class x)]))
Are there any plans in the future to introduce something like this? If
not, do you think it would be a difficult feature to contribute?
Loading...