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

How to check return type of script before trying to convert it? #939

Open
Boscop opened this issue Oct 24, 2022 · 1 comment
Open

How to check return type of script before trying to convert it? #939

Boscop opened this issue Oct 24, 2022 · 1 comment

Comments

@Boscop
Copy link

Boscop commented Oct 24, 2022

In my use case, after evaluating a script, the final value can be one of several types that I want to support.
E.g. I want to support 5 different custom types that the script can produce.
The final value of the script should not be an enum though!
So after evaluating a script, how can I check which gluon type the final value is, before I try to convert it to the equivalent Rust type? :)
Or can I evaluate a script and then try converting the final value to a Rust type, to check if it succeeds?

@Marwes
Copy link
Member

Marwes commented Oct 25, 2022

https://github.com/gluon-lang/gluon/blob/master/examples/marshalling.rs contains several examples though none exactly matches what you want.

You should be able to achieve it by using OpaqueValue<_, Hole> as the type on the rust side which will not put any type constraints on the gluon side. Then you can dereference that to a Value which you can extract via from_value or via pattern matching.

let (value, typ) = thread.run_expr::<OpaqueValue<RootedThread, Hole>()?;
// or match on `value`, whatever works for your typechecking into the 5 types you want to support
match typ {
}

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

No branches or pull requests

2 participants