Skip to content

Move s.u.FromString to s.u.CommandLineParser.FromString. #9988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions library/src/scala/util/CommandLineParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,49 @@ object CommandLineParser {
else s" after ${err.idx} arguments"
println(s"Illegal command line$where: ${err.msg}")
}

trait FromString[T] {
/** Can throw java.lang.IllegalArgumentException */
def fromString(s: String): T

def fromStringOption(s: String): Option[T] =
try Some(fromString(s))
catch {
case ex: IllegalArgumentException => None
}
}

object FromString {
given FromString[String] {
def fromString(s: String) = s
}

given FromString[Boolean] {
def fromString(s: String) = s.toBoolean
}

given FromString[Byte] {
def fromString(s: String) = s.toByte
}

given FromString[Short] {
def fromString(s: String) = s.toShort
}

given FromString[Int] {
def fromString(s: String) = s.toInt
}

given FromString[Long] {
def fromString(s: String) = s.toLong
}

given FromString[Float] {
def fromString(s: String) = s.toFloat
}

given FromString[Double] {
def fromString(s: String) = s.toDouble
}
}
}
47 changes: 0 additions & 47 deletions library/src/scala/util/FromString.scala

This file was deleted.

2 changes: 1 addition & 1 deletion tests/neg/main-functions2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class Foo {

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

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

@main def i(x: Int)(using Foo) = () // error: @main method cannot have implicit parameters
6 changes: 3 additions & 3 deletions tests/pos/main-method-scheme-class-based.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import collection.mutable
*/
trait MainAnnotation extends StaticAnnotation:

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

class main extends MainAnnotation:

type ArgumentParser[T] = util.FromString[T]
type ArgumentParser[T] = util.CommandLineParser.FromString[T]
type MainResultType = Any

def command(args: Array[String]): Command = new Command:
Expand Down Expand Up @@ -202,4 +202,4 @@ Error: invalid argument for num: binary
Error: unused argument: 01
Usage: add num inc?

*/
*/
4 changes: 2 additions & 2 deletions tests/pos/main-method-scheme.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class main(progName: String, args: Array[String], docComment: String) extends Ma

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

type ArgumentParser[T] = util.FromString[T]
type ArgumentParser[T] = util.CommandLineParser.FromString[T]

/** A buffer of demanded argument names, plus
* "?" if it has a default
Expand Down Expand Up @@ -167,4 +167,4 @@ Error: unknown argument name: --foo
Usage: add num inc?
Adds two numbers

*/
*/
2 changes: 1 addition & 1 deletion tests/run/decorators/main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import collection.mutable
*/
class main extends EntryPoint.Annotation:

type ArgumentParser[T] = util.FromString[T]
type ArgumentParser[T] = util.CommandLineParser.FromString[T]
type EntryPointResult = Unit

inline def wrapperName(entryPointName: String): String =
Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/toplevel.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ extension (x/*<-_empty_::toplevel$package.extension_combine().(x)*/: Int/*->scal
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)*/
def combine/*<-_empty_::toplevel$package.combine(+1).*/ = 0
def foo/*<-_empty_::toplevel$package.foo().*/ = "foo"
/*<-_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"))
/*<-_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"))
2 changes: 1 addition & 1 deletion tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -3973,7 +3973,7 @@ Occurrences:
[5:0..5:0): -> _empty_/toplevel$package.MyProgram().
[5:0..5:0): -> scala/util/CommandLineParser.parseArgument().
[5:0..5:0): -> _empty_/MyProgram#main().(args)
[5:0..5:0): -> scala/util/FromString.given_FromString_Int.
[5:0..5:0): -> scala/util/CommandLineParser.FromString.given_FromString_Int.
[5:0..5:0): -> scala/util/CommandLineParser.showError().
[5:0..5:0): -> local0
[5:1..5:5): main -> scala/main#
Expand Down