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

  • Home
  • SEARCH
  • 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 7980931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:13:15+00:00 2026-06-04T10:13:15+00:00

I want to mixin a trait so that I can use a method from

  • 0

I want to mixin a trait so that I can use a method from to return my own trait type. For example,

> trait M { 
    trait foo {def blah = "foo" }
    def name:foo = { new foo { override def blah = "name"}}}
> trait N extends M { 
    trait bar extends foo {}
    override def name:bar = super.name.asInstanceOf[bar]}
> object t extends N { val baz = name }
> t.name
  java.lang.ClassCastException: M$$anon$1 cannot be cast to N$bar
at N$class.name(<console>:7)
at t$.name(<console>:8)
at t$.<init>(<console>:8)
at t$.<clinit>(<console>)
at .<init>(<console>:10)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.tools.nsc.Interpreter$Request$$anonfun$l...

I know that I’m thinking about this in too much an OO fashion by using the asInstanceOf, and am ignorant in regards to something basic about the way traits work in Scala. How should I change N and its sub-types?

  • 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-06-04T10:13:17+00:00Added an answer on June 4, 2026 at 10:13 am

    M.name constructs and returns a foo. In N.name you call super.name. Since super.name refers to M.name, super.name also constructs and returns a foo. You then take that foo and call .asInstanceOf[bar]. But this doesn’t make sense since nothing in your code ever constructed a bar, and, while a bar is a foo, a foo is not necessarily a bar.

    If you really want N.name to return a bar, then you need to override M.name so that you explicitly construct a bar and return it.

    trait N extends M {
      trait bar extends foo {}
      override def name: bar ={
        val f: foo = super.name                // not what we want; it's not a `bar`
        new bar { override def blah = f.blah } // this is actually a `bar`
      }
    }
    

    Now we get:

    scala> println(t.name.blah)
    name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume I want to create a trait that I can mix in into
Here's an example mixin: =border($alpha: 0.2) 1px solid hsla(0, 0, 0, $alpha) I want
I am having trouble creating a simple mixin that i plan to use on
I have a category class that I want to Use that acts on a
Here's an example of a mixin I'm using: @mixin gradient($from, $to, $height) { background-color:
Why would anyone want to not use a code behind file so that server
I'm thinking about creating a mixin form class so that I can add a
I want to make a function/mixin that will make a color darker if it
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a

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.