Skip to content

Commit 5649570

Browse files
authored
Merge pull request #9988 from dotty-staging/move-commandlineparser-fromstring
Move s.u.FromString to s.u.CommandLineParser.FromString.
2 parents b5a1715 + 854877b commit 5649570

File tree

8 files changed

+54
-56
lines changed

8 files changed

+54
-56
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 =

tests/semanticdb/expect/toplevel.expect.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ extension (x/*<-_empty_::toplevel$package.extension_combine().(x)*/: Int/*->scal
33
def combine/*<-_empty_::toplevel$package.combine().*/(x/*<-_empty_::toplevel$package.combine().(x)*/: Int/*->scala::Int#*/, y/*<-_empty_::toplevel$package.combine().(y)*/: Int/*->scala::Int#*/, z/*<-_empty_::toplevel$package.combine().(z)*/: Int/*->scala::Int#*/) = x/*->_empty_::toplevel$package.combine().(x)*/ +/*->scala::Int#`+`(+4).*/ y/*->_empty_::toplevel$package.combine().(y)*/ +/*->scala::Int#`+`(+4).*/ z/*->_empty_::toplevel$package.combine().(z)*/
44
def combine/*<-_empty_::toplevel$package.combine(+1).*/ = 0
55
def foo/*<-_empty_::toplevel$package.foo().*/ = "foo"
6-
/*<-_empty_::MyProgram#*//*->_empty_::toplevel$package.MyProgram().*//*->scala::util::CommandLineParser.parseArgument().*//*->_empty_::MyProgram#main().(args)*//*->scala::util::FromString.given_FromString_Int.*//*->scala::util::CommandLineParser.showError().*//*->local0*/@main/*->scala::main#*/ def MyProgram/*<-_empty_::toplevel$package.MyProgram().*/(times/*<-_empty_::toplevel$package.MyProgram().(times)*/: Int/*->scala::Int#*/): Unit/*->scala::Unit#*/ = (/*->scala::LowPriorityImplicits#intWrapper().*/1 to/*->scala::runtime::RichInt#to().*/ times/*->_empty_::toplevel$package.MyProgram().(times)*/) foreach/*->scala::collection::immutable::Range#foreach().*/ (_ => println/*->scala::Predef.println(+1).*/("hello"))
6+
/*<-_empty_::MyProgram#*//*->_empty_::toplevel$package.MyProgram().*//*->scala::util::CommandLineParser.parseArgument().*//*->_empty_::MyProgram#main().(args)*//*->scala::util::CommandLineParser.FromString.given_FromString_Int.*//*->scala::util::CommandLineParser.showError().*//*->local0*/@main/*->scala::main#*/ def MyProgram/*<-_empty_::toplevel$package.MyProgram().*/(times/*<-_empty_::toplevel$package.MyProgram().(times)*/: Int/*->scala::Int#*/): Unit/*->scala::Unit#*/ = (/*->scala::LowPriorityImplicits#intWrapper().*/1 to/*->scala::runtime::RichInt#to().*/ times/*->_empty_::toplevel$package.MyProgram().(times)*/) foreach/*->scala::collection::immutable::Range#foreach().*/ (_ => println/*->scala::Predef.println(+1).*/("hello"))

tests/semanticdb/metac.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3973,7 +3973,7 @@ Occurrences:
39733973
[5:0..5:0): -> _empty_/toplevel$package.MyProgram().
39743974
[5:0..5:0): -> scala/util/CommandLineParser.parseArgument().
39753975
[5:0..5:0): -> _empty_/MyProgram#main().(args)
3976-
[5:0..5:0): -> scala/util/FromString.given_FromString_Int.
3976+
[5:0..5:0): -> scala/util/CommandLineParser.FromString.given_FromString_Int.
39773977
[5:0..5:0): -> scala/util/CommandLineParser.showError().
39783978
[5:0..5:0): -> local0
39793979
[5:1..5:5): main -> scala/main#

0 commit comments

Comments
 (0)