logo
down
shadow

scala case class put methods in companion object?


scala case class put methods in companion object?

By : shuang
Date : November 17 2020, 11:52 AM
it fixes the issue I would prefer to put methods in case class. Putting it in companion object sounds like Anemic Domain Model anti-pattern AnemicDomainModel.
Moreover you can override case class methods later or extend and mix some traits.
code :


Share : facebook icon twitter icon
Why can't I access private class methods in the class's companion object in Scala?

Why can't I access private class methods in the class's companion object in Scala?


By : user2410261
Date : March 29 2020, 07:55 AM
should help you out The problem is that classes and companion objects can't be nested like that. To define a companion object, you need to define the class outside of the object's body but in the same file.
Companion object in Scala isn't associating itself with case class

Companion object in Scala isn't associating itself with case class


By : user3165620
Date : March 29 2020, 07:55 AM
it fixes the issue I'm having some trouble understanding why this code won't work. I got it from 99 Scala Problems in the Binary Trees section (http://aperiodic.net/phil/scala/s-99/). It looks valid to me: the Node object is a companion object to the Node class, and it's adding a constructor for leafs on the tree. But when I try compiling it, I get the following: , Works for me (see below). Have you defined them in the same file?
code :

Welcome to Scala version 2.9.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

sealed abstract class Tree[+T]
case class Node[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T] {
    override def toString = "T(" + value.toString + " " + left.toString + " " + right.toString + ")"
}
case object End extends Tree[Nothing] {
    override def toString = "."
}
object Node {
    def apply[T](value: T): Node[T] = Node(value, End, End)
}

// Exiting paste mode, now interpreting.

defined class Tree
defined class Node
defined module End
defined module Node

scala> Node("123")
res0: Node[java.lang.String] = T(123 . .)

scala>

scala> case class A(i: Int, i2: Int)
defined class A

scala> object A {
     | def apply(i: Int): A = A(i, i)
     | }
:25: error: too many arguments for method apply: (i: Int)A in object A
       def apply(i: Int): A = A(i, i)

scala> object A {
         def apply(i: Int): A = new A(i, i)
       }
defined module A
warning: previously defined class A is not a companion to object A.
Companions must be defined together; you may wish to use :paste mode for this.
Scala: order of definition for companion object vs case class

Scala: order of definition for companion object vs case class


By : user3918005
Date : March 29 2020, 07:55 AM
To fix this issue This is a known bug: SI-3772: companions and method-owned case classes. This is partially fixed, but the OP's issue still remains. Vote it up if you want it fixed.
In Scala, how do I access a case class's private constructor from its companion object

In Scala, how do I access a case class's private constructor from its companion object


By : JoER
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have the following code defined (in Scala IDE/Scala Worksheet with Scala 2.10): , Make it private for anybody but As
code :
object WorkSheet1 {
  object A {
    def apply(s: String, huh: Boolean = false): A = A(s)
  }
  case class A private[A](s: String)
  val a = A("Oh, Hai", false)
}
Scala dependency injection when using case class/companion object pattern

Scala dependency injection when using case class/companion object pattern


By : Y Sun
Date : March 29 2020, 07:55 AM
Hope this helps A common pattern is to put the database functionality in a separate class. In your case leave just the data with the user:
code :
 case class User(id: Option[Long] = None, email: String)
class UserRepository @Inject()(db: Database) {
    def save(user: User) = { ... }
    def create() : User = { ... }
    def findById(id: Long) : Option[User] = { ... }
}
Related Posts Related Posts :
  • Recommended Scala io library
  • Thread-safely transforming a value in a mutable map
  • Why does chaining match expressions does not compile?
  • Ebean not persisting foreign keys
  • Difference between def m(p: T forSome {type T} and def m1(p:Any), is there any ? Explanation needed based on Scala Langu
  • macro does not find out enclosing vals
  • Spark : how to run spark file from spark shell
  • Flattening a list of lists to a set with exceptions in scala
  • flatMap implementation in Scala
  • Confused about a few lines code in a scala official document page
  • How to input parameters when running bash command with Scala
  • Location header is lost if max-redirects > 1
  • Controller Spec is using FakeApplication, but can't load test configuration
  • Scala code analyzer targets case variable names that are identical to the outer matched varables - "suspicous shado
  • Why does authorize directive execute after the code it's supposed to protect?
  • Scala. Checking if a Type is Comparable
  • Does having a private constructor on a value class negate the benefits of a value class?
  • How to transform submitted json in Play 2.0?
  • Scala warning match may not be exhaustive
  • Pure not a member of objective Promise in PlayFramework
  • How to unmarshal POST params and JSON body in a single route?
  • Spark:How to use join method?
  • is client thread-safe in Twitter Finagle
  • Why is the method accepts only one argument?
  • Scala Play 2.3 Working with gCloud Storage - any libs to go async?
  • spray.io strange get/delete/detach directives behavior
  • SBT cannot resolve class declared in src/main/scala in a src/test/scala test class
  • Scala typeclass without function argument
  • Configuring actor behavior using typesafe Config and HOCON
  • Scalatra: Migrating Jersey Filters to Scalatra
  • Compilation error when using Scaldi
  • Scalac hanging in phase typer
  • how to have different source code when cross-compiling Scala with sbt? (changes in MurmurHash)
  • How to set different scalacOptions per Scala version when cross-compiling using Build.scala?
  • Possible Bug in JDBC?
  • Is there a Scala compiler flag to warn when tail recursion is applied without annotation?
  • multiproject sbt doesn't generate file structure
  • Scala "multilevel" abstract class / abstract objects replacement
  • Scala, getting the type parameters of a KList as an HList
  • Why does Play refuse form reporting "Cannot resolve method apply with such signature: Missing arguments"?
  • How to split string with trailing empty strings in result?
  • Scala group by list of list and subtracts grouped values
  • Scala - Creating a function to produce Unary string or integer values
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org