I was thinking about something like this
#[sql_table]
pub struct TestTable {
pub a: Option<int>,
pub b: String
}
let selector = sql!( select a from TestTable );
let mut iter: SqlRows<Option<int>> = selector.fetch();
let result: Vec<Option<int>> = iter.collect();
I first intended to convert sql! macro to some Iterator<Option<int>>.
However, we don't have typeof(TestTable::a) syntax yet, which means it's
impossible to get the type of "a" column without creating a dummy instance.
So I plan to convert above macro into rough equivalent of below.
pub fn new_selector<T>(_f: fn || -> T) -> SqlSelector<T> {
// selector initialization code.
// _f should not be executed anywhere
}
let selector = {
// This code is unsafe and should not be executed
let not_executed = fn || {
unsafe {
let dummy_instance = TestTable::uninitialized();
dummy_instance.a
}
};
// This automatically type-checks to SqlSelector<Option<int>>
new_selector(not_executed);
};
I'd love to know if there's a better alternative of this hack.
2014-06-05 18:21 GMT+09:00 Huon Wilson <[email protected]>:
> On 05/06/14 19:11, [email protected] wrote:
>
>>
>> I was also planning to add sql!() macro almost exactly same as Chris
>> Morgan suggests. However, you can't directly access type-checking part of
>> rustc in #![phase(syntax)] modules, which means you need some dirty hacks
>> to peroperly type-check such macros.
>>
>
>
> The conventional approach is to expand to something that uses certain
> traits, meaning any external data has to satisfy those traits for the macro
> invocation to work. This technique is used by `println!` and `#[deriving]`,
> for example.
>
> (I don't know if you regard this as a dirty hack or not.)
>
>
> Huon
>
> _______________________________________________
> 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