1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::fmt::{Debug, Formatter, Error};

use ::abc::{EvalResult};
use ::incrust::{Context, Incrust};

use super::abc::*;


pub struct Function {
    pub f: fn(&[BType], &Context) -> EvalResult,
}

impl Function {
    pub fn new<'a>(f: fn(&[BType], &Context) -> EvalResult) -> BType<'a> {
        box Function { f: f }
    }
}

impl Clone for Function {
    fn clone(&self) -> Self {
        Function { f: self.f }
    }
}

impl Debug for Function {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
        fmt.write_str("Anonymous Function")
    }
}

impl Type for Function {
    fn iclone<'a>(&self) -> BType<'a> {
        box self.clone()
    }
}

impl <'a> Into<BType<'a>> for Function {
    fn into(self) -> BType<'a> {
        box self
    }
}

impl <'b> AsInvocable for Function {
    fn try_as_invocable(&self) -> Option<&IInvocable> {
        Some(self)
    }

    fn is_invocable(&self) -> bool {
        true
    }
}

impl <'a, 'b: 'a> IInvocable<'a> for Function {
    fn invoke(&self, args: &[BType], context: &Context) -> EvalResult {
        (self.f)(args, context)
    }
}



// --------------------------------------------------------------------------------------------------------------------


//impl Type for (fn(&[BType], &Context) -> EvalResult) {
//    fn iclone<'a>(&self) -> BType<'a> { Box::new(self.clone()) }
//    fn to_bool(&self) -> bool { true }
//}
//
//impl <'a, 'b: 'a> IInvocable<'a> for (fn(&[BType], &Context) -> EvalResult) {
//    fn invoke(&self, args: &[BType], context: &Context) -> EvalResult {
//        self(args, context, env)
//    }
//}