or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

arrayseq.mdbuildfrom.mdcollection-extensions.mdfactory-operations.mdindex.mdmigration-tools.md

factory-operations.mddocs/

0

# Collection Factory Operations

1

2

This document covers the factory operations that enable 2.13-style collection creation patterns in 2.11/2.12.

3

4

## Core Factory API

5

6

### Factory Type

7

8

```scala { .api }

9

type Factory[-A, +C] = CanBuildFrom[Nothing, A, C]

10

```

11

12

The `Factory` type provides a unified interface for collection creation across Scala versions.

13

14

### FactoryOps Extension Methods

15

16

```scala { .api }

17

implicit class FactoryOps[-A, +C](private val factory: Factory[A, C]) {

18

/**

19

* Creates a collection of type C containing the same elements

20

* as the source collection.

21

*/

22

def fromSpecific(it: TraversableOnce[A]): C

23

24

/**

25

* Gets a Builder for the collection. For non-strict collection types

26

* this will use an intermediate buffer. Building collections with

27

* fromSpecific is preferred because it can be lazy for lazy collections.

28

*/

29

def newBuilder: Builder[A, C]

30

}

31

```

32

33

## Implicit Factory Conversions

34

35

### Generic Companion Conversion

36

37

```scala { .api }

38

implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](

39

fact: GenericCompanion[CC]

40

): CanBuildFrom[Any, A, CC[A]]

41

```

42

43

Converts generic collection companions (like `List`, `Vector`) to factory instances.

44

45

### Specialized Factory Conversions

46

47

```scala { .api }

48

// Sorted set factory conversion

49

implicit def sortedSetCompanionToCBF[A: Ordering, CC[X] <: SortedSet[X]](

50

fact: SortedSetFactory[CC]

51

): CanBuildFrom[Any, A, CC[A]]

52

53

// Array companion conversion

54

implicit def arrayCompanionToCBF[A: ClassTag](

55

fact: Array.type

56

): CanBuildFrom[Any, A, Array[A]]

57

58

// Map factory conversion

59

implicit def mapFactoryToCBF[K, V, CC[A, B] <: Map[A, B]](

60

fact: MapFactory[CC]

61

): CanBuildFrom[Any, (K, V), CC[K, V]]

62

63

// Sorted map factory conversion

64

implicit def sortedMapFactoryToCBF[K: Ordering, V, CC[A, B] <: SortedMap[A, B]](

65

fact: SortedMapFactory[CC]

66

): CanBuildFrom[Any, (K, V), CC[K, V]]

67

```

68

69

### BitSet Factory Conversions

70

71

```scala { .api }

72

// Generic BitSet factory conversion

73

implicit def bitSetFactoryToCBF(

74

fact: BitSetFactory[BitSet]

75

): CanBuildFrom[Any, Int, BitSet]

76

77

// Immutable BitSet factory conversion

78

implicit def immutableBitSetFactoryToCBF(

79

fact: BitSetFactory[immutable.BitSet]

80

): CanBuildFrom[Any, Int, ImmutableBitSetCC[Int]]

81

82

// Mutable BitSet factory conversion

83

implicit def mutableBitSetFactoryToCBF(

84

fact: BitSetFactory[mutable.BitSet]

85

): CanBuildFrom[Any, Int, MutableBitSetCC[Int]]

86

```

87

88

## Factory Extension Methods

89

90

### Iterable Factory Extensions

91

92

```scala { .api }

93

implicit class IterableFactoryExtensionMethods[CC[X] <: GenTraversable[X]](

94

private val fact: GenericCompanion[CC]

95

) {

96

/**

97

* Creates a collection from a source of elements.

98

*/

99

def from[A](source: TraversableOnce[A]): CC[A]

100

}

101

```

102

103

### Map Factory Extensions

104

105

```scala { .api }

106

implicit class MapFactoryExtensionMethods[CC[A, B] <: Map[A, B]](

107

private val fact: MapFactory[CC]

108

) {

109

/**

110

* Creates a map from a source of key-value pairs.

111

*/

112

def from[K, V](source: TraversableOnce[(K, V)]): CC[K, V]

113

}

114

```

115

116

### BitSet Factory Extensions

117

118

```scala { .api }

119

implicit class BitSetFactoryExtensionMethods[C <: BitSet with BitSetLike[C]](

120

private val fact: BitSetFactory[C]

121

) {

122

/**

123

* Creates a BitSet from a source of integers.

124

*/

125

def fromSpecific(source: TraversableOnce[Int]): C

126

}

127

```

128

129

## Usage Examples

130

131

### Basic Factory Usage

132

133

```scala

134

import scala.collection.compat._

135

136

// Create collections using factory pattern

137

val data = List(1, 2, 3, 4, 5)

138

139

// Using implicit factory conversions

140

val vector = Vector.from(data)

141

val set = Set.from(data)

142

val array = Array.from(data)

143

144

// Using explicit factory operations

145

val factory: Factory[Int, List[Int]] = List

146

val result = factory.fromSpecific(data)

147

```

148

149

### Generic Factory Functions

150

151

```scala

152

import scala.collection.compat._

153

154

def createCollection[A, C](

155

elements: TraversableOnce[A],

156

factory: Factory[A, C]

157

): C = {

158

factory.fromSpecific(elements)

159

}

160

161

// Usage with different collection types

162

val numbers = List(1, 2, 3)

163

val vector = createCollection(numbers, Vector)

164

val set = createCollection(numbers, Set)

165

```

166

167

### Map Factory Usage

168

169

```scala

170

import scala.collection.compat._

171

172

val pairs = List(("a", 1), ("b", 2), ("c", 3))

173

174

// Create different map types

175

val hashMap = mutable.HashMap.from(pairs)

176

val treeMap = immutable.TreeMap.from(pairs)

177

val listMap = immutable.ListMap.from(pairs)

178

```

179

180

### Builder Pattern

181

182

```scala

183

import scala.collection.compat._

184

185

val factory: Factory[String, Vector[String]] = Vector

186

val builder = factory.newBuilder

187

188

builder += "hello"

189

builder += "world"

190

builder ++= List("scala", "collection")

191

192

val result = builder.result()

193

// result: Vector("hello", "world", "scala", "collection")

194

```

195

196

## Cross-Version Compatibility

197

198

The factory operations work identically across Scala versions:

199

200

**Scala 2.11/2.12**: Uses `CanBuildFrom` internally with compatibility extensions

201

**Scala 2.13**: Direct aliases to the standard library's `Factory` type

202

203

This ensures that code written using the factory pattern compiles and runs consistently across all supported Scala versions.