Case class (with companion object) in method declaration evaluated as Any












1















I have an issue with a case class + companion object and (possibly) its interaction with Play. Whenever i try to pass it as an argument for a method, it is interpreted as being of type any instead of EventList. Using it in a method body works perfectly, however.



I can't seem to understand why. Below is a simplified segment of the code in question (it comes from a large code base).



EventList.scala:



package v1.objects

final case class EventList( ... ) {
...
}

object EventList {
def apply( ... ): EventList = {
...
new EventList( ... )
}
}


ObjectRepository.scala:



package v1.objects

class ObjectExecutionContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")

trait ObjectRepository {
def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int]
...
}

@Singleton
class ObjectRepositoryImpl @Inject()()(implicit ec: ObjectExecutionContext) extends ObjectRepository {
override def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int] = {
...
var eventList = EventList(...) // Retreive the old events from DB, works fine
eventList = EventList(eventList.underlying :+ newEvents.underlying) // <- This fails! newEvents has type Any
...
}
}


The error message on compilation:



Error: overloaded method value apply with alternatives:
(underlying: List[v1.objects.Event])v1.objects.EventList
<and>
(doc: org.mongodb.scala.Document)v1.objects.EventList
cannot be applied to (String, List[Object])
eventList = EventList(eventList.underlying :+ newEvents.underlying)









share|improve this question























  • You should include the full declarations of the case class arguments and the apply method arguments, without that it is quite hard to guess why it does not work.

    – Gábor Bakos
    Nov 26 '18 at 11:52






  • 4





    :+ adds one element to a list. You probably intended to use ++ to combine two lists together. Other than that, more info is needed.

    – Oleg Pyzhcov
    Nov 26 '18 at 12:01











  • The case class arguments and the apply method arguments all work as expected, and they work fine in the method bodies. The problem is that the compiler ignores the type in the method declaration and treats it as Any. Thanks Oleg, I simplified a for loop and forgot to make that change :)

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20











  • If I include the full declarations then I break non-disclosure clauses on my contract. I've heavily modified this example code to cover the gist of the problem without revealing anything about the actual problem, but I can try to construct a minimal breaking example and upload it to github. I was hoping that it perhaps was some fundamental flaw in my usage of case classes with object companions and their interaction with singletons in play (or perhaps in method arguments in general) that would easily be identified by someone with more knowledge on Scala/Play.

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20


















1















I have an issue with a case class + companion object and (possibly) its interaction with Play. Whenever i try to pass it as an argument for a method, it is interpreted as being of type any instead of EventList. Using it in a method body works perfectly, however.



I can't seem to understand why. Below is a simplified segment of the code in question (it comes from a large code base).



EventList.scala:



package v1.objects

final case class EventList( ... ) {
...
}

object EventList {
def apply( ... ): EventList = {
...
new EventList( ... )
}
}


ObjectRepository.scala:



package v1.objects

class ObjectExecutionContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")

trait ObjectRepository {
def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int]
...
}

@Singleton
class ObjectRepositoryImpl @Inject()()(implicit ec: ObjectExecutionContext) extends ObjectRepository {
override def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int] = {
...
var eventList = EventList(...) // Retreive the old events from DB, works fine
eventList = EventList(eventList.underlying :+ newEvents.underlying) // <- This fails! newEvents has type Any
...
}
}


The error message on compilation:



Error: overloaded method value apply with alternatives:
(underlying: List[v1.objects.Event])v1.objects.EventList
<and>
(doc: org.mongodb.scala.Document)v1.objects.EventList
cannot be applied to (String, List[Object])
eventList = EventList(eventList.underlying :+ newEvents.underlying)









