Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:59:48+00:00 2026-05-25T21:59:48+00:00

Given a generic interface such as the following interface I<T> { void m(T t);

  • 0

Given a generic interface such as the following

interface I<T> {
    void m(T t);
}

I can in C# create a class that implements I twice (or more) with different types supplied for T, e.g.

class C : I<int>, I<String> {
   public void m(int i) { }
   public void m(String s) { }
}

This cannot be done in Java due to erasure of the generic type info, but can something like this be achieved in Scala?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:59:48+00:00Added an answer on May 25, 2026 at 9:59 pm

    No. Mixing in the same trait is only possible in Scala if the 2 types with which the trait (interface) is parametrized with types that conform to each other and the trait is not mixed into the same class twice directly. To ensure that the 2 types conform to each other, you will generally have to make the type parameter covariant (+).

    For example, this is not allowed:

    scala> trait A[+T] { def foo: T = sys.error() }
    defined trait A
    
    scala> class C extends A[AnyRef] with A[String]
    <console>:8: error: trait A is inherited twice
           class C extends A[AnyRef] with A[String]
    

    But this is:

    scala> trait A[+T] { def foo: T = sys.error() }
    defined trait A
    
    scala> class C extends A[AnyRef]
    defined class C
    
    scala> class B extends C with A[String]
    defined class B
    

    Note that in this case you will not obtain the overloading semantics as is the case with C#, but the overriding semantics – all the methods in A with the conforming signature will be fused in one method with the most specific signature, choosing the method according to linearization rules, rather than having one method for each time you’ve mixed the trait in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C#, I can implement a generic interface twice on one class, using two
Given the following code: using System.Collections.Generic; static class Program { static void Main() {
Given the following generic interface and implementing class: public interface IRepository<T> { // U
In C#, given a generic type such as this: interface IGenericType<T> where T :
How can I specify that I want any object that implements the A interface
Given a Generic List of objects that contain a member variable that is a
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given the following interfaces/classes: public interface IRequest<TResponse> { } public interface IHandler<TRequest, TResponse> where
I am trying to create a generic extension that uses 'TryParse' to check if
I have the following generic question Here is a my generic message interface. public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.