facebook / starlark-rust

A Rust implementation of the Starlark language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how do user defined types interact with typechecking?

johnynek opened this issue · comments

Thanks for this great library and the documentation.

I have been working on some example configuration tooling based on this library and I would like to have typechecking working for my particular dialect of starlark.

Taking as an example your complex type here:

https://docs.rs/starlark/latest/starlark/#defining-rust-objects-that-are-used-from-starlark

I tried to implement something similar to Label in the standard bazel starlark:

#[derive(Debug, PartialEq, Eq, ProvidesStaticType, Allocative)]
struct Label { path: String, name: String }
starlark_simple_value!(Label);

impl Label {
  pub fn string_repr(&self) -> String {
    format!("{:}:{:}", self.path, self.name)
  }
}

#[starlark_value(type = "Label")]
impl<'v> StarlarkValue<'v> for Label {

I would have thought that type = Label would have allowed me to use Label in typechecking:

foo: Label = ...

but I get an error like: error: Variable Label not found

Is there something else I need to do make that type name available? If I remove the type ascription then everything I am doing works, so I'm wiring up my functions correctly.

Rather than debug my particular example, another possible solution to this issue would be to make your complex example work with typechecking so that your newly introduced type could be referenced in type annotations.

Thank you!

Is there something else I need to do make that type name available?

Use StarlarkValueAsType to define global with type symbol.

Overall, typechecking documentation is heavily outdated. For example, if you start using this symbol in function signatures, it will be runtime typechecking, but static typechecking is turned off by default.

Thank you for the reply.

After your comment I found this code as a good example to learn from:

const Input: StarlarkValueAsType<InputTypeRepr> = StarlarkValueAsType::new();

Thanks again. I'm unblocked.