Scala 3 - Type parameterization - How to express a bizarre requirement

Consider these 3 types: MyGeneric[I, O], TypeA and TypeB.
TypeA and TypeB are not related.
If possible at all, how do I express the constraint that if I is TypeA then O is TypeB and if I is a subtype of TypeB then O is a subtype of I?

Let me say that most probably I can, and should, rethink this code.
But I thought it would be a good idea to ask and see if Scala can accomodate such a peculiar requirement and which areas I need to study more.

1 Like

You might find this useful, answers part of your question. (Not sure about the “if-then” part)

2 Likes

I’ll dive into it right away, thank you @spamegg1!

1 Like

I mean, there are a couple of ways to model that relationship:

  • Typeclasses
  • Match types
  • A wrapper ADT
  • Path dependant types

The right solution would depend on your Scala version and if you can explain more about the real problem and meta-problem you are trying to solve.

2 Likes

@BalmungSan I’m using the 3.6.0 nightly from a few days ago, are some of your suggestions more useful with this version, compared to others?

Anyway, thanks for the pointers, I will look into those topics.
I had already tried match types but couldn’t accomplish much with them.
I will try again.

1 Like

Again, it would be easier to help if you could share some code or pseudo code.

Like show a minimized version of what you have and what you want.
You may also add what you tried.

2 Likes

Wrapper ADT is probably the most obvious / easiest solution, I would try to go for path dependent types (since Scala 3 type system is based on it so it’s worth learning). Though it depends on what you want to do…

1 Like