Set an implicit function return in Scala

时间:2018-06-18 11:37:14

标签: scala dsl

I am writing DSL (domain specific language for my code). The code is:

class NeuronGroupDSL(identifier: String)(implicit network: N2S3SimulationDSL ) // declared values which must be provided by the user, [we can just give a default constructor to this]
 {
  implicit val sizeDefault : Int = 28 // added the implicit default value for the setNumberOfNeurons method
  val id: String = identifier // We can add a default constructor with the Id and the network to this too allow user to skip this part
  var neuronGroup: Option[NeuronGroupRef] = None // this variable stores all the information by using the get property and a associated function with it
  private var neuronParameters: Seq[(Property[_], _)] = Seq()
  private var neuronModel: NeuronModel = _

/**Properties of the NeuronGroupDSL **/
// Added a default size to the size variable
  def ofSize(implicit size: Int): NeuronGroupDSL = {
    this.neuronGroup.get.setNumberOfNeurons(size)
    this
  }
// Give user the opportunity to choose the type of available models by the help of pattern matching
/**Neuron Model:
1. Linear leaky integrate-and-fire model
2. FitzHugh-Nagumo model
3. Izhikevich model
**/
  def modeling(model: NeuronModel): NeuronGroupDSL = {
    this.neuronModel = model
    this.neuronGroup.get.setNeuronModel(model, this.neuronParameters)
    this
  }
//we can define a default property the neuron group model here
  def withParameters(properties: (Property[_], _)*): NeuronGroupDSL = {
    this.neuronParameters = properties
    this.neuronGroup.get.setNeuronModel(this.neuronModel, properties)
    this
  }
}

As you can see the definitions ofSize modeling withParameters have the same return type, so is it possible to implicitly define their return type and define these functions without their return part ?

Please help me I want to minimize the code as much as possible.

I too want to know that is it possible to use case class , concise lambda syntax in this code to further minimize it.

1 个答案:

答案 0 :(得分:0)

您可以简单地删除返回类型,它将自动推断:

def ofSize(implicit size: Int) = {
  neuronGroup.get.setNumberOfNeurons(size)
  this
}