This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Scala Higher Kinded Types

 
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading about higher kinded types in Scala recently and have a simple question. What is the difference between the following?



My understanding is that both are the same! If so then why use an underscore?
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not really sure yet what "higher kinded types" means, even after I read the explanation on the Artima site. I should re-read it again because that was months ago and I'm a little farther along now.

But...as to this question, isn't it the same difference as always between naming something versus providing an underscore? Namely, that naming it lets you then refer to it in subsequent code as A or whatever name you chose, whereas using an underscore is when you don't need to refer to it again and don't care.
 
Joe Harry
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen this in the Functor definition such as:



I was wondering what if I used a character instead of the underscore. I will have to do a bit of thorough reading to really understand this concept!
 
Ranch Hand
Posts: 55
Scala Mac OS X Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joe Harry wrote:I have been reading about higher kinded types in Scala recently and have a simple question. What is the difference between the following?



My understanding is that both are the same! If so then why use an underscore?




I'll try to get there with a REPL example


What you see here is that if you specify a type parameter A, then the type is bound to be defined in the context you're using it in. It's an explicitly named type, even though its runtime specific type is not known.
If your usage of the higher-kinded type doesn't depend on the specific type parameter, you can omit it (with the generic underscore placeholder syntax), and the compiler will happily comply.
I hope this is somewhat comprehensible.

Going to the details, the "_" wildcard, when used in this way is called an existential type and is a shorthand syntax for the more wordy (but maybe clearer)
It's like saying: 'I need a type T, but I don't care what it is, as long as it exists'.

If you're asking yourself why should I ask for existence of the type, the answer is that you can constraint the type like the following snippet



Makes sense?


For a more detailed reference look at this post on higher-kinded types, and check the appendix links.

Also a nice reference for existential types is here on stackoverflow
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I once asked a question on StackOverflow to which Daniel Spiewak answered with a good explanation of what higher-kinded types are.

Basically, they are a level above types.

List[T] is a type - a list of T's. It's kind is written as: *

List[_] is not a type by itself - it's a category of types, which encompasses all types of the form List[<some type>]. It's kind is written as: * => *

It works in a way that is analogous to values. Values are instances of types. The kind * is analogous to a constant (a fixed value). The kind * => * is analogous to a function that takes one input and produces one output.

List[_] is like a function that takes a type, and that produces types. The result of this "function" is a concrete List type, for example List[T] or List[String] etc.

The kind is the "type of the type". It's all quite abstract and I'm not explaining it very rigorously, but I hope you get the basic idea.
 
Joe Harry
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivano Pagano wrote:
What you see here is that if you specify a type parameter A, then the type is bound to be defined in the context you're using it in. It's an explicitly named type, even though its runtime specific type is not known.
If your usage of the higher-kinded type doesn't depend on the specific type parameter, you can omit it (with the generic underscore placeholder syntax), and the compiler will happily comply.
I hope this is somewhat comprehensible.



To some extent this makes sense but only from a type-less-characters in Scala point of view!
 
Joe Harry
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:I once asked a question on StackOverflow to which Daniel Spiewak answered with a good explanation of what higher-kinded types are.



That seems to be a good explanation!
 
Joe Harry
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I have the following type of a type:



This then has the kind:



If I have a type constructor List, I would end up having Function[List], but what type is that List made of? I would understand this kind to be:



But why is this only represented as a Functor[List] and not Functor[List[String]]?
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!