json-ld icon indicating copy to clipboard operation
json-ld copied to clipboard

Get rid of the `trait_alias` feature.

Open timothee-haudebourg opened this issue 4 years ago • 1 comments

The trait_alias feature seems far from stabilization. It is currently used to combine complicated where bounds on JSON values that are repeated everywhere in the code.

where bounds are not propagated using super traits bounds:

pub trait Foo where bounds { ... }

// where bounds need to be repeated here
// even though it is part of the definition of `Foo`.
fn bar<F: Foo>(t: F) where bounds { ... }

This can be solved using trait aliases:

pub trait Foo = ... where bounds;

// where bounds don't need to be repeated here.
// they are propagated from the trait alias definition.
fn bar<F: Foo>(t: F) { ... }

Using trait alias has proved that the JSON abstraction layer can work. However with the release of version 1.0, it seems not reasonable to keep relying on an unstable feature.

Possible solution

Maybe we can use macros containing the where bounds to replace trait aliases.

macro_rules! foo {
  ($f:ident) => { bounds };
}

fn bar<F>(t: F) where foo!(F) { ... }

The tradeoffs are:

  • This complicates the source code.
  • This may complicate the documentation a lot, by listing a lot of bounds that where previously hidden in trait aliases.

timothee-haudebourg avatar Nov 09 '21 11:11 timothee-haudebourg

Btw, the "possible solution" does not work: macro calls are not allowed as trait bounds.

timothee-haudebourg avatar Mar 23 '22 13:03 timothee-haudebourg

This is fixed by https://github.com/timothee-haudebourg/json-ld/pull/37, right?

bjorn3 avatar Nov 12 '22 12:11 bjorn3

Yes it is, thanks.

timothee-haudebourg avatar Nov 12 '22 12:11 timothee-haudebourg