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

Help with Default with generics not working. #30

Open
beckend opened this issue Jul 18, 2024 · 3 comments
Open

Help with Default with generics not working. #30

beckend opened this issue Jul 18, 2024 · 3 comments

Comments

@beckend
Copy link

beckend commented Jul 18, 2024

#[derive(Educe, Debug)]
#[educe(Default(bound(T: Default), expression = Self { data: vec![1, 2, 3, 4, 5] }))]
struct Dummy<T> {
  data: T,
}
39 | #[educe(Default(bound(T: Default), expression = Self { data: vec![1, 2, 3, 4, 5] }))]
   |                                                              ^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `Vec<{integer}>`
#[educe(Default)]
#[derive(Educe, Debug)]
struct DummyStructMusliGeneric<T> {
  #[educe(Default = true)]
  data: T,
}
43 |   #[educe(Default = true)]
   |                     ^^^^ the trait `From<bool>` is not implemented for `T`, which is required by `bool: std::convert::Into<_>`
@ijackson
Copy link
Contributor

What do you expect

  #[educe(Default = true)]

to do ?

It looks like it's trying to say that when constructing a default value of DummyStructMusliGeneric<T>, the field data should be initialised to true.

That's fine for DummyStructMusliGeneric<bool> but it's obviously not going to work for DummyStructMusliGeneric<String>.

@linkenquydinhhexagon
Copy link

Ok got it, is there a way to annotate what T would be?

@ijackson
Copy link
Contributor

ijackson commented Jul 31, 2024

Ok got it, is there a way to annotate what T would be?

I'm not sure I understand the question. In the original, you're setting the default to a vec of numbers. Why haven't you written:

struct Dummy {
   data: Vec<i32>,

? (Genuine question: there must be a reason.)

One option would be something like this:

trait HasMySpecialDefault {
   fn my_special_default() -> Self;
}
impl HasMySpecialDefault for Vec<i32> { ... }

#[derive(Educe, Debug)]
#[educe(Default(bound(T: HasMySpecialDefault))]
struct DummyStructMusliGeneric<T> {
  #[educe(Default = <T as HasMySpecialDefault>::my_special_default())]
  data: T,
}

I haven't tried that, the syntax may be wrong, but I hope you get the idea. You'd only be able to get a Dummy::default() if you'd provided the corresponding default value.

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

3 participants