Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    how would you add custom variable type to an object. AFAIK it's not possible. Here is an example: You have this class class Average[YType](yZeroValue:YType). How do you pass YType from it's object since objects can't take type params? Commented Feb 26, 2014 at 15:24
  • Types in Scala can usually be inferred based on the arguments, but if not you can supply them via the square brackets. so when instantiating an Average object you could say "val avg = new Average[Int](0)" Commented Jul 6, 2014 at 19:26
  • 8
    Case classes are worth a mention here. Case classes conveniently, automagically and invisibly adds apply and unapply methods to a companion object of the class (among other things). So defining a class with just one line like this case class Car(make:String, model: String, year: Short, color: String) makes it possible to produce new objects of the Car class by just: val myCar = Car("VW","Golf",1999,"Blue"). This is shorthand for Car.apply(...) Commented Sep 27, 2017 at 9:10
  • 8
    every object can be treated as a function, provided it has the apply method - so much now makes sense. Commented Jun 19, 2018 at 7:35
  • see also slideshare.net/pjschwarz/… Commented Jan 15, 2020 at 9:20