- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Passing associative parameters to a method?
Hi All
I have a slightly weird thought that I would like to get your opinion
on. Please consider a use case of sending emails as an example. Lets
say I have a "send" method on a Email class. Now the method signature
could look like:
def send(template:String, params: Any*) { }
where template is the name/path of the template I would want to use
and params are the objects that should be bound on the template for
generating the email text. I would need to know the names of the
variables in the template that each of these parameters would be bound
to, so I could either infer through some way, or I could instead pass
a Map to the method:
def send(template:String, params: Map[String, Any]) { }
This will require the client code to build a map before/ while it
calls the method (nothing wrong with it as far as I can see!). But, it
would be really nice to instead have the Map implicitly inferred with
the client code calling it as follows:
email.send(template, p1 -> v1, p2 -> v2, p3 -> v3)
where p1, p2, p3 are attribute names and v1, v2 and v3 are their
expected values.
obviously, one could argue that we could write the client code as
email.send(template, Map(p1 -> v1, p2 -> v2, p3 -> v3)) but the call
without the Map looks easier to read to me...
what do you think? or is this anyways supported in some way and I am
not aware (in which case, please excuse my ignorance).
Best regards
Aishwarya










Passing associative parameters to a method?
---------- Forwarded message ----------
From: Ken Scambler <ken [dot] scambler [at] gmail [dot] com>
Date: 23 November 2011 22:46
Subject: Re: [scala-debate] Passing associative parameters to a method?
To: Aishwarya Singhal <asinghal79 [at] gmail [dot] com>
Try this:
def send(template:String, params: (String, Any)*) { }
Ken
On 23 November 2011 22:41, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
Re: Passing associative parameters to a method?
def send(template:String, params: (String,Any)*){ val paramsMap = Map(params:_*) }
On Wed, Nov 23, 2011 at 3:49 AM, Ken Scambler <ken [dot] scambler [at] gmail [dot] com> wrote:
Re: Passing associative parameters to a method?
Or even better:
val paramsMap = params.toMap
The compiler knows that "params" contains 2-tuples!
Re: Passing associative parameters to a method?
Thanks guys, that's awesome! As it turns out, it was just me that was
not aware of this flexibility. And well, huge apologies for shooting
this query on the wrong group!
Best regards
Aishwarya
On Nov 23, 7:37 pm, Dan Shryock wrote:
> You can also convert the Seq[(String,Any)] that you get for params into a
> map quite easily like this:
>
> def send(template:String, params: (String,Any)*){
> val paramsMap = Map(params:_*)
>
> }
>
> On Wed, Nov 23, 2011 at 3:49 AM, Ken Scambler wrote:
>
>
>
>
>
>
>
> > Sorry, forgot to include list. This probably belongs on scala-user, so
> > I'm transferring to there.
>
> > ---------- Forwarded message ----------
> > From: Ken Scambler
> > Date: 23 November 2011 22:46
> > Subject: Re: [scala-debate] Passing associative parameters to a method?
> > To: Aishwarya Singhal
>
> > Try this:
> > def send(template:String, params: (String, Any)*) { }
>
> > Ken
>
> > On 23 November 2011 22:41, Aishwarya Singhal wrote:
>
> >> Hi All
>
> >> I have a slightly weird thought that I would like to get your opinion
> >> on. Please consider a use case of sending emails as an example. Lets
> >> say I have a "send" method on a Email class. Now the method signature
> >> could look like:
>
> >> def send(template:String, params: Any*) { }
>
> >> where template is the name/path of the template I would want to use
> >> and params are the objects that should be bound on the template for
> >> generating the email text. I would need to know the names of the
> >> variables in the template that each of these parameters would be bound
> >> to, so I could either infer through some way, or I could instead pass
> >> a Map to the method:
>
> >> def send(template:String, params: Map[String, Any]) { }
>
> >> This will require the client code to build a map before/ while it
> >> calls the method (nothing wrong with it as far as I can see!). But, it
> >> would be really nice to instead have the Map implicitly inferred with
> >> the client code calling it as follows:
>
> >> email.send(template, p1 -> v1, p2 -> v2, p3 -> v3)
> >> where p1, p2, p3 are attribute names and v1, v2 and v3 are their
> >> expected values.
>
> >> obviously, one could argue that we could write the client code as
> >> email.send(template, Map(p1 -> v1, p2 -> v2, p3 -> v3)) but the call
> >> without the Map looks easier to read to me...
>
> >> what do you think? or is this anyways supported in some way and I am
> >> not aware (in which case, please excuse my ignorance).
>
> >> Best regards
> >> Aishwarya
Re: Re: Passing associative parameters to a method?
-- Cédric
On Wed, Nov 23, 2011 at 7:31 AM, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
Re: Passing associative parameters to a method?
Hi Ken
sure, thought about that too. the calling code would then look like
email.send(template, (p1,v1), (p2,v2), (p3, v3))... too many brackets
IMHO...
best regards
On Nov 23, 4:49 pm, Ken Scambler wrote:
> Sorry, forgot to include list. This probably belongs on scala-user, so I'm
> transferring to there.
>
>
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Ken Scambler
> Date: 23 November 2011 22:46
> Subject: Re: [scala-debate] Passing associative parameters to a method?
> To: Aishwarya Singhal
>
> Try this:
> def send(template:String, params: (String, Any)*) { }
>
> Ken
>
> On 23 November 2011 22:41, Aishwarya Singhal wrote:
>
> > Hi All
>
> > I have a slightly weird thought that I would like to get your opinion
> > on. Please consider a use case of sending emails as an example. Lets
> > say I have a "send" method on a Email class. Now the method signature
> > could look like:
>
> > def send(template:String, params: Any*) { }
>
> > where template is the name/path of the template I would want to use
> > and params are the objects that should be bound on the template for
> > generating the email text. I would need to know the names of the
> > variables in the template that each of these parameters would be bound
> > to, so I could either infer through some way, or I could instead pass
> > a Map to the method:
>
> > def send(template:String, params: Map[String, Any]) { }
>
> > This will require the client code to build a map before/ while it
> > calls the method (nothing wrong with it as far as I can see!). But, it
> > would be really nice to instead have the Map implicitly inferred with
> > the client code calling it as follows:
>
> > email.send(template, p1 -> v1, p2 -> v2, p3 -> v3)
> > where p1, p2, p3 are attribute names and v1, v2 and v3 are their
> > expected values.
>
> > obviously, one could argue that we could write the client code as
> > email.send(template, Map(p1 -> v1, p2 -> v2, p3 -> v3)) but the call
> > without the Map looks easier to read to me...
>
> > what do you think? or is this anyways supported in some way and I am
> > not aware (in which case, please excuse my ignorance).
>
> > Best regards
> > Aishwarya
Re: Re: Passing associative parameters to a method?
On 23 November 2011 22:54, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
You're in luck! a -> b is in fact the very same thing as (a,b). In Predef, any value is implicitly extended to have a -> method, which will yield a pair with the next value.
Cheers,
Ken
Passing associative parameters to a method?
---------- Forwarded message ----------
From: Ken Scambler <ken [dot] scambler [at] gmail [dot] com>
Date: 23 November 2011 22:46
Subject: Re: [scala-debate] Passing associative parameters to a method?
To: Aishwarya Singhal <asinghal79 [at] gmail [dot] com>
Try this:
def send(template:String, params: (String, Any)*) { }
Ken
On 23 November 2011 22:41, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote: