Skip to content

Fix deprecated auto application of () to method invocations#1422

Merged
olabusayoT merged 1 commit intoapache:mainfrom
olabusayoT:daf-2152-auto-application
Feb 6, 2025
Merged

Fix deprecated auto application of () to method invocations#1422
olabusayoT merged 1 commit intoapache:mainfrom
olabusayoT:daf-2152-auto-application

Conversation

@olabusayoT
Copy link
Copy Markdown
Contributor

  • add support for 2.13 scala options
  • add explicit () to some method invocations
  • fix some collections to explicitly used toSeq to return an immutable Seq
  • replace string concat with + to interpolated strings

DAFFODIL-2152

Copy link
Copy Markdown
Member

@stevedlawrence stevedlawrence left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Comment thread build.sbt Outdated
"-Xlint:inaccessible",
"-Xlint:infer-any",
"-Xlint:nullary-unit",
"-Ywarn-unused:imports"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Ywarn-unused:imports is in both 2.12 and 2.13, should that be moved to the above commonOptions?

Copy link
Copy Markdown
Contributor

@mbeckerle mbeckerle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like replacing simple string concatenation by string interpolations.


object ImplicitsSuppressUnusedImportWarning {
def apply() = if (scala.math.random.isNaN()) Assert.impossible()
def apply() = if (scala.math.random().isNaN) Assert.impossible()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the code coverage suppression comments around these.

config.getStringList(XercesValidator.name).asScala
else Seq.empty
XercesValidator.fromFiles(schemaFiles)
XercesValidator.fromFiles(schemaFiles.toSeq)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so what exactly are the rules about when you must have the "()" and when not?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out Scala 2.13 doesn't always complain about all nullary methods

"For reasons of backwards compatibility, Scala 3 for the moment also auto-inserts () for nullary methods that are defined in Scala 2, or that override a method defined in Scala 2."

Which toSeq seems to fall under, shall I add it to toSeq?

https://docs.scala-lang.org/scala3/reference/dropped-features/auto-apply.html


private val scalaEnums = {
val scalaEnumValues = allEnumerationValues.map { e => e.head.toUpper + e.tail }
val scalaEnumValues = allEnumerationValues.map { e => s"${e.head.toUpper}${e.tail}" }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are just concatenating strings then using "+" is clearer than the string interpolation which I must read very carefully to see if a whitespace is in there, or some other small characters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Char + is conctatenation to a string is deprecated in 2.13, and string interpolation was the recommended fix from Intellij, we can either do that or convert the char to a string before using +. Though I prefer string interpolation personally

https://docs.scala-lang.org/scala3/guides/migration/incompat-dropped-features.html#any2stringadd-conversion

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gaaak. A String is a Seq[Char]. Appending T to Seq[T] at the front of the sequence is one of the most basic operations in functional programming. It is cons from Lisp.

Perhaps + is the wrong operator here? Maybe these should be the actual cons operator i.e., is that char :: string or char +: string ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But to your point sticking with string concatenation will look something like

e/head.toUpper.toString + e.tail

Is that preferable?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling toString is not, But e.head.toUpper +: e.tail is preferable to the string interpolation.

) {
private val nodeName = (simpleTypeNode \@ "name").stripPrefix("Tunable")
private val scalaType = nodeName.head.toUpper + nodeName.tail
private val scalaType = s"${nodeName.head.toUpper}${nodeName.tail}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string concat with + was clearer.


private val values = {
val scalaEnumValues = allEnumerationValues.map { e => e.head.toUpper + e.tail }
val scalaEnumValues = allEnumerationValues.map { e => s"${e.head.toUpper}${e.tail}" }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer string concat not be done by string interpolations.

Copy link
Copy Markdown
Contributor

@mbeckerle mbeckerle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 suggestions.

Plus, you also should suppress those codeCov things with their "COVERAGE-OFF" comment stuff.

} else {
val defaultSeq =
trimmedDefault.split("\\s+").map(d => s"${listType}.${s"${d.head.toUpper}${d.tail}"}")
trimmedDefault.split("\\s+").map(d => s"${listType}.${d.head.toUpper +: d.tail}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, this one is not just concatenating two strings.
So in this case a single string interpolation is the right thing.

s"${listType}.${d.head.toUpper}${d.tail}"

val outDir = new java.io.File(rootDir + "/" + pkg.split('.').reduceLeft(_ + "/" + _))
outDir.mkdirs()
val outPath = s"$outDir/$filename"
val outPath = String.valueOf(outDir) + "/" + filename
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not just concatenating two strings, so I think this is better as a string interpolation, particularly as it avoids the String.valueOf call.

- add support for 2.13 scala options
- add explicit () to some method invocations
- fix some collections to explicitly used toSeq to return an immutable Seq
- replace string concat with + with interpolated strings or char prepend where appropriate
- add coverage comments

DAFFODIL-2152
@olabusayoT olabusayoT force-pushed the daf-2152-auto-application branch from e5c12e0 to 4b7e02b Compare February 6, 2025 22:27
@olabusayoT olabusayoT merged commit f767d83 into apache:main Feb 6, 2025
@olabusayoT olabusayoT deleted the daf-2152-auto-application branch February 6, 2025 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants