New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
any2stringadd implicit should be removed from Predef #194
Comments
Imported From: https://issues.scala-lang.org/browse/SI-194?orig=1 |
@odersky said: |
@odersky said: |
Eugene Yokota (eed3si9n) said: |
Heikki Vesalainen (hvesalai) said: |
@SethTisue said: |
@Sciss said: |
Kevin Wright (kev.lee.wright) said: |
Heikki Vesalainen (hvesalai) said: |
@soc said: |
Kevin Wright (kev.lee.wright) said: |
Rich Oliver (rich oliver) said: Println etc can now be implemented as macros for ease of use. So there's even less justification for polluting the whole of the global name-space. |
Alexandru Nedelcu (alex_ndc) said: Here's Python: >>> "" + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects Here's Ruby: irb(main):001:0> "" + 2
TypeError: can't convert Fixnum into String
from (irb):1:in `+'
from (irb):1
from /usr/bin/irb:12:in `<main>' And in languages where it is, it's given as a bad example. I think Java folks can learn to appreciate a lack of implicit conversion to string, especially given that they don't need it with string interpolation. |
Matthew Pocock (drdozer) said: What else needs tackling related to this? Perhaps we need to grep the stdlib for all |
@adriaanm said: |
@SethTisue said (edited on Feb 12, 2014 4:24:44 PM UTC):
This is just one example; I've seen versions of this exchange on #scala countless times over the years. It gets especially confusing if someone points out or realizes that Set actually does expect + and not :+ or +: . |
@SethTisue said: |
@SethTisue said: |
@adriaanm said: |
Naftoli Gugenheim (naftoligug) said: |
@cvogt said: |
Lionel Parreaux (LPTK) said: def any2stringadd = ??? |
@som-snytt said: scala> case class C(c: String = "hi")
defined class C
scala> { def any2stringadd = ??? ; C() + "EOS" }
<console>:13: error: value + is not a member of C
{ def any2stringadd = ??? ; C() + "EOS" }
^
scala> { def any2stringadd = ??? ; (C(): any2stringadd[C]) + "EOS" }
res1: String = C(hi)EOS
scala> { def any2stringadd = ??? ; type any2stringadd[_] = Nothing ; (C(): any2stringadd[C]) + "EOS" }
<console>:13: error: type mismatch;
found : C
required: any2stringadd[C]
(which expands to) Nothing
{ def any2stringadd = ??? ; type any2stringadd[_] = Nothing ; (C(): any2stringadd[C]) + "EOS" }
^
<console>:13: error: value + is not a member of any2stringadd[C]
{ def any2stringadd = ??? ; type any2stringadd[_] = Nothing ; (C(): any2stringadd[C]) + "EOS" }
^ |
@puffnfresh said: package object io.atlassian {
def undefined[A]: A = sys.error("undefined")
// any2stringadd allows anything to be added against String.
class AmbiguousStringAdd {
def +(b: String) = undefined
}
implicit def amb1any2stringadd(a: Any) = new AmbiguousStringAdd
implicit def amb2any2stringadd(a: Any) = new AmbiguousStringAdd
} |
@puffnfresh said (edited on Jul 30, 2015 10:20:02 PM UTC): I'll submit a PR to scala/scala |
@SethTisue said: |
@SethTisue said: |
Another newbie confused by |
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the convertion altogether under `-Xfuture` flag. Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the conversion altogether under `-Xsource:2.14` flag. ### Background Scala up till 2.12 has had two kinds of String concatenation operator `+` (also known as PLUS or ADD). a. When the receiver is a non-String, it uses implicit `Predef.any2addstring` to inject `+`. b. When the receiver is a String, the compiler injects a special method `String_+`. In other words, `true + "what"` and `"what" + true` use different mechanisms. This change addresses the first variant that introduces `+` operator into `Any`. The rationale discussed in scala/bug#194 is that this implicit injection removes too much type safety, leading to confusing results. Here some examples listed: ``` scala> var v = Vector[Int]() v: scala.collection.immutable.Vector[Int] = Vector() scala> v += 3 <console>:13: error: value += is not a member of scala.collection.immutable.Vector[Int] Expression does not convert to assignment because: type mismatch; found : Int(3) required: String expansion: v = v.$plus(3) v += 3 ^ scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> xs foreach { println _ + 1 } <console>:13: error: type mismatch; found : Int(1) required: String xs foreach { println _ + 1 } ^ ``` Note that in both examples the person who wrote the code were not trying to concatenate String. ### Implementation note Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the conversion altogether under `-Xsource:2.14` flag. ### Background Scala up till 2.12 has had two kinds of String concatenation operator `+` (also known as PLUS or ADD). a. When the receiver is a non-String, it uses implicit `Predef.any2addstring` to inject `+`. b. When the receiver is a String, the compiler injects a special method `String_+`. In other words, `true + "what"` and `"what" + true` use different mechanisms. This change addresses the first variant that introduces `+` operator into `Any`. The rationale discussed in scala/bug#194 is that this implicit injection removes too much type safety, leading to confusing results. Here some examples listed: ``` scala> var v = Vector[Int]() v: scala.collection.immutable.Vector[Int] = Vector() scala> v += 3 <console>:13: error: value += is not a member of scala.collection.immutable.Vector[Int] Expression does not convert to assignment because: type mismatch; found : Int(3) required: String expansion: v = v.$plus(3) v += 3 ^ scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> xs foreach { println _ + 1 } <console>:13: error: type mismatch; found : Int(1) required: String xs foreach { println _ + 1 } ^ ``` Note that in both examples the person who wrote the code were not trying to concatenate String. ### Implementation note Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the conversion altogether under `-Xsource:2.14` flag. ### Background Scala up till 2.12 has had two kinds of String concatenation operator `+` (also known as PLUS or ADD). a. When the receiver is a non-String, it uses implicit `Predef.any2addstring` to inject `+`. b. When the receiver is a String, the compiler injects a special method `String_+`. In other words, `true + "what"` and `"what" + true` use different mechanisms. This change addresses the first variant that introduces `+` operator into `Any`. The rationale discussed in scala/bug#194 is that this implicit injection removes too much type safety, leading to confusing results. Here some examples listed: ``` scala> var v = Vector[Int]() v: scala.collection.immutable.Vector[Int] = Vector() scala> v += 3 <console>:13: error: value += is not a member of scala.collection.immutable.Vector[Int] Expression does not convert to assignment because: type mismatch; found : Int(3) required: String expansion: v = v.$plus(3) v += 3 ^ scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> xs foreach { println _ + 1 } <console>:13: error: type mismatch; found : Int(1) required: String xs foreach { println _ + 1 } ^ ``` Note that in both examples the person who wrote the code were not trying to concatenate String. ### Implementation note Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the conversion altogether under `-Xsource:2.14` flag. ### Background Scala up till 2.12 has had two kinds of String concatenation operator `+` (also known as PLUS or ADD). a. When the receiver is a non-String, it uses implicit `Predef.any2addstring` to inject `+`. b. When the receiver is a String, the compiler injects a special method `String_+`. In other words, `true + "what"` and `"what" + true` use different mechanisms. This change addresses the first variant that introduces `+` operator into `Any`. The rationale discussed in scala/bug#194 is that this implicit injection removes too much type safety, leading to confusing results. Here some examples listed: ``` scala> var v = Vector[Int]() v: scala.collection.immutable.Vector[Int] = Vector() scala> v += 3 <console>:13: error: value += is not a member of scala.collection.immutable.Vector[Int] Expression does not convert to assignment because: type mismatch; found : Int(3) required: String expansion: v = v.$plus(3) v += 3 ^ scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> xs foreach { println _ + 1 } <console>:13: error: type mismatch; found : Int(1) required: String xs foreach { println _ + 1 } ^ ``` Note that in both examples the person who wrote the code were not trying to concatenate String. ### Implementation note Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the conversion altogether under `-Xsource:2.14` flag. ### Background Scala up till 2.12 has had two kinds of String concatenation operator `+` (also known as PLUS or ADD). a. When the receiver is a non-String, it uses implicit `Predef.any2addstring` to inject `+`. b. When the receiver is a String, the compiler injects a special method `String_+`. In other words, `true + "what"` and `"what" + true` use different mechanisms. This change addresses the first variant that introduces `+` operator into `Any`. The rationale discussed in scala/bug#194 is that this implicit injection removes too much type safety, leading to confusing results. Here some examples listed: ``` scala> var v = Vector[Int]() v: scala.collection.immutable.Vector[Int] = Vector() scala> v += 3 <console>:13: error: value += is not a member of scala.collection.immutable.Vector[Int] Expression does not convert to assignment because: type mismatch; found : Int(3) required: String expansion: v = v.$plus(3) v += 3 ^ scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> xs foreach { println _ + 1 } <console>:13: error: type mismatch; found : Int(1) required: String xs foreach { println _ + 1 } ^ ``` Note that in both examples the person who wrote the code were not trying to concatenate String. ### Implementation note Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the conversion altogether under `-Xsource:2.14` flag. Scala up till 2.12 has had two kinds of String concatenation operator `+` (also known as PLUS or ADD). a. When the receiver is a non-String, it uses implicit `Predef.any2addstring` to inject `+`. b. When the receiver is a String, the compiler injects a special method `String_+`. In other words, `true + "what"` and `"what" + true` use different mechanisms. This change addresses the first variant that introduces `+` operator into `Any`. The rationale discussed in scala/bug#194 is that this implicit injection removes too much type safety, leading to confusing results. Here some examples listed: ``` scala> var v = Vector[Int]() v: scala.collection.immutable.Vector[Int] = Vector() scala> v += 3 <console>:13: error: value += is not a member of scala.collection.immutable.Vector[Int] Expression does not convert to assignment because: type mismatch; found : Int(3) required: String expansion: v = v.$plus(3) v += 3 ^ scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> xs foreach { println _ + 1 } <console>:13: error: type mismatch; found : Int(1) required: String xs foreach { println _ + 1 } ^ ``` Note that in both examples the person who wrote the code were not trying to concatenate String. Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
Ref scala/bug#194 1. This deprecates scala.Predef.any2stringadd. 2. Another thing it does is to remove the conversion altogether under `-Xsource:2.14` flag. Scala up till 2.12 has had two kinds of String concatenation operator `+` (also known as PLUS or ADD). a. When the receiver is a non-String, it uses implicit `Predef.any2addstring` to inject `+`. b. When the receiver is a String, the compiler injects a special method `String_+`. In other words, `true + "what"` and `"what" + true` use different mechanisms. This change addresses the first variant that introduces `+` operator into `Any`. The rationale discussed in scala/bug#194 is that this implicit injection removes too much type safety, leading to confusing results. Here some examples listed: ``` scala> var v = Vector[Int]() v: scala.collection.immutable.Vector[Int] = Vector() scala> v += 3 <console>:13: error: value += is not a member of scala.collection.immutable.Vector[Int] Expression does not convert to assignment because: type mismatch; found : Int(3) required: String expansion: v = v.$plus(3) v += 3 ^ scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> xs foreach { println _ + 1 } <console>:13: error: type mismatch; found : Int(1) required: String xs foreach { println _ + 1 } ^ ``` Note that in both examples the person who wrote the code were not trying to concatenate String. Initially I've attempted to implement what Adriaan proposed in 2014 (scala/bug#194 (comment)) and remove all deprecated implicits under `-Xfuture` flag. A simple implementation ```scala val fd = settings.future && sym.isDeprecated ``` did not work, because the implicit caching is unware of the annotations?
scala/scala#6315, which deprecates |
This implicit removes too much type safety, and can lead to confusing results.
I think this is more important than the '+' operator being symmetric.
UPDATE 2015-07-29:
Workaround: You can opt-out on a per-file basis by unimporting it.
The text was updated successfully, but these errors were encountered: