MiraiForum

    • Register
    • Login
    • Search
    • Popular
    • Recent
    • Unsolved
    • Tags
    • Groups
    • 友情链接

    Solved 一个kotlin基础问题

    开发交流
    kotlin plugin
    4
    9
    175
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • xiao_zheng
      xiao_zheng 摸鱼 last edited by xiao_zheng

      在开发插件时,想写一个函数传入正则字符串和代码块 ,但是报错了

      1 Reply Last reply Reply Quote 0
      • J
        Jaqi last edited by

        上代码,上截图

        xiao_zheng 1 Reply Last reply Reply Quote 0
        • xiao_zheng
          xiao_zheng 摸鱼 @Jaqi last edited by

          @jaqi

          fun reg(regex: String,block:()->Unit) {
            if (regex.toRegex().matches(it.contentToString())) {
              val list = Regex(regex).findAll(it.contentToString()).toList().flatMap(MatchResult::groupValues)
                block(list)
              }
            }
          reg("""#?\s*ping(\d{0,2}|100)?""") {
            group.sendMessage("正则1 ${list[1]}")
          }
          
          1 Reply Last reply Reply Quote 0
          • N
            Nepsyn last edited by

            异步调用写进同步方法体了

            xiao_zheng 1 Reply Last reply Reply Quote 1
            • xiao_zheng
              xiao_zheng 摸鱼 @Nepsyn last edited by

              @nepsyn 啊,那该怎么改呢

              N 1 Reply Last reply Reply Quote 0
              • Karlatemp
                Karlatemp last edited by

                每个变量都有作用域,你需要将 list 作为 lambda block 的一个参数传递过去

                
                fun a(b: (List<String>)->Unit) {
                    b(mutableListOf())
                }
                
                a { argx -> 
                    println(argx)
                }
                
                xiao_zheng 1 Reply Last reply Reply Quote 1
                • N
                  Nepsyn @xiao_zheng last edited by Nepsyn

                  suspend fun reg(regex: String, block: suspend (List<String>) -> Unit) {
                      if (regex.toRegex().matches(it.contentToString())) {
                          val list = Regex(regex).findAll(it.contentToString()).toList().flatMap(MatchResult::groupValues)
                          block(list)
                      }
                  }
                  
                  reg("""#?\s*ping(\d{0,2}|100)?""") { list ->
                      group.sendMessage("正则1 ${list[1]}")
                  }
                  
                  xiao_zheng 1 Reply Last reply Reply Quote 1
                  • xiao_zheng
                    xiao_zheng 摸鱼 @Nepsyn last edited by

                    @nepsyn 感谢!

                    1 Reply Last reply Reply Quote 0
                    • Topic has been marked as solved  xiao_zheng xiao_zheng 
                    • xiao_zheng
                      xiao_zheng 摸鱼 @Karlatemp last edited by

                      @karlatemp 感谢!

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      • First post
                        Last post
                      Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact