Skip to content

Commit 589acad

Browse files
committed
Move s.u.FromString to s.u.CommandLineParser.FromString.
There are many competing ways to parse things. `FromString` is tailored for command-line parsing. As such we should not expose it with a general name like `scala.util.FromString`. Instead, we move it inside `CommandLineParser` to make it clear that it is meant for command-line parsing.
1 parent 2f5b5fd commit 589acad

File tree

6 files changed

+52
-54
lines changed

6 files changed

+52
-54
lines changed

library/src/scala/util/CommandLineParser.scala

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,49 @@ object CommandLineParser {
4141
else s" after ${err.idx} arguments"
4242
println(s"Illegal command line$where: ${err.msg}")
4343
}
44+
45+
trait FromString[T] {
46+
/** Can throw java.lang.IllegalArgumentException */
47+
def fromString(s: String): T
48+
49+
def fromStringOption(s: String): Option[T] =
50+
try Some(fromString(s))
51+
catch {
52+
case ex: IllegalArgumentException => None
53+
}
54+
}
55+
56+
object FromString {
57+
given FromString[String] {
58+
def fromString(s: String) = s
59+
}
60+
61+
given FromString[Boolean] {
62+
def fromString(s: String) = s.toBoolean
63+
}
64+
65+
given FromString[Byte] {
66+
def fromString(s: String) = s.toByte
67+
}
68+
69+
given FromString[Short] {
70+
def fromString(s: String) = s.toShort
71+
}
72+
73+
given FromString[Int] {
74+
def fromString(s: String) = s.toInt
75+
}
76+
77+
given FromString[Long] {
78+
def fromString(s: String) = s.toLong
79+
}
80+
81+
given FromString[Float] {
82+
def fromString(s: String) = s.toFloat
83+
}
84+
85+
given FromString[Double] {
86+
def fromString(s: String) = s.toDouble
87+
}
88+
}
4489
}

library/src/scala/util/FromString.scala

Lines changed: 0 additions & 47 deletions
This file was deleted.

tests/neg/main-functions2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Foo {
88

99
@main def g(x: Int*)(y: Int*) = () // error: varargs parameter of @main method must come last
1010

11-
@main def h[T: util.FromString](x: T) = () // error: @main method cannot have type parameters
11+
@main def h[T: util.CommandLineParser.FromString](x: T) = () // error: @main method cannot have type parameters
1212

1313
@main def i(x: Int)(using Foo) = () // error: @main method cannot have implicit parameters

tests/pos/main-method-scheme-class-based.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import collection.mutable
1313
*/
1414
trait MainAnnotation extends StaticAnnotation:
1515

16-
/** The class used for argument string parsing. E.g. `scala.util.FromString`,
16+
/** The class used for argument string parsing. E.g. `scala.util.CommandLineParser.FromString`,
1717
* but could be something else
1818
*/
1919
type ArgumentParser[T]
@@ -44,7 +44,7 @@ end MainAnnotation
4444

4545
class main extends MainAnnotation:
4646

47-
type ArgumentParser[T] = util.FromString[T]
47+
type ArgumentParser[T] = util.CommandLineParser.FromString[T]
4848
type MainResultType = Any
4949

5050
def command(args: Array[String]): Command = new Command:
@@ -202,4 +202,4 @@ Error: invalid argument for num: binary
202202
Error: unused argument: 01
203203
Usage: add num inc?
204204
205-
*/
205+
*/

tests/pos/main-method-scheme.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class main(progName: String, args: Array[String], docComment: String) extends Ma
2222

2323
def this() = this("", Array(), "")
2424

25-
type ArgumentParser[T] = util.FromString[T]
25+
type ArgumentParser[T] = util.CommandLineParser.FromString[T]
2626

2727
/** A buffer of demanded argument names, plus
2828
* "?" if it has a default
@@ -167,4 +167,4 @@ Error: unknown argument name: --foo
167167
Usage: add num inc?
168168
Adds two numbers
169169
170-
*/
170+
*/

tests/run/decorators/main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import collection.mutable
55
*/
66
class main extends EntryPoint.Annotation:
77

8-
type ArgumentParser[T] = util.FromString[T]
8+
type ArgumentParser[T] = util.CommandLineParser.FromString[T]
99
type EntryPointResult = Unit
1010

1111
inline def wrapperName(entryPointName: String): String =

0 commit comments

Comments
 (0)