ensuring correct function invocation when multiple parameters of same type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ensuring correct function invocation when multiple parameters of same type

najwa4
This post has NOT been accepted by the mailing list yet.
luar biasa, akhirnya saya bisa membetulkan fungsi multiple parameters
Belilah bermacam-macam mikroskop digital baik jenis mikroskop laboratorium monokuler atau binokuler harga murah di Alatkesehatan.id
Loading...