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

Automatic wiring of manifests?

3 replies
Bernd Johannes
Joined: 2011-01-28,
User offline. Last seen 42 years 45 weeks ago.

Hello scalaists...

it's just a minor thing - but somehow I feel that all the wiring with
manifests I have to do to achieve rather mundane things like this:

class Coordinate[T <: Dimensional, P] (values: Seq[P]) (
implicit ddesc: Dimension[T], prec: Numeric[P], m: Manifest[P]
){
private var _coord = new Array[P](ddesc.dim);
...
}
def coord: Array[P] = _coord
}

could be provided by the compiler rather automatically.

Would it be possible to "enhance" the compiler to cause him to provide the
manifests required *indirectly* by a given method as an anonymous/automatic
implicit parameter?

Maybe I did something wrong but I have those Manifests (when dealing with
arrays and matches) all over the place and it feels like boilerplate. And as
scala already has the basic mechanisms for "unerasure" it would be nice if
things would work out more silently "under the hood" where possible.

Just a thought...
Greetings
Bernd

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Automatic wiring of manifests?
It's one thing to infer types, or values (implicits). It's another thing to infer type or value parameters...

On Fri, Dec 16, 2011 at 1:37 AM, Bernd Johannes <bjohanns [at] bacon [dot] de> wrote:
Hello scalaists...

it's just a minor thing - but somehow I feel that all the wiring with
manifests I have to do to achieve rather mundane things like this:

class Coordinate[T <: Dimensional, P] (values: Seq[P]) (
   implicit ddesc: Dimension[T], prec: Numeric[P], m: Manifest[P]
 ){
 private var _coord = new Array[P](ddesc.dim);
 ...
 }
 def coord: Array[P] = _coord
}

could be provided by the compiler rather automatically.

Would it be possible to "enhance" the compiler to cause him to provide the
manifests required *indirectly* by a given method as an anonymous/automatic
implicit parameter?

Maybe I did something wrong but I have those Manifests (when dealing with
arrays and matches) all over the place and it feels like boilerplate. And as
scala already has the basic mechanisms for "unerasure" it would be nice if
things would work out more silently "under the hood" where possible.

Just a thought...
Greetings
Bernd

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Automatic wiring of manifests?
This is exactly what context bounds are for!
def coordinate[T <: Dimensional : Dimension, P : Numeric : Manifest] (values: Seq[P]) {
  val ddesc = implicitly[Dimension[T]] //maybe not needed  val prec = implicitly[Numeric[P]] //maybe not needed  val m = manifest[P] //maybe not needed
  ...}

ddesc, prec and m don't even need to be defined if you're just passing them downstream as implicit params to other methods. Also note the manifest[P] pattern, this is defined in predef, but you could have easily defined it yourself:
  def manifest[T](implicit m: Manifest[T]): Manifest[T] = m

or
  def manifest[T : Manifest]: Manifest[T] = implicitly[Manifest[T]]
This is a pattern I usually follow for all my type classes


On Friday, 16 December 2011, Bernd Johannes wrote:
Hello scalaists...

it's just a minor thing - but somehow I feel that all the wiring with
manifests I have to do to achieve rather mundane things like this:

class Coordinate[T <: Dimensional, P] (values: Seq[P]) (
   implicit ddesc: Dimension[T], prec: Numeric[P], m: Manifest[P]
 ){
 private var _coord = new Array[P](ddesc.dim);
 ...
 }
 def coord: Array[P] = _coord
}

could be provided by the compiler rather automatically.

Would it be possible to "enhance" the compiler to cause him to provide the
manifests required *indirectly* by a given method as an anonymous/automatic
implicit parameter?

Maybe I did something wrong but I have those Manifests (when dealing with
arrays and matches) all over the place and it feels like boilerplate. And as
scala already has the basic mechanisms for "unerasure" it would be nice if
things would work out more silently "under the hood" where possible.

Just a thought...
Greetings
Bernd
Bernd Johannes
Joined: 2011-01-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Automatic wiring of manifests?

Am Freitag, 16. Dezember 2011, 08:34:08 schrieb Kevin Wright:
> This is exactly what context bounds are for!
>
> def coordinate[T <: Dimensional : Dimension, P : Numeric : Manifest]
> (values: Seq[P]) {
> val ddesc = implicitly[Dimension[T]] //maybe not needed
> val prec = implicitly[Numeric[P]] //maybe not needed
> val m = manifest[P] //maybe not needed
>
> ...
> }
>
> ddesc, prec and m don't even need to be defined if you're just passing them
> downstream as implicit params to other methods.
> Also note the manifest[P] pattern, this is defined in predef, but you could
> have easily defined it yourself:
>
> def manifest[T](implicit m: Manifest[T]): Manifest[T] = m
>
> or
>
> def manifest[T : Manifest]: Manifest[T] = implicitly[Manifest[T]]
>
> This is a pattern I usually follow for all my type classes
>

Living and learning... sometimes the amount of my ignorance is amazing. I'm
sure I read about context bounds but somehow didn't get the picture (as so
often when in type-land).
I guess I am one of those who can learn scala only by practicing and
ranting...

Many thanks for the enlightenment!
Thats really nice...

Greetings
Bernd

> On Friday, 16 December 2011, Bernd Johannes wrote:
> > Hello scalaists...
> >
> > it's just a minor thing - but somehow I feel that all the wiring with
> > manifests I have to do to achieve rather mundane things like this:
> >
> > class Coordinate[T <: Dimensional, P] (values: Seq[P]) (
> >
> > implicit ddesc: Dimension[T], prec: Numeric[P], m: Manifest[P]
> >
> > ){
> > private var _coord = new Array[P](ddesc.dim);
> > ...
> > }
> > def coord: Array[P] = _coord
> >
> > }
> >
> > could be provided by the compiler rather automatically.
> >
> > Would it be possible to "enhance" the compiler to cause him to provide
> > the manifests required *indirectly* by a given method as an
> > anonymous/automatic implicit parameter?
> >
> > Maybe I did something wrong but I have those Manifests (when dealing with
> > arrays and matches) all over the place and it feels like boilerplate. And
> > as
> > scala already has the basic mechanisms for "unerasure" it would be nice
> > if things would work out more silently "under the hood" where possible.
> >
> > Just a thought...
> > Greetings
> > Bernd

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