Struct tulisp::TulispObject
source · pub struct TulispObject { /* private fields */ }
Expand description
A type for representing tulisp objects.
Implementations§
source§impl TulispObject
impl TulispObject
sourcepub fn nil() -> TulispObject
pub fn nil() -> TulispObject
Create a new nil
value.
nil
is the False
value in Tulisp. It is also the value of an
empty list. So it is possible to construct lists, by chaining calls to
push
on a nil
value.
Read more about nil
in Emacs Lisp
here.
sourcepub fn t() -> TulispObject
pub fn t() -> TulispObject
Create a new t
value.
Any value that is not nil
is considered True
. t
may be used as a
way to explicitly specify True
.
sourcepub fn cons(car: TulispObject, cdr: TulispObject) -> TulispObject
pub fn cons(car: TulispObject, cdr: TulispObject) -> TulispObject
Make a cons cell with the given car and cdr values.
sourcepub fn equal(&self, other: &TulispObject) -> bool
pub fn equal(&self, other: &TulispObject) -> bool
Returns true if self
and other
have equal values.
Read more about Emacs equality predicates here.
sourcepub fn eq(&self, other: &TulispObject) -> bool
pub fn eq(&self, other: &TulispObject) -> bool
Returns true if self
and other
are the same object.
Read more about Emacs equality predicates here.
sourcepub fn eql(&self, other: &TulispObject) -> bool
pub fn eql(&self, other: &TulispObject) -> bool
Returns true if self
and other
are the same object, or are
indistinguishable numbers.
Read more about Emacs eql
here
sourcepub fn iter<T: TryFrom<TulispObject>>(&self) -> Iter<T> ⓘ
pub fn iter<T: TryFrom<TulispObject>>(&self) -> Iter<T> ⓘ
Returns an iterator over the TryInto
results on the values inside
self
.
§Example
let items = ctx.eval_string("'(10 20 30 40 -5)")?;
let items_vec: Vec<i64> = items
.iter::<i64>()
.map(|x| x.unwrap()) // works because there are only i64 values.
.collect();
assert_eq!(items_vec, vec![10, 20, 30, 40, -5]);
sourcepub fn push(&self, val: TulispObject) -> Result<&TulispObject, Error>
pub fn push(&self, val: TulispObject) -> Result<&TulispObject, Error>
Adds the given value to the end of a list. Returns an Error if self
is
not a list.
sourcepub fn append(&self, other_list: TulispObject) -> Result<&TulispObject, Error>
pub fn append(&self, other_list: TulispObject) -> Result<&TulispObject, Error>
Attaches the other list to the end of self. Returns an Error if self
is not a list.
sourcepub fn fmt_string(&self) -> String
pub fn fmt_string(&self) -> String
Returns a string representation of self
, similar to the Emacs Lisp
function princ
.
sourcepub fn set(&self, to_set: TulispObject) -> Result<(), Error>
pub fn set(&self, to_set: TulispObject) -> Result<(), Error>
Sets a value to self
in the current scope. If there was a previous
value assigned to self
in the current scope, it will be lost.
Returns an Error if self
is not a Symbol
.
sourcepub fn set_scope(&self, to_set: TulispObject) -> Result<(), Error>
pub fn set_scope(&self, to_set: TulispObject) -> Result<(), Error>
Sets a value to self
, in the new scope, such that when it is unset
,
the previous value becomes active again.
Returns an Error if self
is not a Symbol
.
sourcepub fn unset(&self) -> Result<(), Error>
pub fn unset(&self) -> Result<(), Error>
Unsets the value from the most recent scope.
Returns an Error if self
is not a Symbol
.
sourcepub fn get(&self) -> Result<TulispObject, Error>
pub fn get(&self) -> Result<TulispObject, Error>
Gets the value from self
.
Returns an Error if self
is not a Symbol
.
sourcepub fn try_float(&self) -> Result<f64, Error>
pub fn try_float(&self) -> Result<f64, Error>
Returns a float if self
holds a float or an int, and an Error otherwise.
sourcepub fn try_int(&self) -> Result<i64, Error>
pub fn try_int(&self) -> Result<i64, Error>
Returns an int if self
holds a float or an int, and an Error otherwise.
sourcepub fn as_float(&self) -> Result<f64, Error>
pub fn as_float(&self) -> Result<f64, Error>
Returns a float is self
holds a float, and an Error otherwise.
sourcepub fn as_int(&self) -> Result<i64, Error>
pub fn as_int(&self) -> Result<i64, Error>
Returns an int is self
holds an int, and an Error otherwise.
sourcepub fn as_symbol(&self) -> Result<String, Error>
pub fn as_symbol(&self) -> Result<String, Error>
Returns a string containing symbol name, if self
is a symbol, and an Error otherwise.
sourcepub fn as_string(&self) -> Result<String, Error>
pub fn as_string(&self) -> Result<String, Error>
Returns a string if self
contains a string, and an Error otherwise.
sourcepub fn as_any(&self) -> Result<Rc<dyn Any>, Error>
pub fn as_any(&self) -> Result<Rc<dyn Any>, Error>
Returns a boxed value if self
contains a boxed value, and an Error otherwise.
Functions exported to Tulisp can return arbitrary boxed values, which can be extracted
with as_any
, and downcast to desired types.
§Example
let mut ctx = TulispContext::new();
struct TestStruct {
value: i64,
}
#[tulisp_fn(add_func = "ctx")]
fn make_any(inp: i64) -> Rc<dyn Any> {
Rc::new(TestStruct { value: inp })
}
let out = ctx.eval_string("(make_any 25)")?;
let ts = out.as_any()?.downcast::<TestStruct>().unwrap();
assert_eq!(ts.value, 25);
source§impl TulispObject
impl TulispObject
This impl block contains all the car
/cdr
/caar
/cadr
/etc. functions.
In addition to the functions documented below, there are also 8 cxxxr
functions, like caadr
, cdddr
, etc., and 16 cxxxxr
functions, like
caaaar
, cddddr
, etc., which are not documented here, to avoid
repetition.
sourcepub fn car(&self) -> Result<TulispObject, Error>
pub fn car(&self) -> Result<TulispObject, Error>
Returns the car
of self
if it is a list, and an Error otherwise.
sourcepub fn cdr(&self) -> Result<TulispObject, Error>
pub fn cdr(&self) -> Result<TulispObject, Error>
Returns the cdr
of self
if it is a list, and an Error otherwise.
sourcepub fn caar(&self) -> Result<TulispObject, Error>
pub fn caar(&self) -> Result<TulispObject, Error>
Returns the car
of car
of self
if it is a list, and an Error otherwise.
sourcepub fn cadr(&self) -> Result<TulispObject, Error>
pub fn cadr(&self) -> Result<TulispObject, Error>
Returns the car
of cdr
of self
if it is a list, and an Error otherwise.
sourcepub fn cdar(&self) -> Result<TulispObject, Error>
pub fn cdar(&self) -> Result<TulispObject, Error>
Returns the cdr
of car
of self
if it is a list, and an Error otherwise.
sourcepub fn cddr(&self) -> Result<TulispObject, Error>
pub fn cddr(&self) -> Result<TulispObject, Error>
Returns the cdr
of cdr
of self
if it is a list, and an Error otherwise.
sourcepub fn car_and_then<Out: Default>(
&self,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>
) -> Result<Out, Error>
pub fn car_and_then<Out: Default>( &self, f: impl FnOnce(&TulispObject) -> Result<Out, Error> ) -> Result<Out, Error>
Executes the given function on the car
of self
and returns the result.
sourcepub fn cdr_and_then<Out: Default>(
&self,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>
) -> Result<Out, Error>
pub fn cdr_and_then<Out: Default>( &self, f: impl FnOnce(&TulispObject) -> Result<Out, Error> ) -> Result<Out, Error>
Executes the given function on the cdr
of self
and returns the result.
sourcepub fn caar_and_then<Out: Default>(
&self,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>
) -> Result<Out, Error>
pub fn caar_and_then<Out: Default>( &self, f: impl FnOnce(&TulispObject) -> Result<Out, Error> ) -> Result<Out, Error>
Executes the given function on the car
of car
of self
and returns the result.
sourcepub fn cadr_and_then<Out: Default>(
&self,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>
) -> Result<Out, Error>
pub fn cadr_and_then<Out: Default>( &self, f: impl FnOnce(&TulispObject) -> Result<Out, Error> ) -> Result<Out, Error>
Executes the given function on the car
of cdr
of self
and returns the result.
sourcepub fn cdar_and_then<Out: Default>(
&self,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>
) -> Result<Out, Error>
pub fn cdar_and_then<Out: Default>( &self, f: impl FnOnce(&TulispObject) -> Result<Out, Error> ) -> Result<Out, Error>
Executes the given function on the cdr
of car
of self
and returns the result.
sourcepub fn cddr_and_then<Out: Default>(
&self,
f: impl FnOnce(&TulispObject) -> Result<Out, Error>
) -> Result<Out, Error>
pub fn cddr_and_then<Out: Default>( &self, f: impl FnOnce(&TulispObject) -> Result<Out, Error> ) -> Result<Out, Error>
Executes the given function on the cdr
of cdr
of self
and returns the result.
Trait Implementations§
source§impl Clone for TulispObject
impl Clone for TulispObject
source§fn clone(&self) -> TulispObject
fn clone(&self) -> TulispObject
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more