ensuring correct function invocation when multiple parameters of same type

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

ensuring correct function invocation when multiple parameters of same type

Colin Bester
With Scala 2.11x

I have a function with multiple parameters of same type and what ensure correct usage (order) of parameters.

For example with function
def someFunction(sort: Bson, limit:Bson)

how does one ensure that someFunction is always called correctly, i.e. sort parameter is forced to be first in parameter call by calling function?

Can't allow

val sort = ...
val limit
= ...


val res
= someFunction(limit, sort)

Careful coding and specifying parameter names can help, but this is dependent on person writing code

val res = someFunction(limit=limit, sort=sort)

I was thinking of using value classes

case class SortBy(underlying: Bson) extends AnyVal
case class LimitTo(underlying: Bson) extends AnyVal

def someFunction(sort: SortBy, limit: LimitTo) {}



val sort
= SortBy(...)
val limit
= LimitTo(...)


val res
= someFunction(sort, limit)


val res
= someFunction(limit, sort) //should fail compile

Any other suggestions?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.