ensuring correct function invocation when multiple parameters of same type

82 views
Skip to first unread message

Colin Bester

unread,
Jul 31, 2017, 10:57:18 AM7/31/17
to scala-user
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?
Reply all
Reply to author
Forward
0 new messages