在 Scala 中设置隐式函数 return
Set an implicit function return in Scala
我正在编写 DSL(我的代码的领域特定语言)。
代码是:
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
}
}
如您所见定义 ofSize
modeling
withParameters
具有相同的 return 类型,因此是否可以隐式定义它们的 return 类型并在没有 return 部分的情况下定义这些函数?
请帮助我,我想尽可能地减少代码。
我也想知道是否可以在此代码中使用 case class
、 concise lambda syntax
以进一步最小化它。
您可以简单地删除 return 类型,它将自动推断:
def ofSize(implicit size: Int) = {
neuronGroup.get.setNumberOfNeurons(size)
this
}
我正在编写 DSL(我的代码的领域特定语言)。 代码是:
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
}
}
如您所见定义 ofSize
modeling
withParameters
具有相同的 return 类型,因此是否可以隐式定义它们的 return 类型并在没有 return 部分的情况下定义这些函数?
请帮助我,我想尽可能地减少代码。
我也想知道是否可以在此代码中使用 case class
、 concise lambda syntax
以进一步最小化它。
您可以简单地删除 return 类型,它将自动推断:
def ofSize(implicit size: Int) = {
neuronGroup.get.setNumberOfNeurons(size)
this
}