That would require me to declare up front which types were usable and which
weren't. I'm looking for a solution where I don't need to do that; that is,
allow me to add new components to the system with new configuration
parameter types, without having to go to a central source location and
declare these types there.


On Fri, Aug 23, 2013 at 7:54 AM, Abhijeet Gaiha <[email protected]>wrote:

> You could define an enum that encapsulates all known types.
>
> enum monster {
>      Integer(int),
>      Float(float),
> ....
> }
>
> Then use a container for this type.
> On Aug 23, 2013 10:20 AM, "Oren Ben-Kiki" <[email protected]> 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