代替特质论证 [英] In lieu of trait arguments

查看:88
本文介绍了代替特质论证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要写的东西:

val alg10 = new GeneticAlgorithm[FlatDesign]
  with PhenotypeSize(10)
  with FlatDesignMutator
  with ChildrenReplaceParents(numChildren=2)
  with TargetFitnessFunction(targetPhenotype)
  with PopulationSize(40)
  with RunLength(numGenerations=100)

换句话说,我想设置很多约束和参数.例如,PhenotypeSize(10)对突变器和适应度函数有影响.抽象类型应该使实现/强制所有这些一致性约束变得容易.当然,特征不能接受参数,因此此代码将无效.

In other words, there are lots of constraints and parameters I'd like to set. For example, PhenotypeSize(10) has implications for the mutator and the fitness function. Abstract types ought to make it easy to implement/enforce all of those consistency constraints. Of course, traits can't take parameters, so this code won't work.

我喜欢构图特质如何使您混合并匹配所需的任何功能.上面的代码可读性强!使用这样的代码,我可以轻松地编写一个循环来尝试算法的笛卡尔积,该笛卡尔积具有很多变化.

I love how composing traits lets you mix and match whatever functionality you need. The code above is so readable! With code like that, I could easily write a loop to try out the Cartesian product of lots and lots of variations to the algorithm.

不过,我一直坚持寻找一种干净的方法来提供这些特征的参数.有什么建议吗?

I'm stuck, though, on finding a clean way to supply the parameters for those traits. Any suggestions?

推荐答案

早期定义在在Scala中编程为预初始化字段"或

Early definitions are described in Programming in Scala as "pre-initialized fields" or 5.1.6 of the spec and have the semantics you'd expect from trait parameters, namely, they are available in constructors.

val alg10 = new { val size = 10 } with GeneticAlgorithm[FlatDesign]
  with PhenotypeSize { val size: Int ; val psize = 2 * size }

这个问题必须是重复的.

This question must be a duplicate.

这篇关于代替特质论证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