From 103cbe78b2df37b92be2c6016c5abda486b36bb2 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 9 Feb 2024 19:04:56 +0100 Subject: [PATCH] Make os.Path and os.RelPath implement CharSequence Having os.Path and os.RelPath, in fact os.FilePath, implement CharSequence reduces the boilerplate when using functions that take a input collection and produce some form of textual output. Consider the following example def printBashArray(out: Appendable, name: String, content: Seq[CharSequence]) = out.append(f"${name}=(\n") for c <- content do out.append(f"\t${c}\n") out.append(")\n") and val fooPaths: Seq[os.RelPath] Currently we have to printBashArray(out, "FOO_PATHS", fooPaths.map(_.toString)) after this change, we can simply printBashArray(out, "FOO_PATHS", fooPaths) (Of course, we could also declare 'content' as Seq[Any], but this blurres the intention.) --- os/src/Path.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/os/src/Path.scala b/os/src/Path.scala index d9fb0554..9ad92046 100644 --- a/os/src/Path.scala +++ b/os/src/Path.scala @@ -234,9 +234,13 @@ object PathError { * relative [[RelPath]], and can be constructed from a * java.nio.file.Path or java.io.File */ -sealed trait FilePath extends BasePath { +sealed trait FilePath extends BasePath with CharSequence { def toNIO: java.nio.file.Path def resolveFrom(base: os.Path): os.Path + + override def charAt(index: Int): Char = toString().charAt(index) + override def subSequence(start: Int, end: Int): CharSequence = toString().subSequence(start, end) + override def length(): Int = toString.length() } object FilePath {