Skip to content

Add Array.from extension for scala 2.11 and 2.12#658

Merged
SethTisue merged 3 commits intoscala:mainfrom
RustedBones:array-extensions
Sep 17, 2024
Merged

Add Array.from extension for scala 2.11 and 2.12#658
SethTisue merged 3 commits intoscala:mainfrom
RustedBones:array-extensions

Conversation

@RustedBones
Copy link
Copy Markdown

Without the extension, code fails to compile for scala 2.11 and 2.12 with

 value from is not a member of object Array
     val a = Array.from(xs)

Add missing extension to the compat package

@SethTisue
Copy link
Copy Markdown
Member

review by @scala/collections ?


class ArrayExtensions(private val fact: Array.type) extends AnyVal {
def from[A: ClassTag](source: TraversableOnce[A]): Array[A] =
fact.apply(source.toSeq: _*)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

source match {
case it: Iterable[A] => it.toArray[A]
case _ => it.iterator.toArray[A]
}

new RandomExtensions(self)
}

class ArrayExtensions(private val fact: Array.type) extends AnyVal {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

final class?

@SethTisue SethTisue merged commit e566811 into scala:main Sep 17, 2024
@SethTisue
Copy link
Copy Markdown
Member

Thank you!

@RustedBones RustedBones deleted the array-extensions branch September 17, 2024 14:05
@SethTisue
Copy link
Copy Markdown
Member

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