GithubHelp home page GithubHelp logo

Comments (4)

MrGVSV avatar MrGVSV commented on June 26, 2024 1

is it possible in the derive macro to detect the lacking default impl and report on it?

Unfortunately no. Macros don't have any type information available to them, including what traits a type implements.

Opt out of FromReflect by adding #[reflect(from_reflect = false)] to your Bar type

If I was guided towards this in the error msg that would have helped me right away, which would be very nice

Yeah ideally we would do this, but it's not possible to know whether or not a type implements Default via the macro. We'd have to generate this error based on the lack of default attributes when not opting out, which may be controversial compared to just returning None.

from bevy.

MrGVSV avatar MrGVSV commented on June 26, 2024

This is actually expected behavior since FromReflect (which is automatically derived alongside Reflect) requires Default for ignored fields.

There are two possible solutions besides implementing Default for Foo:

  1. Opt out of FromReflect by adding #[reflect(from_reflect = false)] to your Bar type
  2. Add a custom default function for Foo like #[reflect(ignore, default = "create_foo")], #[reflect(ignore, default = "Foo::new")], etc.

Hopefully that helps!

from bevy.

MrGVSV avatar MrGVSV commented on June 26, 2024

As for how we can avoid this in the future, I think we could update the macro code to make FromReflect opt-in. If any field is ignored, we can skip implementing FromReflect unless a default attribute is set for that field. I did something like this in #13432.

One possible issue is that this would mean that users won't be 100% sure their type implements FromReflect unless they try to use it.

from bevy.

torsteingrindvik avatar torsteingrindvik commented on June 26, 2024

As for how we can avoid this in the future, I think we could update the macro code to make FromReflect opt-in. If any field is ignored, we can skip implementing FromReflect unless a default attribute is set for that field. I did something like this in #13432.

One possible issue is that this would mean that users won't be 100% sure their type implements FromReflect unless they try to use it.

If this is a longer term solution, is the near term is it possible in the derive macro to detect the lacking default impl and report on it?

Opt out of FromReflect by adding #[reflect(from_reflect = false)] to your Bar type

If I was guided towards this in the error msg that would have helped me right away, which would be very nice

from bevy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.