share|improve this question























  • You should include the full declarations of the case class arguments and the apply method arguments, without that it is quite hard to guess why it does not work.

    – Gábor Bakos
    Nov 26 '18 at 11:52






  • 4





    :+ adds one element to a list. You probably intended to use ++ to combine two lists together. Other than that, more info is needed.

    – Oleg Pyzhcov
    Nov 26 '18 at 12:01











  • The case class arguments and the apply method arguments all work as expected, and they work fine in the method bodies. The problem is that the compiler ignores the type in the method declaration and treats it as Any. Thanks Oleg, I simplified a for loop and forgot to make that change :)

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20











  • If I include the full declarations then I break non-disclosure clauses on my contract. I've heavily modified this example code to cover the gist of the problem without revealing anything about the actual problem, but I can try to construct a minimal breaking example and upload it to github. I was hoping that it perhaps was some fundamental flaw in my usage of case classes with object companions and their interaction with singletons in play (or perhaps in method arguments in general) that would easily be identified by someone with more knowledge on Scala/Play.

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20
















1












1








1








I have an issue with a case class + companion object and (possibly) its interaction with Play. Whenever i try to pass it as an argument for a method, it is interpreted as being of type any instead of EventList. Using it in a method body works perfectly, however.



I can't seem to understand why. Below is a simplified segment of the code in question (it comes from a large code base).



EventList.scala:



package v1.objects

final case class EventList( ... ) {
...
}

object EventList {
def apply( ... ): EventList = {
...
new EventList( ... )
}
}


ObjectRepository.scala:



package v1.objects

class ObjectExecutionContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")

trait ObjectRepository {
def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int]
...
}

@Singleton
class ObjectRepositoryImpl @Inject()()(implicit ec: ObjectExecutionContext) extends ObjectRepository {
override def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int] = {
...
var eventList = EventList(...) // Retreive the old events from DB, works fine
eventList = EventList(eventList.underlying :+ newEvents.underlying) // <- This fails! newEvents has type Any
...
}
}


The error message on compilation:



Error: overloaded method value apply with alternatives:
(underlying: List[v1.objects.Event])v1.objects.EventList
<and>
(doc: org.mongodb.scala.Document)v1.objects.EventList
cannot be applied to (String, List[Object])
eventList = EventList(eventList.underlying :+ newEvents.underlying)









share|improve this question














I have an issue with a case class + companion object and (possibly) its interaction with Play. Whenever i try to pass it as an argument for a method, it is interpreted as being of type any instead of EventList. Using it in a method body works perfectly, however.



I can't seem to understand why. Below is a simplified segment of the code in question (it comes from a large code base).



EventList.scala:



package v1.objects

final case class EventList( ... ) {
...
}

object EventList {
def apply( ... ): EventList = {
...
new EventList( ... )
}
}


ObjectRepository.scala:



package v1.objects

class ObjectExecutionContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")

trait ObjectRepository {
def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int]
...
}

@Singleton
class ObjectRepositoryImpl @Inject()()(implicit ec: ObjectExecutionContext) extends ObjectRepository {
override def patch(newEvents: EventList)(implicit mc: MarkerContext): Future[Int] = {
...
var eventList = EventList(...) // Retreive the old events from DB, works fine
eventList = EventList(eventList.underlying :+ newEvents.underlying) // <- This fails! newEvents has type Any
...
}
}


The error message on compilation:



Error: overloaded method value apply with alternatives:
(underlying: List[v1.objects.Event])v1.objects.EventList
<and>
(doc: org.mongodb.scala.Document)v1.objects.EventList
cannot be applied to (String, List[Object])
eventList = EventList(eventList.underlying :+ newEvents.underlying)






scala playframework






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 11:36









Kristian Rafael AlvarezKristian Rafael Alvarez

274




