This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Passing associative parameters to a method?

8 replies
Aishwarya Singhal
Joined: 2011-09-09,
User offline. Last seen 42 years 45 weeks ago.

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

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Passing associative parameters to a method?
Sorry, forgot to include list.  This probably belongs on scala-user, so I'm transferring to there.

---------- 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:
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


Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Passing associative parameters to a method?
Sorry, forgot to include list.  This probably belongs on scala-user, so I'm transferring to there.

---------- 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:
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


Aishwarya Singhal
Joined: 2011-09-09,
User offline. Last seen 42 years 45 weeks ago.
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

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Passing associative parameters to a method?

On 23 November 2011 22:54, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
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...
 
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
Dan Shryock
Joined: 2010-01-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Passing associative parameters to a method?
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 <ken [dot] scambler [at] gmail [dot] com> wrote:
Sorry, forgot to include list.  This probably belongs on scala-user, so I'm transferring to there.

---------- 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:
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



Aishwarya Singhal
Joined: 2011-09-09,
User offline. Last seen 42 years 45 weeks ago.
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

Cédric Beust ♔
Joined: 2011-06-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Passing associative parameters to a method?
FWIW, a traditional way to do this is to use builders, which have the benefit of 1) being type safe (passing Any* should be cause for pause) and 2) allow you to do some validation on your object before actually instantiating it.
-- Cédric




On Wed, Nov 23, 2011 at 7:31 AM, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
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 <dan [dot] shry [dot] [dot] [dot] [at] gmail [dot] com> 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 <ken [dot] scamb [dot] [dot] [dot] [at] gmail [dot] com>wrote:
>
>
>
>
>
>
>
> > Sorry, forgot to include list.  This probably belongs on scala-user, so
> > I'm transferring to there.
>
> > ---------- Forwarded message ----------
> > From: Ken Scambler <ken [dot] scamb [dot] [dot] [dot] [at] gmail [dot] com>
> > Date: 23 November 2011 22:46
> > Subject: Re: [scala-debate] Passing associative parameters to a method?
> > To: Aishwarya Singhal <asingha [dot] [dot] [dot] [at] gmail [dot] com>
>
> > Try this:
> > def send(template:String, params: (String, Any)*) { }
>
> > Ken
>
> > On 23 November 2011 22:41, Aishwarya Singhal <asingha [dot] [dot] [dot] [at] gmail [dot] com> 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

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Passing associative parameters to a method?
On 24 November 2011 01:37, Dan Shryock <dan [dot] shryock [at] gmail [dot] com> 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:_*) }
 
Or even better:
val paramsMap = params.toMap

The compiler knows that "params" contains 2-tuples!

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland