Scala Style Guide: Why Mimic a function? -
i’m reading scala style guide: http://docs.scala-lang.org/style/naming-conventions.html
and mention this:
objects
objects follow class naming convention (camelcase capital first letter) except when attempting mimic package or function. these situations don’t happen often, can expected in general development.:
object ast { sealed trait expr case class plus(e1: expr, e2: expr) extends expr ... } object inc { def apply(x: int): int = x + 1 }
i can think of maybe few thin use cases "object ast". can't think of why want "mimic function" in manner of "object inc". feels bit unconventional, , confuse other developers.
are there example cases core scala libraries this? or when practice define function this?
as mentioned in comments, 1 example shapeless.poly functions.
a poly
function polymorphic version of function. needs represented object 3 main reasons:
- it contains multiple functions (to handle multiple cases, since they're polymorphic)
- an object's companion object object itself. allows defining various cases implicit methods inside object , have them picked compiler
- objects provide stable identifier, compiler won't complain when passing instance of function of shapeless's methods
technicalities aside, they're conceptually functions, hence same naming style regular functions used.
Comments
Post a Comment