0

Is that possible to create a type class based on type parameter by inheriting type parameter like in the code below?

I do suspect that this is fundamentally not possible as this is a scala limitation as functional language...

But if possible what I have to change in the code below??

class MyInt[T : Numeric]  extends T  {

}

1 Answer 1

1

It's not possible. You can extend a class (traits included), but you cannot extend a type, and T is a type.

Even if it were possible, what would it then mean to have MyInt[Int] (which is final), or MyInt[List[String]] (which is not a class) ? It would get quite complicated for the compiler very quickly.

Sign up to request clarification or add additional context in comments.

4 Comments

So this is a language limitation and I would assume general limitation for functional programming ???
It's a language limitation. It doesn't have anything to do with functional programming. I don't know of any programming language that can do exactly what you are asking.
@PavelOliynyk The intersection of FP and subtyping is small and weird—in most functional languages the question wouldn't even make sense. :)
well, just checked. It looks like this is not possible in other languages as well. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.