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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:35:56+00:00 2026-06-12T10:35:56+00:00

trait DAOContract[T <: Entity] { // default create def create(t: T): Option[Int] } trait

  • 0
trait DAOContract[T <: Entity] {
  // default create
  def create(t: T): Option[Int]
}

trait UserContract extends DAOContract[User] {
  // provide alternate create method for transactional blocks
  def create(u: User)(implicit ss: Session): Either[String, Int]
  ...
}

// DAO provides create implementation with embedded session
class UserDAO(implicit val db: Connection) 
  extends DAO[User] with UserContract {

  import db.Driver.Implicit._
  import org.scalaquery.ql._
  ...
}

in a controller

dao.create(model) // boom, no implicit session

I hope I am missing something here: why is the scala compiler unable to differentiate between the 2 create method signatures above?

Basically makes it impossible for me to overload DAO operations without coming up with a different method naming convention for operations that are transactional (i.e. return Either) or standalone.

Or, I’m just going about things in the wrong way, entirely possible…

  • 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-12T10:35:58+00:00Added an answer on June 12, 2026 at 10:35 am

    There are reasons why Scala prohibits that:

    1. Imagine that your create methods would be:

      def create(t: User): Function[Int,Something]
      def create(u: User)(implicit ss: Session): Either[String, Int]
      

      Then calling create(x)(y) would be applicable to both of them.

    2. Even though the problem in (1.) could be perhaps resolved by if Scala compiler carefully examined of the types of the functions, it’d be very error prone. In such setup it would be very easy for programmers to make mistakes. Forcing different names for the functions ensures that programmers always know what they call.


    If change your methods to have the same return type and if you’re willing to take the risky way, you could try something like this:

    trait Broken {
      final def foo(x: Int)(implicit session: String = null): Option[Int] =
        if (session == null) foo1(x)
        else foo2(x, session);
    
      def foo1(x: Int): Option[Int]
      def foo2(x: Int, session: String): Option[Int]
    }
    class Example extends Broken {
      def foo2(x: Int, y: String) = Some(2)
      def foo1(x: Int) = Some(1)
    }
    
    object Test extends App {
      def withImplicit(e: Example) = {
        implicit val imp = "hey"
        e.foo(1) // (imp)
      }
      def withoutImplicit(e: Example) = e.foo(1)
    
      println(withImplicit(new Example()));
      println(withoutImplicit(new Example()));
    }
    

    When an implicit value is available, the corresponding method is called. Otherwise, the method without the “session” argument is called. But I strongly discourage this approach. Just a slight mistake will lead to calling the wrong variant and this will be very hard to debug.

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

Sidebar

Related Questions

Why if I have: trait T { def method(a: Int) } class A extends
Given a trait T trait T { def v: Int def +(t: T): T
Please consider this code: trait A { def a : Int } def f
The trait Map[A,+B] has a method def minBy [B] (f: ((A, B)) ⇒ B)(implicit
Assuming trait A { def t : Int } trait B { this: A
Consider these two traits: trait Poked extends Actor { override def receive = {
Consider the following code: object foo { trait Bar[Q[_]] implicit object OptionBar extends Bar[Option]
I have a trait and an implementation looking like: trait Foo[A] { def bar[B
Assume, we have: class B class A extends B trait T Then it holds:
Say I have a trait with a property a : trait TheTrait { def

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.