Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using LocalDate as an argument to main in Scala 3

Tags:

scala

scala-3

I have the following code

object Ex13:

    import java.time.LocalDate
    import scala.util.CommandLineParser.FromString

    given FromString[LocalDate] with
      def fromString( s: String ): LocalDate =
        LocalDate.parse( s )

    case class A( x: Int )

    given FromString[A] with
      def fromString( s: String ): A = A( s.toInt )

    @main
    def mainEx13( arg: A ): Unit =
      println( s"$arg" )

      val date: LocalDate =
        summon[FromString[LocalDate]].fromString( "2002-10-10" )
      println( date )

And run scala run Ex13.scala -- 100.

The result is as expected:

A(100)
2002-10-10

This means that the compiler: 1) detects a given instance of type FromString[A], 2) uses it to parse the String 100 and convert it to an A object, and 3) detects a given instance of type FromString[LocalDate]. The latter is due to the fact that the date variable receives a value of type LocalDate.

However, if I change the argument of mainEx13 from A to LocalDate, i.e. mainEx13( arg: LocalDate ) and execute scala run Ex13.scala -- 2025-03-01 the result is:

Compiling project (Scala 3.6.3, JVM (13))
  [error] ./Ex13.scala:15:3
  [error] No given instance of type scala.util.CommandLineParser.FromString[java.time.LocalDate] was found for parameter fs of method parseArgument in object CommandLineParser
  [error]   @main
  [error]   ^
  Error compiling project (Scala 3.6.3, JVM (13))
  Compilation failed

I don't see why the error is occurring since there is indeed an instance of FromString[java.time.LocalDate]. Any idea what's happening?

Note. I have done some further research and found out that no errors occur with this code:

import scala.util.CommandLineParser.FromString
import java.time.LocalDate

given FromString[LocalDate] with
    def fromString( s: String ): LocalDate =
        LocalDate.parse( s )
        
object Ex13:
    ...

That is, when I put the given FromString[LocalDate] outside the Ex13 object.

But now the question is: why inside object Ex13 the given FromString[A] is detected but not the given FromString[LocalDate]?

like image 259
flafora Avatar asked Oct 16 '25 13:10

flafora


1 Answers

sarveshseri is right (pls read his comment above). Pity that I read his comment a bit late. I post code as an example where Java instances must be wrapped.

    object Ex13:
        given FromString[LocalDate] with // no way !
            def fromString(s: String): LocalDate =
              LocalDate.parse(s)

        type OptLocalDate = Option[LocalDate]
        given FromString[OptLocalDate] with  // no way !
            def fromString(s: String) =
              Option(LocalDate.parse(s))

        case class Wrap[T](x: T)

        // must create a type because of clashing :  since given
        // FromString[Wrap[<whatever>] would produce  given_String_Wrap

        type WrapLocalDate = Wrap[LocalDate]
        given FromString[WrapLocalDate] with // works
          def fromString(s: String) = Wrap(given_FromString_LocalDate.fromString(s))

        given FromString[File] with  // no way !
          def fromString(s: String) = new File(s)

        type WrapFile = Wrap[File]
        given FromString[WrapFile] with // works
          def fromString(s: String) = Wrap(given_FromString_File.fromString(s))

        given FromString[Integer] with // no way !
          def fromString(s: String) = s.toInt.asInstanceOf[Integer]
        // ...

        @main
        // arg as Wrap[<defined>] or Wrap<defined> type
        def mainEx13(arg: Wrap[File]): Unit =
            println(s"$arg")

Scala 3.6.3
like image 180
Volty De Qua Avatar answered Oct 19 '25 09:10

Volty De Qua



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!