Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print keyword blocks print as a function #931

Open
hen6003 opened this issue Nov 4, 2024 · 2 comments
Open

Print keyword blocks print as a function #931

hen6003 opened this issue Nov 4, 2024 · 2 comments
Labels

Comments

@hen6003
Copy link

hen6003 commented Nov 4, 2024

Print being a keyword currently stops you using "print" as a function on a type using with_fn

Example code:

#[derive(Clone, CustomType)]
#[rhai_type(extra = Self::build_extra)]
struct Type {}

impl Type {
    fn build_extra(builder: &mut TypeBuilder<Self>) {
        builder
            .with_name("Type")
            .with_fn("print", |s: &str| println!("{}", s));
    }
}

fn main() {
    let mut engine = Engine::new();

    engine.register_type::<Type>();

    let mut scope = Scope::new();

    scope.push("t", Type {});

    println!(
        "{:?}",
        engine.run_with_scope(&mut scope, "t.print(\"hello world!\"")
    );
}

Outputs:

Err(ErrorParsing(Reserved("print"), 1:3))
@schungx
Copy link
Collaborator

schungx commented Nov 5, 2024

That's correct, because print is a keyword. You can easily override it by setting the to_string function instead. print uses to_string to format the output.

@rawhuul
Copy link

rawhuul commented Nov 7, 2024

Hey @hen6003, this might be helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants