Vararg constuctor arguments


#1

I have just started with Kotlin and I am doing following

public class SQLQuery(val conn: Connection, val query: String, vararg params: Any): Query{

  //Prepared SQL Statement
  var statement: PreparedStatement? = null
  get(){
           $statement ?: buildStatement(query,params)
  }

  // Prepare statement from connection and set parameters
  private fun buildStatement(query: String,vararg params: Any): Statement {
  //…
  }
}

I am not able to access the params in the getter for statement.  Is there a way to make params as val in the class def.


#2

In class constructor you need to add varval. It will create field automatic. Or make it by yourself.

class Foo(vararg val params: String) {   fun toString(): String = params.fold("Args:") { a, b -> "$a $b" } }

class Bar(vararg params: String) {
  val args = params
  fun toString(): String = args.fold(“Args:”) { a, b -> “$a $b” }
}

fun main(args : Array<String>) {
  println(Foo(“First”,“Second”, “Last”))   // Args: First Second Last
  println(Bar(“First”,“Second”, “Last”))   // Args: First Second Last
}

Look here for more info http://confluence.jetbrains.net/display/Kotlin/Classes+and+Inheritance