Could someone explain and provide real world examples of using with keyword in defining types?
Let’s define type
type T = A with B
What does it mean?
When should it be used?
How to instantiate type T?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I guess it’s called type conjunction.
You can use it to enforce that a certain type must extend all of the specified traits / classes. A stupid example:
As for “how to instantiate it”: don’t think of it that way.
typecreates type aliases / synonyms / functions, which do not necessarily represent concrete instantiable types.Edit:
If you’re familiar with Java,
withas used above is similar to Java’s&.However unlike Java’s
&,withgives you a proper type. The first definition offooI wrote cannot be translated to Java. Also you cannot do the following with Java’s&.