Why do some class classes have an "empty" instance method in addition to the class method?

Both exist, for example, here:

Map.empty[Int, Int]
Map(1 -> 41).empty

Set().empty
Set.empty

But here there are only class methods:

List.empty        //OK
List(1,2,3).empty //Doesn't exist

Array.empty       //OK
Array("a").empty  //Doesn't exist

Is an emptyideal case for a class method (and therefore should not exclude an instance method empty)?

Or should instance method a be added emptyto classes that skip it?

Is there anything in terms of language that makes it difficult to use emptya class as a method (for example, type inference, higher class types, ...).

PS: It has been suggested that cards with default values ​​would be more difficult to achieve without an instance method empty:

Map[Int, Int](1->2, 21->42).withDefault(_*2).empty

What do you think?

+3
source
3

, empty . , , , . , , , , .

+2

, Map empty, . , , -, -.

:

(List(1, 2, 3): Traversable[Int]).companion.empty

Traversable, , .

+1

. , .

mutables, .

... I think something is missing with this explanation, but perhaps this is the beginning.

0
source

Source: https://habr.com/ru/post/1790914/


All Articles