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

Adding a purpose for implicit varargs

2 replies
David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Hello all,The following code snippet demonstrates what I'm looking for:
case class A(value: Int)

object Foo {
  implicit val a1 = new A(1)
  implicit val a2 = new A(2)
  implicit val a3 = new A(3)

  def discoverImplicits()(implicit as: A*) = as.toSeq.mkString(", ")

  def foo() {
    println(discoverImplicits())
    //Should print: "A(1), A(2), A(3)" (or in a different order)
  }
}
Formally: An implicit argument list that contains a vararg-parameter should be filled with all possible implicit values that are in scope and that are satisfying that vararg parameter's type.
I could see this being useful for dependency injection with multiple service providers, adding compile-time "plugins" by having the compiler fill a list for you of all available classes satisfying an interface, and creating context-aware DSLs by using implicits to define a DSL-statement's scope or similar.
Thoughts?
Cheers,David Flemström
Razvan Cojocaru 2
Joined: 2009-11-20,
User offline. Last seen 42 years 45 weeks ago.
RE: Adding a purpose for implicit varargs
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);}

I don’t see the value of it. The implicits have different names and different purposes…I don’t see why you’d bundle them except maybe for debugging?

 

 

From: David Flemström [mailto:david [dot] flemstrom [at] gmail [dot] com]
Sent: Wednesday, June 09, 2010 4:29 PM
To: scala-debate
Subject: [scala-debate] Adding a purpose for implicit varargs

 

Hello all,

The following code snippet demonstrates what I'm looking for:

 
case class A(value: Int)
 
object Foo {
  implicit val a1 = new A(1)
  implicit val a2 = new A(2)
  implicit val a3 = new A(3)
 
  def discoverImplicits()(implicit as: A*) = as.toSeq.mkString(", ")
 
  def foo() {
    println(discoverImplicits())
    //Should print: "A(1), A(2), A(3)" (or in a different order)
  }
}

Formally: An implicit argument list that contains a vararg-parameter should be filled with all possible implicit values that are in scope and that are satisfying that vararg parameter's type.

 

I could see this being useful for dependency injection with multiple service providers, adding compile-time "plugins" by having the compiler fill a list for you of all available classes satisfying an interface, and creating context-aware DSLs by using implicits to define a DSL-statement's scope or similar.

 

Thoughts?

 

Cheers,

David Flemström

Rafael de F. Fe...
Joined: 2010-05-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Adding a purpose for implicit varargs

On Thu, Jun 10, 2010 at 1:17 PM, razie wrote:
> I don’t see the value of it. The implicits have different names and
> different purposes…I don’t see why you’d bundle them except maybe for
> debugging?
>
>
>
>
>
> ________________________________
>
> From: David Flemström [mailto:david [dot] flemstrom [at] gmail [dot] com]
> Sent: Wednesday, June 09, 2010 4:29 PM
> To: scala-debate
> Subject: [scala-debate] Adding a purpose for implicit varargs
>
>
>
> Hello all,
>
> The following code snippet demonstrates what I'm looking for:
>
>
>
> case class A(value: Int)
>
>
>
> object Foo {
>
>   implicit val a1 = new A(1)
>
>   implicit val a2 = new A(2)
>
>   implicit val a3 = new A(3)
>
>
>
>   def discoverImplicits()(implicit as: A*) = as.toSeq.mkString(", ")
>
>
>
>   def foo() {
>
>     println(discoverImplicits())
>
>     //Should print: "A(1), A(2), A(3)" (or in a different order)
>
>   }
>
> }
>
> Formally: An implicit argument list that contains a vararg-parameter should
> be filled with all possible implicit values that are in scope and that are
> satisfying that vararg parameter's type.
>
>
>
> I could see this being useful for dependency injection with multiple service
> providers, adding compile-time "plugins" by having the compiler fill a list
> for you of all available classes satisfying an interface, and creating
> context-aware DSLs by using implicits to define a DSL-statement's scope or
> similar.
>
>
>
> Thoughts?

Looks great, would love to be able to gather implicit dependencies. In
terms of DI, It can be thought as a static analog to this kind of
thing: http://www.picocontainer.org/arrays-collections-and-maps.html

> Cheers,
> David Flemström

--
Rafael de F. Ferreira.
http://www.rafaelferreira.net/

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