Yes, this would be similar to the `Typeable` type class in Haskell. It
queries the vtable-equivalent, which contains stuff like the name of the
type and allows doing `typeof(x)`, dynamic casts, etc. This is heavily
magical (that is, depends on the hidden internal representation) and
properly belongs in the standard platform and not in a user-level library.


On Fri, Aug 23, 2013 at 4:40 PM, Niko Matsakis <[email protected]> wrote:

> Currently, this is not directly supported, though downcasting in
> general is something we have contemplated as a feature.  It might be
> possible to create some kind of horrible hack based on objects. A
> trait like:
>
>     trait Dynamic { }
>     impl<T> Dynamic for T { }
>
> would allow any value to be cast to an object. The type descriptor can
> then be extracted from the vtable of the object using some rather
> fragile unsafe code that will doubtless break when we change the
> vtable format. The real question is what you can do with the type
> descriptor; they are not canonicalized, after all. Still, it's
> ... very close.  This is basically how dynamic downcasting would work,
> in any case.
>
>
> Niko
>
> On Fri, Aug 23, 2013 at 07:49:57AM +0300, Oren Ben-Kiki wrote:
> > Is it possible to implement something like Haskell's Dynamic value holder
> > in Rust? (This would be similar to supporting C++'s dynamic_cast).
> > Basically, something like this:
> >
> > pub struct Dynamic { ... }
> > impl Dynamic {
> >     pub fn put(value: ~T) { ... }
> >     pub fn get() -> Option<T> { ... }
> > }
> >
> > I guess this would require unsafe code... even so, it seems to me that
> Rust
> > pointers don't carry sufficient meta-data for the above to work. A
> possible
> > workaround would be something like:
> >
> > pub struct Dynamic { type_name: ~str, ... }
> > impl Dynamic {
> >     pub fn put(type_name: &str, value: ~T) { Dynamic { type_name:
> > type_name, ... } }
> >     pub fn get(&'a self, type_name: &str) -> Option<&'a T> {
> > assert_eq!(type_name, self.type_name); ... } }
> > }
> >
> > And placing the burden on the caller to always use the type name "int"
> when
> > putting or getting `int` values, etc. This would still require some sort
> of
> > unsafe code to cast the `~T` pointer into something and back, while
> > ensuring that the storage for the `T` (whatever its size is) is not
> > released until the `Dynamic` itself is.
> >
> > (Why do I need such a monstrosity? Well, I need it to define a
> > `Configuration` container, which holds key/value pairs where whoever
> sets a
> > value knows its type, whoever gets the value should ask for the same
> type,
> > and the configuration can hold values of "any" type - not from a
> predefined
> > list of types).
> >
> > Is such a thing possible, and if so, how?
> >
> > Thanks,
> >
> > Oren Ben-Kiki
>
> > _______________________________________________
> > Rust-dev mailing list
> > [email protected]
> > https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to