• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Scala wildcard meaning

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following function definition:



What would the _ mean?
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure, but isn't it basically the Option[A] that you would want to convert using f? Applying a map() to the Option[A] means you will get an Option as a result, but your function f is applied to the actual value inside the Option i.e. an instance of A. It's a bit like using a map() on a List i.e. your mapping function is applied to each individual element inside the List (e.g. A=>B), but you still get a List out.

So you now have a way to use the function f, which turns an A into a B, to convert an Option[A] to an Option[B], which is what lifting does.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually for the last 4 months, I have been doing C# and all the knowledge and experience that I had when I worked with Scala last year seems to be slowly diminishing. Honestly it took me some couple of minutes to understand what that function was doing. I have been out of Java programming for the last 3 years, but I could still pick up any legacy Java code base and understand it on the fly. That seems to be a bit difficult with Scala!
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't that a shorthand for a placeholder in a function literal? So
is the same as
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In Scala, _ is useful in many contexts.

Here are some examples:



What follows the map is a function. In the first form we are explicitly naming the function argument.
The second form is terse - but means the same thing. We are not naming the argument (we don't need to).

Now consider the following:



If we try to use the terse form, we get an error.



When you repeat the _, each occurrence refers to successive arguments. As we are passing in just one element
(which is a pair), there is just one argument, so _._2 is erroneous.

Following contrived snippet shows the successive arguments form:


_ is used in Scala in many contexts (pattern matching don't care) - and for creating partially applied functions:



Hope this helps...

--- regards atul


 
reply
    Bookmark Topic Watch Topic
  • New Topic