M
Modular6mo ago
tkeitt

Specialize on trait

As far as I can tell, the following will not compile:
trait T1:
pass

trait T2:
pass

struct S[T: T1]:
pass

struct S[T: T2]:
pass
trait T1:
pass

trait T2:
pass

struct S[T: T1]:
pass

struct S[T: T2]:
pass
Is the intention to allow this sort of type specialization in the future? I have a workaround using static methods for compile-time branching. However, it requires the definition of a trait that is the superset of the two cases and so each has to fulfill functions that the (sub-)trait does not require.
3 Replies
benny
benny6mo ago
conditional conformance is coming down the pipeline and is available in nightly, the way you could kind of achieve this is parameterizing the struct with T as AnyType (or Variant[S1, S2]) then having specific functions require Self[S1] etc look more in #nightly if this seems like the direction you want to go
benny
benny6mo ago
trait A:
...

trait B:
...

struct Generic[T: AnyType]:
fn any(self):
...

fn requires_a(self: Self[A]):
...

fn requires_b(self: Self[B]):
...
trait A:
...

trait B:
...

struct Generic[T: AnyType]:
fn any(self):
...

fn requires_a(self: Self[A]):
...

fn requires_b(self: Self[B]):
...
@tkeitt
tkeitt
tkeittOP6mo ago
Thanks. I found it in the changelog. But your example is clearer.
Want results from more Discord servers?
Add your server