Can extractors be customized with parameters in the body of a case statement (or anywhere else that an extractor would be used)?

前端 未结 5 1245
情书的邮戳
情书的邮戳 2020-12-06 10:15

Basically, I would like to be able to build a custom extractor without having to store it in a variable prior to using it.

This isn\'t a real example of how I would

5条回答
  •  忘掉有多难
    2020-12-06 10:52

    Though what you are asking isn't directly possible,
    it is possible to create an extractor returning a contaner
    that gets evaluated value in the if-part of the case evaluation. In the if part it
    is possible to provide parameters.

    object DateExtractor {
      def unapply(in: String): Option[DateExtractor] = Some(new DateExtractor(in));
    }
    
    class DateExtractor(input:String){
      var value:LocalDate=null;
      def apply():LocalDate = value;
      def apply(format: String):Boolean={
        val formater=DateTimeFormatter.ofPattern(format);
        try{
          val parsed=formater.parse(input, TemporalQueries.localDate());
          value=parsed
          true;
        } catch {
          case e:Throwable=>{
            false
          }
        }
      }
    }
    

    Usage:

    object DateExtractorUsage{
      def main(args: Array[String]): Unit = {
        "2009-12-31" match {
          case DateExtractor(ext) if(ext("dd-MM-yyyy"))=>{
            println("Found dd-MM-yyyy date:"+ext())
          }
          case DateExtractor(ext) if(ext("yyyy-MM-dd"))=>{
            println("Found yyyy-MM-dd date:"+ext())
          }
          case _=>{
            println("Unable to parse date")
          }
        }
      }
    }
    

    This pattern preserves the PartialFunction nature of the piece of code.
    I find this useful since I am quite a fan of the collect/collectFirst methods, which take a partial function as a parameter and typically does not leave room for precreating a set of extractors.

提交回复
热议问题