|
| 1 | +scalaShadowing |
| 2 | +# object language |
| 3 | + |
| 4 | +<pre><code class="language-scala" >final object language extends Serializable</pre></code> |
| 5 | +The `scala.language` object controls the language features available to the programmer, as proposed in the |
| 6 | +[**SIP-18 document**](https://docs.google.com/document/d/1nlkvpoIRkx7at1qJEZafJwthZ3GeIklTFhqmXMvTX9Q/edit). |
| 7 | +Each of these features has to be explicitly imported into the current scope to become available: |
| 8 | +```scala |
| 9 | +import language.postfixOps // or language._ |
| 10 | +List(1, 2, 3) reverse |
| 11 | +``` |
| 12 | +The language features are: |
| 13 | +* [`dynamics`](../scalaShadowing/language$.md#dynamics) enables defining calls rewriting using the `Dynamic` trait |
| 14 | +* [`postfixOps`](../scalaShadowing/language$.md#postfixOps) enables postfix operators |
| 15 | +* [`reflectiveCalls`](../scalaShadowing/language$.md#reflectiveCalls) enables using structural types |
| 16 | +* [`implicitConversions`](../scalaShadowing/language$.md#implicitConversions) enables defining implicit methods and members |
| 17 | +* [`higherKinds`](../scalaShadowing/language$.md#higherKinds) enables writing higher-kinded types |
| 18 | +* [`existentials`](../scalaShadowing/language$.md#existentials) enables writing existential types |
| 19 | +* `experimental` contains newer features that have not yet been tested in production |
| 20 | + |
| 21 | +and, for dotty: |
| 22 | +* `Scala2`] backwards compatibility mode for Scala2 |
| 23 | +* `noAutoTupling` disable auto-tupling |
| 24 | +* `strictEquality` enable strick equality |
| 25 | + |
| 26 | +***production*** Language Features |
| 27 | + |
| 28 | +***experimental*** Experimental Language Features |
| 29 | + |
| 30 | +***experimental*** 10 |
| 31 | +Dotty-specific features come at the end. |
| 32 | +Note: Due to the more restricted language import mechanism in dotty (only |
| 33 | +imports count, implicits are disregarded) we don't need the constructions |
| 34 | +of the inherited language features. A simple object for each feature is |
| 35 | +sufficient. |
| 36 | + |
| 37 | +## Known subclasses: |
| 38 | +<a href="./language$/strictEquality$.md">strictEquality</a>, <a href="./language$/noAutoTupling$.md">noAutoTupling</a>, <a href="./language$/Scala2$.md">Scala2</a>, <a href="./language$/experimental$.md">experimental</a> |
| 39 | +## Concrete Type Members: |
| 40 | +### Scala2 |
| 41 | +<pre><code class="language-scala" >final object <a href="./language$/Scala2$.md">Scala2</a></pre></code> |
| 42 | +Where imported, a backwards compatibility mode for Scala2 is enabled |
| 43 | + |
| 44 | +### experimental |
| 45 | +<pre><code class="language-scala" >final object <a href="./language$/experimental$.md">experimental</a></pre></code> |
| 46 | +The experimental object contains features that have been recently added but have not |
| 47 | +been thoroughly tested in production yet. |
| 48 | +Experimental features **may undergo API changes** in future releases, so production |
| 49 | +code should not rely on them. |
| 50 | +Programmers are encouraged to try out experimental features and |
| 51 | +[report any bugs or API inconsistencies](http://issues.scala-lang.org) |
| 52 | +they encounter so they can be improved in future releases. |
| 53 | + |
| 54 | +***Group*** experimental |
| 55 | + |
| 56 | +### noAutoTupling |
| 57 | +<pre><code class="language-scala" >final object <a href="./language$/noAutoTupling$.md">noAutoTupling</a></pre></code> |
| 58 | +Where imported, auto-tupling is disabled |
| 59 | + |
| 60 | +### strictEquality |
| 61 | +<pre><code class="language-scala" >final object <a href="./language$/strictEquality$.md">strictEquality</a></pre></code> |
| 62 | +Where imported loose equality using eqAny is disabled |
| 63 | + |
| 64 | +## Concrete Value Members: |
| 65 | +### != |
| 66 | +<pre><code class="language-scala" >final def !=(x$0: Any): Boolean</pre></code> |
| 67 | + |
| 68 | +### ## |
| 69 | +<pre><code class="language-scala" >final def ##: Int</pre></code> |
| 70 | + |
| 71 | +### == |
| 72 | +<pre><code class="language-scala" >final def ==(x$0: Any): Boolean</pre></code> |
| 73 | + |
| 74 | +### asInstanceOf |
| 75 | +<pre><code class="language-scala" >final def asInstanceOf[X0]: X0</pre></code> |
| 76 | + |
| 77 | +### clone |
| 78 | +<pre><code class="language-scala" >protected def clone(): Object</pre></code> |
| 79 | + |
| 80 | +### eq |
| 81 | +<pre><code class="language-scala" >final def eq(x$0: Object): Boolean</pre></code> |
| 82 | + |
| 83 | +### equals |
| 84 | +<pre><code class="language-scala" >def equals(x$0: Any): Boolean</pre></code> |
| 85 | + |
| 86 | +### finalize |
| 87 | +<pre><code class="language-scala" >protected def finalize(): Unit</pre></code> |
| 88 | + |
| 89 | +### getClass |
| 90 | +<pre><code class="language-scala" >final def getClass(): Class[Nothing <: Any]</pre></code> |
| 91 | + |
| 92 | +### hashCode |
| 93 | +<pre><code class="language-scala" >def hashCode(): Int</pre></code> |
| 94 | + |
| 95 | +### isInstanceOf |
| 96 | +<pre><code class="language-scala" >final def isInstanceOf[X0]: Boolean</pre></code> |
| 97 | + |
| 98 | +### ne |
| 99 | +<pre><code class="language-scala" >final def ne(x$0: Object): Boolean</pre></code> |
| 100 | + |
| 101 | +### notify |
| 102 | +<pre><code class="language-scala" >final def notify(): Unit</pre></code> |
| 103 | + |
| 104 | +### notifyAll |
| 105 | +<pre><code class="language-scala" >final def notifyAll(): Unit</pre></code> |
| 106 | + |
| 107 | +### synchronized |
| 108 | +<pre><code class="language-scala" >final def synchronized[X0](x$0: X0): X0</pre></code> |
| 109 | + |
| 110 | +### toString |
| 111 | +<pre><code class="language-scala" >def toString(): String</pre></code> |
| 112 | + |
| 113 | +### wait |
| 114 | +<pre><code class="language-scala" >final def wait(x$0: Long, x$1: Int): Unit</pre></code> |
| 115 | + |
| 116 | +### wait |
| 117 | +<pre><code class="language-scala" >final def wait(x$0: Long): Unit</pre></code> |
| 118 | + |
| 119 | +### wait |
| 120 | +<pre><code class="language-scala" >final def wait(): Unit</pre></code> |
| 121 | + |
| 122 | +### dynamics |
| 123 | +<pre><code class="language-scala" >@volatile implicit val dynamics: dynamics</pre></code> |
| 124 | +Where enabled, direct or indirect subclasses of trait scala.Dynamic can |
| 125 | +be defined. Unless dynamics is enabled, a definition of a class, trait, |
| 126 | +or object that has Dynamic as a base trait is rejected. Dynamic member |
| 127 | +selection of existing subclasses of trait Dynamic are unaffected; |
| 128 | +they can be used anywhere. |
| 129 | +**Why introduce the feature?** To enable flexible DSLs and convenient interfacing |
| 130 | +with dynamic languages. |
| 131 | +**Why control it?** Dynamic member selection can undermine static checkability |
| 132 | +of programs. Furthermore, dynamic member selection often relies on reflection, |
| 133 | +which is not available on all platforms. |
| 134 | + |
| 135 | +***Group*** production |
| 136 | + |
| 137 | + |
| 138 | +### existentials |
| 139 | +<pre><code class="language-scala" >@volatile implicit val existentials: existentials</pre></code> |
| 140 | +Only where enabled, existential types that cannot be expressed as wildcard |
| 141 | +types can be written and are allowed in inferred types of values or return |
| 142 | +types of methods. Existential types with wildcard type syntax such as `List[_]`, |
| 143 | +or `Map[String, _]` are not affected. |
| 144 | +**Why keep the feature?** Existential types are needed to make sense of Java’s wildcard |
| 145 | +types and raw types and the erased types of run-time values. |
| 146 | +**Why control it?** Having complex existential types in a code base usually makes |
| 147 | +application code very brittle, with a tendency to produce type errors with |
| 148 | +obscure error messages. Therefore, going overboard with existential types |
| 149 | +is generally perceived not to be a good idea. Also, complicated existential types |
| 150 | +might be no longer supported in a future simplification of the language. |
| 151 | + |
| 152 | +***Group*** production |
| 153 | + |
| 154 | + |
| 155 | +### higherKinds |
| 156 | +<pre><code class="language-scala" >@volatile implicit val higherKinds: higherKinds</pre></code> |
| 157 | +Only where this flag is enabled, higher-kinded types can be written. |
| 158 | +**Why keep the feature?** Higher-kinded types enable the definition of very general |
| 159 | +abstractions such as functor, monad, or arrow. A significant set of advanced |
| 160 | +libraries relies on them. Higher-kinded types are also at the core of the |
| 161 | +scala-virtualized effort to produce high-performance parallel DSLs through staging. |
| 162 | +**Why control it?** Higher kinded types in Scala lead to a Turing-complete |
| 163 | +type system, where compiler termination is no longer guaranteed. They tend |
| 164 | +to be useful mostly for type-level computation and for highly generic design |
| 165 | +patterns. The level of abstraction implied by these design patterns is often |
| 166 | +a barrier to understanding for newcomers to a Scala codebase. Some syntactic |
| 167 | +aspects of higher-kinded types are hard to understand for the uninitiated and |
| 168 | +type inference is less effective for them than for normal types. Because we are |
| 169 | +not completely happy with them yet, it is possible that some aspects of |
| 170 | +higher-kinded types will change in future versions of Scala. So an explicit |
| 171 | +enabling also serves as a warning that code involving higher-kinded types |
| 172 | +might have to be slightly revised in the future. |
| 173 | + |
| 174 | +***Group*** production |
| 175 | + |
| 176 | + |
| 177 | +### implicitConversions |
| 178 | +<pre><code class="language-scala" >@volatile implicit val implicitConversions: implicitConversions</pre></code> |
| 179 | +Only where enabled, definitions of legacy implicit conversions and certain uses |
| 180 | +of implicit conversions are allowed. |
| 181 | +A legacy implicit conversion is an implicit value of unary function type `A => B`, |
| 182 | +or an implicit method that has in its first parameter section a single, |
| 183 | +non-implicit parameter. Examples: |
| 184 | +```scala |
| 185 | +implicit def stringToInt(s: String): Int = s.length |
| 186 | +implicit val conv = (s: String) => s.length |
| 187 | +implicit def listToX(xs: List[T])(implicit f: T => X): X = ... |
| 188 | +``` |
| 189 | +Implicit values of other types are not affected, and neither are implicit |
| 190 | +classes. In particular, implied instances of the scala.Conversion class can be |
| 191 | +defined without having to import the language feature. |
| 192 | +The language import is also required to enable _uses_ of implicit conversions |
| 193 | +unless the conversion in question is co-defined with the type to which it maps. |
| 194 | +Co-defined means: defined in the companion object of the class of the result type. |
| 195 | +Examples: |
| 196 | +```scala |
| 197 | +class A |
| 198 | +class B |
| 199 | +object B { |
| 200 | + implied a2b for Conversion[A, B] { ... } |
| 201 | +} |
| 202 | +object C { |
| 203 | + implied b2a for Conversion[B, A] { ... } |
| 204 | +} |
| 205 | +import implied B._ |
| 206 | +import implied C._ |
| 207 | +val x: A = new B // language import required |
| 208 | +val x: B = new A // no import necessary since a2b is co-defined with B |
| 209 | +``` |
| 210 | +**Why keep the feature?** Implicit conversions are central to many aspects |
| 211 | +of Scala’s core libraries. |
| 212 | +**Why control it?** Implicit conversions are known to cause many pitfalls |
| 213 | +if over-used. This holds in particular for implicit conversions defined after |
| 214 | +the fact between unrelated types. |
| 215 | + |
| 216 | +***Group*** production |
| 217 | + |
| 218 | + |
| 219 | +### postfixOps |
| 220 | +<pre><code class="language-scala" >@volatile implicit val postfixOps: postfixOps</pre></code> |
| 221 | +Only where enabled, postfix operator notation `(expr op)` will be allowed. |
| 222 | +**Why keep the feature?** Several DSLs written in Scala need the notation. |
| 223 | +**Why control it?** Postfix operators interact poorly with semicolon inference. |
| 224 | + Most programmers avoid them for this reason. |
| 225 | + |
| 226 | +***Group*** production |
| 227 | + |
| 228 | + |
| 229 | +### reflectiveCalls |
| 230 | +<pre><code class="language-scala" >@volatile implicit val reflectiveCalls: reflectiveCalls</pre></code> |
| 231 | +Only where enabled, accesses to members of structural types that need |
| 232 | +reflection are supported. Reminder: A structural type is a type of the form |
| 233 | +`Parents { Decls }` where `Decls` contains declarations of new members that do |
| 234 | +not override any member in `Parents`. To access one of these members, a |
| 235 | +reflective call is needed. |
| 236 | +**Why keep the feature?** Structural types provide great flexibility because |
| 237 | +they avoid the need to define inheritance hierarchies a priori. Besides, |
| 238 | +their definition falls out quite naturally from Scala’s concept of type refinement. |
| 239 | +**Why control it?** Reflection is not available on all platforms. Popular tools |
| 240 | +such as ProGuard have problems dealing with it. Even where reflection is available, |
| 241 | +reflective dispatch can lead to surprising performance degradations. |
| 242 | + |
| 243 | +***Group*** production |
| 244 | + |
| 245 | + |
0 commit comments