274













  • You should include the full declarations of the case class arguments and the apply method arguments, without that it is quite hard to guess why it does not work.

    – Gábor Bakos
    Nov 26 '18 at 11:52






  • 4





    :+ adds one element to a list. You probably intended to use ++ to combine two lists together. Other than that, more info is needed.

    – Oleg Pyzhcov
    Nov 26 '18 at 12:01











  • The case class arguments and the apply method arguments all work as expected, and they work fine in the method bodies. The problem is that the compiler ignores the type in the method declaration and treats it as Any. Thanks Oleg, I simplified a for loop and forgot to make that change :)

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20











  • If I include the full declarations then I break non-disclosure clauses on my contract. I've heavily modified this example code to cover the gist of the problem without revealing anything about the actual problem, but I can try to construct a minimal breaking example and upload it to github. I was hoping that it perhaps was some fundamental flaw in my usage of case classes with object companions and their interaction with singletons in play (or perhaps in method arguments in general) that would easily be identified by someone with more knowledge on Scala/Play.

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20





















  • You should include the full declarations of the case class arguments and the apply method arguments, without that it is quite hard to guess why it does not work.

    – Gábor Bakos
    Nov 26 '18 at 11:52






  • 4





    :+ adds one element to a list. You probably intended to use ++ to combine two lists together. Other than that, more info is needed.

    – Oleg Pyzhcov
    Nov 26 '18 at 12:01











  • The case class arguments and the apply method arguments all work as expected, and they work fine in the method bodies. The problem is that the compiler ignores the type in the method declaration and treats it as Any. Thanks Oleg, I simplified a for loop and forgot to make that change :)

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20











  • If I include the full declarations then I break non-disclosure clauses on my contract. I've heavily modified this example code to cover the gist of the problem without revealing anything about the actual problem, but I can try to construct a minimal breaking example and upload it to github. I was hoping that it perhaps was some fundamental flaw in my usage of case classes with object companions and their interaction with singletons in play (or perhaps in method arguments in general) that would easily be identified by someone with more knowledge on Scala/Play.

    – Kristian Rafael Alvarez
    Nov 26 '18 at 12:20



















You should include the full declarations of the case class arguments and the apply method arguments, without that it is quite hard to guess why it does not work.

– Gábor Bakos
Nov 26 '18 at 11:52





You should include the full declarations of the case class arguments and the apply method arguments, without that it is quite hard to guess why it does not work.

– Gábor Bakos
Nov 26 '18 at 11:52




4




4





:+ adds one element to a list. You probably intended to use ++ to combine two lists together. Other than that, more info is needed.

– Oleg Pyzhcov
Nov 26 '18 at 12:01





:+ adds one element to a list. You probably intended to use ++ to combine two lists together. Other than that, more info is needed.

– Oleg Pyzhcov
Nov 26 '18 at 12:01













The case class arguments and the apply method arguments all work as expected, and they work fine in the method bodies. The problem is that the compiler ignores the type in the method declaration and treats it as Any. Thanks Oleg, I simplified a for loop and forgot to make that change :)

– Kristian Rafael Alvarez
Nov 26 '18 at 12:20





The case class arguments and the apply method arguments all work as expected, and they work fine in the method bodies. The problem is that the compiler ignores the type in the method declaration and treats it as Any. Thanks Oleg, I simplified a for loop and forgot to make that change :)

– Kristian Rafael Alvarez
Nov 26 '18 at 12:20













If I include the full declarations then I break non-disclosure clauses on my contract. I've heavily modified this example code to cover the gist of the problem without revealing anything about the actual problem, but I can try to construct a minimal breaking example and upload it to github. I was hoping that it perhaps was some fundamental flaw in my usage of case classes with object companions and their interaction with singletons in play (or perhaps in method arguments in general) that would easily be identified by someone with more knowledge on Scala/Play.

– Kristian Rafael Alvarez
Nov 26 '18 at 12:20







If I include the full declarations then I break non-disclosure clauses on my contract. I've heavily modified this example code to cover the gist of the problem without revealing anything about the actual problem, but I can try to construct a minimal breaking example and upload it to github. I was hoping that it perhaps was some fundamental flaw in my usage of case classes with object companions and their interaction with singletons in play (or perhaps in method arguments in general) that would easily be identified by someone with more knowledge on Scala/Play.

