|
| 1 | +/* |
| 2 | + * Scala (https://www.scala-lang.org) |
| 3 | + * |
| 4 | + * Copyright EPFL and Lightbend, Inc. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (http://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package scala |
| 14 | + |
| 15 | +/** |
| 16 | + * The `scala.language` object controls the language features available to the programmer, as proposed in the |
| 17 | + * [[https://docs.google.com/document/d/1nlkvpoIRkx7at1qJEZafJwthZ3GeIklTFhqmXMvTX9Q/edit '''SIP-18 document''']]. |
| 18 | + * |
| 19 | + * Each of these features has to be explicitly imported into the current scope to become available: |
| 20 | + * {{{ |
| 21 | + * import language.postfixOps // or language._ |
| 22 | + * List(1, 2, 3) reverse |
| 23 | + * }}} |
| 24 | + * |
| 25 | + * The language features are: |
| 26 | + * - [[dynamics `dynamics`]] enables defining calls rewriting using the [[scala.Dynamic `Dynamic`]] trait |
| 27 | + * - [[existentials `existentials`]] enables writing existential types |
| 28 | + * - [[higherKinds `higherKinds`]] enables writing higher-kinded types |
| 29 | + * - [[implicitConversions `implicitConversions`]] enables defining implicit methods and members |
| 30 | + * - [[postfixOps `postfixOps`]] enables postfix operators (not recommended) |
| 31 | + * - [[reflectiveCalls `reflectiveCalls`]] enables using structural types |
| 32 | + * - [[experimental `experimental`]] contains newer features that have not yet been tested in production |
| 33 | + * |
| 34 | + * @groupname production Language Features |
| 35 | + * @groupname experimental Experimental Language Features |
| 36 | + * @groupprio experimental 10 |
| 37 | + */ |
| 38 | +object language { |
| 39 | + |
| 40 | + import languageFeature._ |
| 41 | + |
| 42 | + /** Only where this feature is enabled, can direct or indirect subclasses of trait scala.Dynamic |
| 43 | + * be defined. If `dynamics` is not enabled, a definition of a class, trait, |
| 44 | + * or object that has `Dynamic` as a base trait is rejected by the compiler. |
| 45 | + * |
| 46 | + * Selections of dynamic members of existing subclasses of trait `Dynamic` are unaffected; |
| 47 | + * they can be used anywhere. |
| 48 | + * |
| 49 | + * '''Why introduce the feature?''' To enable flexible DSLs and convenient interfacing |
| 50 | + * with dynamic languages. |
| 51 | + * |
| 52 | + * '''Why control it?''' Dynamic member selection can undermine static checkability |
| 53 | + * of programs. Furthermore, dynamic member selection often relies on reflection, |
| 54 | + * which is not available on all platforms. |
| 55 | + * |
| 56 | + * @group production |
| 57 | + */ |
| 58 | + implicit lazy val dynamics: dynamics = languageFeature.dynamics |
| 59 | + |
| 60 | + /** Only where this feature is enabled, is postfix operator notation `(expr op)` permitted. |
| 61 | + * If `postfixOps` is not enabled, an expression using postfix notation is rejected by the compiler. |
| 62 | + * |
| 63 | + * '''Why keep the feature?''' Postfix notation is preserved for backward |
| 64 | + * compatibility only. Historically, several DSLs written in Scala need the notation. |
| 65 | + * |
| 66 | + * '''Why control it?''' Postfix operators interact poorly with semicolon inference. |
| 67 | + * Most programmers avoid them for this reason alone. Postfix syntax is |
| 68 | + * associated with an abuse of infix notation, `a op1 b op2 c op3`, |
| 69 | + * that can be harder to read than ordinary method invocation with judicious |
| 70 | + * use of parentheses. It is recommended not to enable this feature except for |
| 71 | + * legacy code. |
| 72 | + * |
| 73 | + * @group production |
| 74 | + */ |
| 75 | + implicit lazy val postfixOps: postfixOps = languageFeature.postfixOps |
| 76 | + |
| 77 | + /** Where this feature is enabled, accesses to members of structural types that need |
| 78 | + * reflection are supported. If `reflectiveCalls` is not enabled, an expression |
| 79 | + * requiring reflection will trigger a warning from the compiler. |
| 80 | + * |
| 81 | + * A structural type is a type of the form |
| 82 | + * `Parents { Decls }` where `Decls` contains declarations of new members that do |
| 83 | + * not override any member in `Parents`. To access one of these members, a |
| 84 | + * reflective call is needed. |
| 85 | + * |
| 86 | + * '''Why keep the feature?''' Structural types provide great flexibility because |
| 87 | + * they avoid the need to define inheritance hierarchies a priori. Besides, |
| 88 | + * their definition falls out quite naturally from Scala’s concept of type refinement. |
| 89 | + * |
| 90 | + * '''Why control it?''' Reflection is not available on all platforms. Popular tools |
| 91 | + * such as ProGuard have problems dealing with it. Even where reflection is available, |
| 92 | + * reflective dispatch can lead to surprising performance degradations. |
| 93 | + * |
| 94 | + * @group production |
| 95 | + */ |
| 96 | + implicit lazy val reflectiveCalls: reflectiveCalls = languageFeature.reflectiveCalls |
| 97 | + |
| 98 | + /** Where this feature is enabled, definitions of implicit conversions are allowed. |
| 99 | + * If `implicitConversions` is not enabled, the definition of an implicit |
| 100 | + * conversion will trigger a warning from the compiler. |
| 101 | + * |
| 102 | + * An implicit conversion is an implicit value of unary function type `A => B`, |
| 103 | + * or an implicit method that has in its first parameter section a single, |
| 104 | + * non-implicit parameter. Examples: |
| 105 | + * |
| 106 | + * {{{ |
| 107 | + * implicit def stringToInt(s: String): Int = s.length |
| 108 | + * implicit val conv = (s: String) => s.length |
| 109 | + * implicit def listToX(xs: List[T])(implicit f: T => X): X = ... |
| 110 | + * }}} |
| 111 | + * |
| 112 | + * Implicit classes and implicit values of other types are not governed by this |
| 113 | + * language feature. |
| 114 | + * |
| 115 | + * '''Why keep the feature?''' Implicit conversions are central to many aspects |
| 116 | + * of Scala’s core libraries. |
| 117 | + * |
| 118 | + * '''Why control it?''' Implicit conversions are known to cause many pitfalls |
| 119 | + * if over-used. And there is a tendency to over-use them because they look |
| 120 | + * very powerful and their effects seem to be easy to understand. Also, in |
| 121 | + * most situations using implicit parameters leads to a better design than |
| 122 | + * implicit conversions. |
| 123 | + * |
| 124 | + * @group production |
| 125 | + */ |
| 126 | + implicit lazy val implicitConversions: implicitConversions = languageFeature.implicitConversions |
| 127 | + |
| 128 | + /** Where this feature is enabled, higher-kinded types can be written. |
| 129 | + * If `higherKinds` is not enabled, a higher-kinded type such as `F[A]` |
| 130 | + * will trigger a warning from the compiler. |
| 131 | + * |
| 132 | + * '''Why keep the feature?''' Higher-kinded types enable the definition of very general |
| 133 | + * abstractions such as functor, monad, or arrow. A significant set of advanced |
| 134 | + * libraries relies on them. Higher-kinded types are also at the core of the |
| 135 | + * scala-virtualized effort to produce high-performance parallel DSLs through staging. |
| 136 | + * |
| 137 | + * '''Why control it?''' Higher kinded types in Scala lead to a Turing-complete |
| 138 | + * type system, where compiler termination is no longer guaranteed. They tend |
| 139 | + * to be useful mostly for type-level computation and for highly generic design |
| 140 | + * patterns. The level of abstraction implied by these design patterns is often |
| 141 | + * a barrier to understanding for newcomers to a Scala codebase. Some syntactic |
| 142 | + * aspects of higher-kinded types are hard to understand for the uninitiated and |
| 143 | + * type inference is less effective for them than for normal types. Because we are |
| 144 | + * not completely happy with them yet, it is possible that some aspects of |
| 145 | + * higher-kinded types will change in future versions of Scala. So an explicit |
| 146 | + * enabling also serves as a warning that code involving higher-kinded types |
| 147 | + * might have to be slightly revised in the future. |
| 148 | + * |
| 149 | + * @group production |
| 150 | + */ |
| 151 | + @deprecated("higherKinds no longer needs to be imported explicitly", "2.13.1") |
| 152 | + implicit lazy val higherKinds: higherKinds = languageFeature.higherKinds |
| 153 | + |
| 154 | + /** Where this feature is enabled, existential types that cannot be expressed as wildcard |
| 155 | + * types can be written and are allowed in inferred types of values or return |
| 156 | + * types of methods. If `existentials` is not enabled, those cases will trigger |
| 157 | + * a warning from the compiler. |
| 158 | + * |
| 159 | + * Existential types with wildcard type syntax such as `List[_]`, |
| 160 | + * or `Map[String, _]` are not affected. |
| 161 | + * |
| 162 | + * '''Why keep the feature?''' Existential types are needed to make sense of Java’s wildcard |
| 163 | + * types and raw types and the erased types of run-time values. |
| 164 | + * |
| 165 | + * '''Why control it?''' Having complex existential types in a code base usually makes |
| 166 | + * application code very brittle, with a tendency to produce type errors with |
| 167 | + * obscure error messages. Therefore, going overboard with existential types |
| 168 | + * is generally perceived not to be a good idea. Also, complicated existential types |
| 169 | + * might be no longer supported in a future simplification of the language. |
| 170 | + * |
| 171 | + * @group production |
| 172 | + */ |
| 173 | + implicit lazy val existentials: existentials = languageFeature.existentials |
| 174 | + |
| 175 | + /** The experimental object contains features that are known to have unstable API or |
| 176 | + * behavior that may change in future releases. |
| 177 | + * |
| 178 | + * Experimental features '''may undergo API changes''' in future releases, so production |
| 179 | + * code should not rely on them. |
| 180 | + * |
| 181 | + * Programmers are encouraged to try out experimental features and |
| 182 | + * [[https://github.com/scala/bug/issues report any bugs or API inconsistencies]] |
| 183 | + * they encounter so they can be improved in future releases. |
| 184 | + * |
| 185 | + * @group experimental |
| 186 | + */ |
| 187 | + object experimental { |
| 188 | + |
| 189 | + import languageFeature.experimental._ |
| 190 | + |
| 191 | + /** Only where this feature is enabled, are macro definitions allowed. |
| 192 | + * If `macros` is not enabled, macro definitions are rejected by the compiler. |
| 193 | + * |
| 194 | + * Macro implementations and macro applications are not governed by this |
| 195 | + * language feature; they can be used anywhere. |
| 196 | + * |
| 197 | + * '''Why introduce the feature?''' Macros promise to make the language more regular, |
| 198 | + * replacing ad-hoc language constructs with a general powerful abstraction |
| 199 | + * capability that can express them. Macros are also a more disciplined and |
| 200 | + * powerful replacement for compiler plugins. |
| 201 | + * |
| 202 | + * '''Why control it?''' For their very power, macros can lead to code that is hard |
| 203 | + * to debug and understand. |
| 204 | + */ |
| 205 | + implicit lazy val macros: macros = languageFeature.experimental.macros |
| 206 | + } |
| 207 | +} |
0 commit comments