Scala: Constructor taking either Seq or varargs

前端 未结 3 1254
鱼传尺愫
鱼传尺愫 2021-01-01 15:43

I am guessing that, for compatibility reasons, the type of vararg parameters Any* is Array[Any] - please correct this if I\'m wrong. However, this does not expl

3条回答
  •  盖世英雄少女心
    2021-01-01 15:54

    I suppose that you would like to make the method calls prettier and so explicit calling with _* is not an option. In that case you may solve the problem with method overloading.

    class Api(api_url: String, params: Seq[(String, String)]) {
      def this(api_url: String, param : (String, String), params: (String, String)*)
        = this(api_url, param +: params)
      def this(api_url: String)
        = this(api_url, Seq())
    }
    

提交回复
热议问题