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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:37:48+00:00 2026-06-15T12:37:48+00:00

Say we have the following two traits: trait Foo[A] { def howMany(xs: List[A]) =

  • 0

Say we have the following two traits:

trait Foo[A] { def howMany(xs: List[A]) = xs.size }
trait Bar

And an implicit conversion from the second to the first:

implicit def bar2foo[A](bar: Bar) = new Foo[A] {}

We create a Bar and a list of integers:

val bar = new Bar {}
val stuff = List(1, 2, 3)

Now I’d expect the following to work:

bar howMany stuff

But it doesn’t:

scala> bar howMany stuff
<console>:13: error: type mismatch;
 found   : List[Int]
 required: List[A]
              bar howMany stuff
                          ^

So we go to the spec, which has this to say (emphasis in bold is mine):

Views are applied in three situations.

  1. [Isn’t relevant here.]

  2. In a selection e.m with e of type T, if the selector m does not denote a member of T. In this case, a view v is searched which
    is applicable to e and whose result contains a member named m. The
    search proceeds as in the case of implicit parameters, where the
    implicit scope is the one of T. If such a view is found, the
    selection e.m is converted to v(e).m.

  3. In a selection e.m(args) with e of type T, if the selector m denotes some member(s) of T, but none of these members is applicable to the arguments args. In this case a view v is searched
    which is applicable to e and whose result contains a method m
    which is applicable to args. The search proceeds as in the case of
    implicit parameters, where the implicit scope is the one of T. If
    such a view is found, the selection e.m is converted to
    v(e).m(args).

So we try the following, thinking it must be too absurd to work:

trait Foo[A] { def howMany(xs: List[A]) = xs.size }
trait Bar { def howMany = throw new Exception("I don't do anything!") }

implicit def bar2foo[A](bar: Bar) = new Foo[A] {}

val bar = new Bar {}
val stuff = List(1, 2, 3)

But it does (on both 2.9.2 and 2.10.0-RC2, at least):

scala> bar howMany stuff
res0: Int = 3

This leads to some really strange behavior, as for example in this workaround for this problem.

I have three (closely related) questions:

  1. Is there a straightforward way (i.e., one that doesn’t involve adding fake methods with the appropriate name) to have the view applied correctly in the original case above?
  2. Can someone provide a reading of the spec that accounts for this behavior?
  3. Assuming this is the intended behavior, does it make any sense at all?

I’d also appreciate any links to previous discussions of this issue—I haven’t been having much luck with Google.

  • 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-15T12:37:49+00:00Added an answer on June 15, 2026 at 12:37 pm

    For everyone’s reference, this could only be a bug. The way you know that is the error message:

    <console>:13: error: type mismatch;
     found   : List[Int]
     required: List[A]
    

    List[A] is not a real type – it is List applied to its own type parameter. That is not a type which can be required since it is not a type which can be expressed.

    [Edit – it’s too early, who knows what I’m talking about. Ignore the above, but you can still follow the link.]

    The relevant ticket for this is https://issues.scala-lang.org/browse/SI-6472 .

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

Sidebar

Related Questions

Let's say I have the following xml: <one> <two> <three> <three> foo </three> <four
Lets say I have the following two PHP arrays that contain integers: $foo =
Say I have the following: class Parent(object): [...] def func(self, list): for item in
Let's say, I have the following two classes: class A(object): def __init__(self, i): self.i
Say I have the following two functions: 1 int * foo() { int b=8;
Say I have the following two Bash scripts: Version #1: #!/bin/bash function bar {
Lets say I have the following two branches and the local branch is 'mybranch'
For example, let's say I have the following two tables: Table1: Id ---- 1
Say for example, I have the following two tables: TableA { _id } TableB
Say i have the following two classes: public class User { public int ID

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.