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 7637463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:56:46+00:00 2026-05-31T07:56:46+00:00

I have a type that i use that use for ducktyping: type t={ def

  • 0

I have a type that i use that use for ducktyping:

type t={
   def x:Int
 ...
}
class t2 {
 def x:Int=1
}
def myt:t=new t2 //ducktyping

I want to write a trait that is forced to interface the type but this does NOT work:

trait c extends t { //interface DOES NOT COMPILE
  def x:Int=1
}

On the other hand: if i write a trait t1 instead of type t then i lose the ducktyping feature:

trait t1 {
 def x:Int
}
type t=t1
trait c extends t1 { // t1 can be used as interface
  def x:Int=1
}
def myt:t=new t2  // DOES NOT COMPILE since t1 is expected

So how can i use both ducktyping and interfacing?

  • 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-31T07:56:48+00:00Added an answer on May 31, 2026 at 7:56 am

    You can only extend class-like entities in Scala (ie. classes, traits, Java interfaces) not types in general (ie. structural types, type parameters or members). However, you can self-type to all of these. That means that we can rewrite your non-compiling trait c as follows,

    trait c { self : t =>
      def x : Int = 1
    }
    

    Within the body of c the type of this is now known to be t, ie., known to conform to the structural type { def x : Int }, and it will only be possible to mix c into a class which does actually conform to that structural type (either by implementing the signature directly or, if abstract, by reasserting the self-type and propagating the obligation to the eventual concrete class),

    type t = { def x : Int }
    
    trait c { self : t => }
    
    class t2 extends c {  // OK, t2 conforms to t
      def x : Int = 1
    }
    
    def myt : t = new t2  // OK, as before
    
    class t3 extends c {  // Error: t3 doesn't conform to c's self-type
      def y : String = "foo"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that uses enum types. enum Type{a,b,}; class A { //use
I have an interface that will be implemented by client code: class Runner: def
I have 2 separate java services that use a complex type that is a
Do you have a default type that you prefer to use in your dealings
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a simple type that explicitly implemets an Interface. public interface IMessageHeader {
If have a type hierarchy that starts out with a generic type class A<T>
I have a type that I build with TypeBuilder to match an interface, like
I have a class that is of type UITableViewController . This class has a
I have a custom type Type that implement IEquatable(Type). Then I new up two

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.