– Kristian Rafael Alvarez
Nov 26 '18 at 12:20














1 Answer
1






active

oldest

votes


















5














This creates a List of Any:



eventList.underlying :+ newEvents.underlying


This adds a List as an Element to the existing List.



And the common Super-Type is then Any.



What you need is the function that adds a List to another List > this returns a List of their contents:



eventList.underlying ++ newEvents.underlying


The exact syntax depends on the underlying type.



Example:



case class EventList(underlying: Seq[String]) 

val el1 = EventList(Seq("e1", "e2"))
val el2 = EventList(Seq("e4", "e5"))

println(el1.underlying :+ el2.underlying) // List(e1, e2, List(e4, e5))

println(el1.underlying ++ el2.underlying) // List(e1, e2, e4, e5)





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480277%2fcase-class-with-companion-object-in-method-declaration-evaluated-as-any%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    This creates a List of Any:



    eventList.underlying :+ newEvents.underlying


    This adds a List as an Element to the existing List.



    And the common Super-Type is then Any.



    What you need is the function that adds a List to another List > this returns a List of their contents:



    eventList.underlying ++ newEvents.underlying


    The exact syntax depends on the underlying type.



    Example:



    case class EventList(underlying: Seq[String]) 

    val el1 = EventList(Seq("e1", "e2"))
    val el2 = EventList(Seq("e4", "e5"))

    println(el1.underlying :+ el2.underlying) // List(e1, e2, List(e4, e5))

    println(el1.underlying ++ el2.underlying) // List(e1, e2, e4, e5)





    share|improve this answer






























      5














      This creates a List of Any:



      eventList.underlying :+ newEvents.underlying


      This adds a List as an Element to the existing List.



      And the common Super-Type is then Any.



      What you need is the function that adds a List to another List > this returns a List of their contents:



      eventList.underlying ++ newEvents.underlying


      The exact syntax depends on the underlying type.



      Example:



      case class EventList(underlying: Seq[String]) 

      val el1 = EventList(Seq("e1", "e2"))
      val el2 = EventList(Seq("e4", "e5"))

      println(el1.underlying :+ el2.underlying) // List(e1, e2, List(e4, e5))

      println(el1.underlying ++ el2.underlying) // List(e1, e2, e4, e5)





      share|improve this answer




























        5












        5








        5







        This creates a List of Any:



        eventList.underlying :+ newEvents.underlying


        This adds a List as an Element to the existing List.



        And the common Super-Type is then Any.



        What you need is the function that adds a List to another List > this returns a List of their contents:



        eventList.underlying ++ newEvents.underlying


        The exact syntax depends on the underlying type.



        Example:



        case class EventList(underlying: Seq[String]) 

        val el1 = EventList(Seq("e1", "e2"))
        val el2 = EventList(Seq("e4", "e5"))

        println(el1.underlying :+ el2.underlying) // List(e1, e2, List(e4, e5))

        println(el1.underlying ++ el2.underlying) // List(e1, e2, e4, e5)





        share|improve this answer















        This creates a List of Any:



        eventList.underlying :+ newEvents.underlying


        This adds a List as an Element to the existing List.



        And the common Super-Type is then Any.



        What you need is the function that adds a List to another List > this returns a List of their contents:



        eventList.underlying ++ newEvents.underlying


        The exact syntax depends on the underlying type.



        Example:



        case class EventList(underlying: Seq[String]) 

        val el1 = EventList(Seq("e1", "e2"))
        val el2 = EventList(Seq("e4", "e5"))

        println(el1.underlying :+ el2.underlying) // List(e1, e2, List(e4, e5))

        println(el1.underlying ++ el2.underlying) // List(e1, e2, e4, e5)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 12:23

























        answered Nov 26 '18 at 12:16









        pmepme

        3,28211830




        3,28211830
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480277%2fcase-class-with-companion-object-in-method-declaration-evaluated-as-any%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin