Skip to content
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

consider revert #6755 because maybe Char + String is more efficient. #11025

Closed
xuwei-k opened this issue Jul 16, 2018 · 5 comments
Closed

consider revert #6755 because maybe Char + String is more efficient. #11025

xuwei-k opened this issue Jul 16, 2018 · 5 comments

Comments

@xuwei-k
Copy link

xuwei-k commented Jul 16, 2018

build.sbt

resolvers += "scala-pr" at "https://scala-ci.typesafe.com/artifactory/scala-integration/"

scalaVersion := "2.13.0-pre-4d39d4f"

scalacOptions += "-deprecation"

A.scala

class A {
  def x = 'a' + "b"
  def y = 'c'.toString + "d"
}

sbt compile log

A.scala:2:15: method + in class Char is deprecated (since 2.13.0): Adding a number and a String is deprecated. Convert the number to a String with `toString` first to call +
[warn]   def x = 'a' + "b"
[warn]               ^
[warn] one warning found

javap -v target/scala-2.13.0-pre-4d39d4f/classes/A.class

  public java.lang.String x();
    descriptor: ()Ljava/lang/String;
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=1, args_size=1
         0: new           #12                 // class java/lang/StringBuilder
         3: dup
         4: ldc           #13                 // int 12
         6: invokespecial #17                 // Method java/lang/StringBuilder."<init>":(I)V
         9: bipush        97
        11: invokevirtual #21                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
        14: ldc           #23                 // String b
        16: invokevirtual #26                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        19: invokevirtual #29                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        22: areturn
      LineNumberTable:
        line 2: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      23     0  this   LA;

  public java.lang.String y();
    descriptor: ()Ljava/lang/String;
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=1, args_size=1
         0: new           #12                 // class java/lang/StringBuilder
         3: dup
         4: ldc           #33                 // int 1
         6: invokespecial #17                 // Method java/lang/StringBuilder."<init>":(I)V
         9: bipush        99
        11: invokestatic  #39                 // Method scala/runtime/BoxesRunTime.boxToCharacter:(C)Ljava/lang/Character;
        14: invokevirtual #40                 // Method java/lang/Object.toString:()Ljava/lang/String;
        17: invokevirtual #26                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        20: ldc           #42                 // String d
        22: invokevirtual #26                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        25: invokevirtual #29                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        28: areturn
      LineNumberTable:
        line 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      29     0  this   LA;
@sjrd
Copy link
Member

sjrd commented Jul 16, 2018

Write

def x = "" + 'a' + "b"

instead.

@smarter
Copy link
Member

smarter commented Jul 16, 2018

Or just write "ab" :) In less trivial cases use the s"" interpolator which is optimlzed since 2.12.5:

def foo(a: Char) = s"${a}b"
  public java.lang.String foo(char);
    descriptor: (C)Ljava/lang/String;
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=2
         0: new           #18                 // class java/lang/StringBuilder
         3: dup
         4: ldc           #19                 // int 1
         6: invokespecial #23                 // Method java/lang/StringBuilder."<init>":(I)V
         9: iload_1
        10: invokevirtual #27                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
        13: ldc           #29                 // String b
        15: invokevirtual #32                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        18: invokevirtual #36                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        21: areturn

@xuwei-k
Copy link
Author

xuwei-k commented Jul 16, 2018

Should we recommend the s"" interpolator instead of Char#toString in deprecation message?

@dwijnand
Copy link
Member

I think that's an excellent idea.

@SethTisue
Copy link
Member

@exoego if you accept my invite to https://github.com/orgs/scala/teams/contributors/members, and comment here saying you did, I'll assign you this ticket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants