Accessing a Class Member from a First-Class Function
Posted
by dbyrne
on Stack Overflow
See other posts from Stack Overflow
or by dbyrne
Published on 2010-05-18T19:03:05Z
Indexed on
2010/05/18
19:40 UTC
Read the original article
Hit count: 274
I have a case class which takes a list of functions:
case class A(q:Double, r:Double, s:Double, l:List[(Double)=>Double])
I have over 20 functions defined. Some of these functions have their own parameters, and some of them also use the q
, r
, and s
values from the case class. Two examples are:
def f1(w:Double) = (d:Double) => math.sin(d) * w
def f2(w:Double, q:Double) = (d:Double) => d * q * w
The problem is that I then need to reference q
, r
, and s
twice when instantiating the case class:
A(0.5, 1.0, 2.0, List(f1(3.0), f2(4.0, 0.5))) //0.5 is referenced twice
I would like to be able to instantiate the class like this:
A(0.5, 1.0, 2.0, List(f1(3.0), f2(4.0))) //f2 already knows about q!
What is the best technique to accomplish this? Can I define my functions in a trait that the case class extends?
EDIT: The real world application has 7 members, not 3. Only a small number of the functions need access to the members. Most of the functions don't care about them.
© Stack Overflow or respective owner