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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:05:15+00:00 2026-05-30T22:05:15+00:00

Given this simple code snippet I am astounded to provoke a stack overflow this

  • 0

Given this simple code snippet I am astounded to provoke a stack overflow this easy:

implicit def foobar: Unit = implicitly[Unit]

In my little more complex use case I have the following situtation:

abstract class Foo {
  type Repr_Tpe
  protected implicit def repr2Ordered: Repr_Tpe => Ordered[Repr_Tpe]
}

class Bar extends Foo {
  type Repr_Tpe = Long
  protected implicit def repr2Ordered = implicitly[Repr_Tpe => Ordered[Repr_Tpe]]
}

Defining method repr2Ordered in class Foo does not work because type Repr_Tpe is abstract. So I decided to copy & paste the declaration and make a definition out of it; apparently leading to the stack overflow from above. Only by removing the modifier implicit from the definition in class Bar solves this problem.

Isn’t there an elegant solution circumventing this pitfall?

  • 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-30T22:05:16+00:00Added an answer on May 30, 2026 at 10:05 pm

    You’ve defined foobar to be the implicit value of type Unit. Then you’ve defined it as the implicit value of type Unit. Thinking of it this way:

    implicit def foobar: Unit = implicitly[Unit]
    // you've defined foobar as the implicit value for Unit.
    // so implicitly[Unit] is the same as calling foobar
    // which is the same as:
    implicit def foobar: Unit = foobar
    

    You should be no more surprised that this causes a stack overflow than you would be by this statement:

    def tharSheBlows: Unit = tharSheBlows
    

    For something with a bit more elegance, I would use a view bound to ensure that the type paramater is Ordered instead.

    scala> abstract class Foo[Repr_Tpe <% Ordered[Repr_Tpe]] {}
    defined class Foo
    
    scala> class Bar extends Foo[Long] {}
    defined class Bar
    
    scala> case class Unordered(data: String)
    defined class Unordered
    
    scala> class Bam extends Foo[Unordered] {}
    <console>:10: error: No implicit view available from Unordered => Ordered[Unordered].
           class Bam extends Foo[Unordered] {}
                     ^
    
    scala> implicit def bringOrder(u: Unordered) = new Ordered[Unordered] { def compare(that: Unordered) = u.data.compareTo(that.data) }
    bringOrder: (u: Unordered)java.lang.Object with Ordered[Unordered]
    
    scala> class Bam extends Foo[Unordered] {}
    defined class Bam
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simple C++ constructor concept I'm having trouble with. Given this code
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
This very simple code gives me tons of errors: #include <iostream> #include <string> int
This is probably pretty simple. I want to select all elements of a given
This should be a simple one, but I'm a beginner with C#. Given a
This is a purely theoretical question. Given three simple classes: class Base { }
I have this snippet from apple sample code LazyTableImages . In the code below
I found a snippet similar to this in some (C++) code I'm preparing for
Recently i have attended an interview . A code snippet is given to me.I
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath

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.