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

Missing Parameter Type Error

4 replies
Dave Ray
Joined: 2009-01-07,
User offline. Last seen 42 years 45 weeks ago.

Hello. I've only been using Scala about a week and am really enjoying
it. One confusing error I've gotten is related to this simple utility
function I created:

def withSome[T](in : Option[T], code : (T) => Unit) {
in match {
case Some(s) => code(s)
case None =>
}
}

Scala requires me to provide a type parameter even though it seems
like it should be able to infer it from the parameters. That is, if I
call:

withSome(Some("some"), (s) => println(s))

I get "error: missing parameter type". However, if I provide the type parameter

withSome[String](Some("some"), (s) => println(s))

everything works fine. What am I doing wrong? Also, if there is a
built-in library function that provides the same functionality, or a
better way to do this, I'd love to hear about it. It's going to take
me a while to be able to figure out how to write idiomatic Scala code.

Thanks!

Dave

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: Missing Parameter Type Error
The built-in foreach function does exactly what you want:

Some("some").foreach( s => println(s))
None.foreach((s: String) =>println(s))

--j

On Wed, Jan 7, 2009 at 1:06 PM, Dave Ray <daveray [at] gmail [dot] com> wrote:
Hello. I've only been using Scala about a week and am really enjoying
it. One confusing error I've gotten is related to this simple utility
function I created:

 def withSome[T](in : Option[T], code : (T) => Unit) {
   in match {
     case Some(s) => code(s)
     case None =>
   }
 }


Scala requires me to provide a type parameter even though it seems
like it should be able to infer it from the parameters. That is, if I
call:

  withSome(Some("some"), (s) => println(s))

I get "error: missing parameter type". However, if I provide the type parameter

  withSome[String](Some("some"), (s) => println(s))

everything works fine. What am I doing wrong?  Also, if there is a
built-in library function that provides the same functionality, or a
better way to do this, I'd love to hear about it. It's going to take
me a while to be able to figure out how to write idiomatic Scala code.

Thanks!

Dave

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: Missing Parameter Type Error
You can also use for-comprehensions to deal with options:

  for(s <- Some("some")) {
    println(s)
  }

Under the hood, this code translates to exactly the first line of my last email (that is, using 'foreach').

--j

On Wed, Jan 7, 2009 at 1:26 PM, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote:
The built-in foreach function does exactly what you want:

Some("some").foreach( s => println(s))
None.foreach((s: String) =>println(s))

--j

On Wed, Jan 7, 2009 at 1:06 PM, Dave Ray <daveray [at] gmail [dot] com> wrote:
Hello. I've only been using Scala about a week and am really enjoying
it. One confusing error I've gotten is related to this simple utility
function I created:

 def withSome[T](in : Option[T], code : (T) => Unit) {
   in match {
     case Some(s) => code(s)
     case None =>
   }
 }


Scala requires me to provide a type parameter even though it seems
like it should be able to infer it from the parameters. That is, if I
call:

  withSome(Some("some"), (s) => println(s))

I get "error: missing parameter type". However, if I provide the type parameter

  withSome[String](Some("some"), (s) => println(s))

everything works fine. What am I doing wrong?  Also, if there is a
built-in library function that provides the same functionality, or a
better way to do this, I'd love to hear about it. It's going to take
me a while to be able to figure out how to write idiomatic Scala code.

Thanks!

Dave


Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Missing Parameter Type Error
To complement Jorge's answer, another way to call your function is to write:

withSome(Some("some"), (s: String) => println(s))

(adding the s: String type annotation)

Or writing the function with multiple parameter lists such that type inference applies only to the first parameter:

def withSome[T](in : Option[T])(code: T => Unit) {
  in match {
    case Some(s) => code(s)
    case None =>
    }
}

then you can simply write,

withSome(Some("some")) { s => println(s) }

alex


On Wed, Jan 7, 2009 at 11:06 AM, Dave Ray <daveray [at] gmail [dot] com> wrote:
Hello. I've only been using Scala about a week and am really enjoying
it. One confusing error I've gotten is related to this simple utility
function I created:

 def withSome[T](in : Option[T], code : (T) => Unit) {
   in match {
     case Some(s) => code(s)
     case None =>
   }
 }


Scala requires me to provide a type parameter even though it seems
like it should be able to infer it from the parameters. That is, if I
call:

  withSome(Some("some"), (s) => println(s))

I get "error: missing parameter type". However, if I provide the type parameter

  withSome[String](Some("some"), (s) => println(s))

everything works fine. What am I doing wrong?  Also, if there is a
built-in library function that provides the same functionality, or a
better way to do this, I'd love to hear about it. It's going to take
me a while to be able to figure out how to write idiomatic Scala code.

Thanks!

Dave

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Missing Parameter Type Error
As others have said, foreach does the same job.  But to answer your question, that behavior is irritating and perhaps strange, but there are workarounds.

scala> def withSome[T](in : Option[T], code : T => Unit) {
     |    in match {
     |      case Some(s) => code(s)
     |      case None =>
     |    }
     | }
withSome: [T](Option[T],(T) => Unit)Unit

scala> withSome(Some("some"), {s => println(s)})
<console>:6: error: missing parameter type
       withSome(Some("some"), {s => println(s)})
                               ^

If you use a naked function instead of a lambda, it Just Works <tm>

scala> withSome(Some("some"), println)
some

It also Just Works<tm> when you write the function in curried form

scala> def withSomeCurried[T](in : Option[T])(code : T => Unit) {
     |    in match {
     |      case Some(s) => code(s)
     |      case None =>
     |    }
     | }
withSomeCurried: [T](Option[T])((T) => Unit)Unit

scala> withSomeCurried(Some("some")){s => println(s)}
some

On Wed, Jan 7, 2009 at 11:06 AM, Dave Ray <daveray [at] gmail [dot] com> wrote:
Hello. I've only been using Scala about a week and am really enjoying
it. One confusing error I've gotten is related to this simple utility
function I created:

 def withSome[T](in : Option[T], code : (T) => Unit) {
   in match {
     case Some(s) => code(s)
     case None =>
   }
 }


Scala requires me to provide a type parameter even though it seems
like it should be able to infer it from the parameters. That is, if I
call:

  withSome(Some("some"), (s) => println(s))

I get "error: missing parameter type". However, if I provide the type parameter

  withSome[String](Some("some"), (s) => println(s))

everything works fine. What am I doing wrong?  Also, if there is a
built-in library function that provides the same functionality, or a
better way to do this, I'd love to hear about it. It's going to take
me a while to be able to figure out how to write idiomatic Scala code.

Thanks!

Dave

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