• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Scala Function1 trait

 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading through the Scala's type system and the Function1 trait caught my attention:



Is this something similar to a function that takes in a superclass type and returns a subclass type?

What qualifies to be a Function1 trait? Only the anonymous functions or all member functions that are defined as part of a class definition also extend from the Function1 trait?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a trait for functions which have 1 parameter. Likewise, there is Function2 for functions with 2 parameters, Function3, Function4 etc., I think up to Function22.

It's just a normal trait, so you can also explicitly create a class which extends it, it's not a special kind of trait which can only be used by anonymous functions or methods.
 
Joe Harry
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I get that, but my concern is more on the variance on the function parameter and the response type! Why is the parameter a contra-variant and the response is a co-variant?
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Dan Grossman explained this in his excellent Programming Languages course:

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, I had a hard time remembering why it was this way, and this lecture of the Programming Languages course finally made it clear to me.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic