Skip to content

Possible to have methods with default values? #185

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

Closed
ndarilek opened this issue Aug 28, 2019 · 8 comments · Fixed by #192
Closed

Possible to have methods with default values? #185

ndarilek opened this issue Aug 28, 2019 · 8 comments · Fixed by #192
Labels
feature Adds functionality to the library question

Comments

@ndarilek
Copy link
Contributor

Is it possible to either assign a method parameter a default value, or to check whether it is nil/not provided and assign it one from within my Rust function? I've got this working for methods with specified parameters, but now I'd like to make others optional and assign sensible defaults.

Thanks.

@karroffel
Copy link
Member

karroffel commented Aug 29, 2019

Rust itself does not have default method parameters, but I think it's possible to define those with NativeScript 1.1, I am not completely sure about this though.

These bindings do not yet use NativeScript 1.1 so adding this would probably take some effort.

@karroffel karroffel added feature Adds functionality to the library question labels Aug 29, 2019
@ghost
Copy link

ghost commented Aug 29, 2019

NativeScript 1.1 did add godot_nativescript_set_method_argument_information, but it doesn't allow setting default values.

If you're fine with writing explicit nulls in GDScript, you can implement ToVariant::from_variant for a newtype around Option<YourType>, and then use unwrap_or_else.

You may also have your method take a Dictionary, if you have a lot of optional arguments. It will be more readable in GDScript too.


Maybe it'd be possible to implement a derive macro for ToVariant? We can map structs to Dictionarys, tuples to VariantArrays, and enums to externally tagged Dictionarys like how serde_json does it: { "VariantName": { "field": 42 } }, where all fields implement ToVariant.

Option and Result can get implementations in -core too, since Rust doesn't allow implementing foreign traits on foreign types. Then there'll be no need to manually implement the traits on wrappers and config structs, so we can just write:

#[derive(ToVariant)]
struct MyMethodsArgs {
    foo: Foo,
    bar: Option<Bar>,
    baz: Option<Baz>,
}

#[methods]
impl MyClass {
    #[export]
    fn my_method(&self, args: MyMethodArgs) -> Result<i64, ()> {
        let foo = args.foo;
        let bar = args.bar.unwrap_or_else(|| Bar::default());
        let baz = args.bar.unwrap_or_else(|| Baz::default());
        // ...
        Ok(42)
    }
}

...and in GDScript:

var answer = my_instance.my_method({
    "foo": foo,
    "baz": baz,
})
# externally tagged dictionaries should work well with GDScript's match
match answer:
    {"Ok": var answer}:
        print(answer)
    {"Err"}:
        print("oops")
    _:
        print("the call itself failed")

@karroffel What do you think about this? Are there any pitfalls I might not be aware of?


I think one problem with Option<T> may be a T that can take Nil as a valid value. In that case a null from GDScript would be ambiguous. Maybe we should just decide that Nil is always a None, so we can short-circuit and don't need to call T's from_variant first?

I just realized that from_variant return an Option<Self> anyway, so it can just be:

fn from_variant(v: &Variant) -> Option<Option<T>> { Some(T::from_variant(v)) }

@ndarilek
Copy link
Contributor Author

ndarilek commented Aug 29, 2019 via email

@ndarilek
Copy link
Contributor Author

ndarilek commented Aug 29, 2019 via email

@ghost
Copy link

ghost commented Aug 29, 2019

For now you need to pass null and accept a Variant, if you don't want to implement a ToVariant wrapper. In your method, you can do let value = YourType::from_variant(v).unwrap_or_else(|| produce_default_value()). If the Variant was a Nil the from_variant will fail, and you'll get your default value.

@ndarilek
Copy link
Contributor Author

ndarilek commented Aug 29, 2019 via email

@ndarilek
Copy link
Contributor Author

ndarilek commented Aug 29, 2019 via email

@ghost
Copy link

ghost commented Aug 31, 2019

Reading godot_wrap_method yesterday I just realized that Godot doesn't count the arguments, so we can just implement optional arguments within Rust itself without need for any Godot API support, using macros and Default. PoC: #192

@ghost ghost mentioned this issue Aug 31, 2019
@ghost ghost closed this as completed in #192 Mar 25, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Adds functionality to the library question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants