Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-194

any2stringadd implicit should be removed from Predef

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Component/s: Library (Misc)
    • Labels:
      None

      Description

      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.

      import Predef.{any2stringadd => _,_}
      object foo extends App{
        println(new java.util.Date + " sfsdf") // error: value + is not a member of java.util.Date
      }
      

        Attachments

          Issue Links

            Activity

            Hide
            apm A. P. Marki added a comment -

            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" }
                                                                                                         ^
            

            Show
            apm A. P. Marki added a comment - 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" } ^
            Hide
            puffnfresh Brian McKenna added a comment -

            Another option is to make the implicit ambiguous, can do that just once for your project by putting it in a top-level package object:

            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
            }
            

            Show
            puffnfresh Brian McKenna added a comment - Another option is to make the implicit ambiguous, can do that just once for your project by putting it in a top-level package object: 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 amb 1 any 2 stringadd(a : Any) = new AmbiguousStringAdd implicit def amb 2 any 2 stringadd(a : Any) = new AmbiguousStringAdd }
            Hide
            puffnfresh Brian McKenna added a comment - - edited

            I also wrote a patch for SI-6806, which allows the above to have a nice error message:

            https://github.com/scala/scala/commit/4d4f70373523085041136b6b570b676ffb96a3e9

            I'll submit a PR to scala/scala

            Show
            puffnfresh Brian McKenna added a comment - - edited I also wrote a patch for SI-6806 , which allows the above to have a nice error message: https://github.com/scala/scala/commit/4d4f70373523085041136b6b570b676ffb96a3e9 I'll submit a PR to scala/scala
            Hide
            sethtisue Seth Tisue added a comment -
            Show
            sethtisue Seth Tisue added a comment - latest go-round on scala-user: https://groups.google.com/d/msg/scala-user/9h41cxPDvI8/rYvCiX2hCgAJ
            Hide
            sethtisue Seth Tisue added a comment -
            Show
            sethtisue Seth Tisue added a comment - proposal from A. P. Marki : https://github.com/scala/scala/pull/5235

              People

              • Assignee:
                moors Adriaan Moors
                Reporter:
                jpretty Jon Pretty
              • Votes:
                47 Vote for this issue
                Watchers:
                28 Start watching this issue

                Dates

                • Created:
                  Updated: