<:< versus HKT

109 views
Skip to first unread message

Alan Burlison

unread,
Sep 18, 2012, 4:59:54 AM9/18/12
to scala...@googlegroups.com
In "Scala in Depth" there is an example of the use of :<: to reify type
constraints:

def peek[C, A](col: C)(implicit ev: V <:< Traversable[A]) = (col.head, col)

the following version of peek also works, and (to me at least) seems
simpler as it doesn't require the use of an implicit:

def peek[A, C[A] <: Traversable[A]](col: C[A]) = (col.head, col)

What I'm not clear about is when you'd use one form over the other. My
natural inclination would be to use the second exclusively, but I'm
pretty sure I'm missing something.

--
Alan Burlison
--

Ken Scambler

unread,
Sep 18, 2012, 5:11:02 AM9/18/12
to Alan Burlison, scala...@googlegroups.com
What I'm not clear about is when you'd use one form over the other.  My natural inclination would be to use the second exclusively, but I'm pretty sure I'm missing something.

--
Alan Burlison
--

Seth Tisue and I had an exchange on Scala Language the other day which should fill in the gaps:

https://groups.google.com/forum/?fromgroups=#!topic/scala-language/ZAGiK4puSac

Basically, use <: when you can, and <:< when you must.

Cheers,
Ken

Josh Suereth

unread,
Sep 18, 2012, 7:17:00 AM9/18/12
to Ken Scambler, Alan Burlison, scala-user

That's a great motto.  <:< can help you bridge a type inference issue.   <: should be preferred.

The Traversable example is, I believe, fixed in 2.10.

Alan Burlison

unread,
Sep 18, 2012, 2:08:29 PM9/18/12
to Ken Scambler, scala...@googlegroups.com
On 18/09/2012 10:11, Ken Scambler wrote:

> Seth Tisue and I had an exchange on Scala Language the other day which
> should fill in the gaps:
>
> https://groups.google.com/forum/?fromgroups=#!topic/scala-language/ZAGiK4puSac

Thanks, I found the links you posted at the end of that thread very
interesting, especially the "Scala - a roadmap" thread.

--
Alan Burlison
--

missingfaktor

unread,
Sep 19, 2012, 8:10:59 AM9/19/12
to Alan Burlison, Ken Scambler, scala...@googlegroups.com
This thread might help too. https://groups.google.com/forum/#!topic/scala-user/s38hv6culCw
--
Cheers,
Rahul.

Alan Burlison

unread,
Sep 19, 2012, 11:08:54 AM9/19/12
to missingfaktor, Ken Scambler, scala...@googlegroups.com
On 19/09/2012 13:10, missingfaktor wrote:

> This thread might help too.
> https://groups.google.com/forum/#!topic/scala-user/s38hv6culCw

A bit. I'm still not 100% clear when you *must* use implicit evidence
parameters because nothing else will work. For example, in the toMap
example, if you need the supplied list to consist of 2-tuples, why can't
you just say that directly using the Tuple2 type?

In the example you give in the thread
(https://groups.google.com/d/msg/scala-user/s38hv6culCw/ssVM5X0ewbMJ) I
can see that you are using 'implicit ev: A =:= Int' to in effect defer
the check that A is an Int until that method is actually used.

However, I'm not clear in the toMap case why implicit evidence
parameters are necessary and a use of Tuple2 won't work, although I'm
struggling to come up with something that the Scala compiler actually likes.

Thanks for the link though, it did help some :-)

--
Alan Burlison
--

missingfaktor

unread,
Sep 19, 2012, 3:07:35 PM9/19/12
to Alan Burlison, Ken Scambler, scala...@googlegroups.com
Actually your hunch is right and there is a way to do that but I am not sure I like it better than implicit parameters.

scala> :paste
// Entering paste mode (ctrl-D to finish)

class Lest[A](xs: List[A]) {
  def toMap[B, C, Dummy >: A <: (B, C)] : Map[B, C] = {
    Map.empty[B, C] ++ (xs : List[Dummy])
  }
}

val l = new Lest(List((3, 4), (1, 9)))
l.toMap[Int, Int, (Int, Int)]

// Exiting paste mode, now interpreting.

defined class Lest
l: Lest[(Int, Int)] = Lest@152b4053
res14: Map[Int,Int] = Map(3 -> 4, 1 -> 9)


--
Cheers,
Rahul.

missingfaktor

unread,
Sep 19, 2012, 3:08:59 PM9/19/12
to Alan Burlison, Ken Scambler, scala...@googlegroups.com
Also in this particular case, compiler cannot infer types with this scheme.
--
Cheers,
Rahul.

Alan Burlison

unread,
Sep 20, 2012, 10:07:53 AM9/20/12
to missingfaktor, Ken Scambler, scala...@googlegroups.com
On 19/09/2012 20:07, missingfaktor wrote:

> Actually your hunch is right and there is a way to do that but I am not
> sure I like it better than implicit parameters.
>
> scala> :paste
> // Entering paste mode (ctrl-D to finish)
>
> class Lest[A](xs: List[A]) {
> def toMap[B, C, Dummy>: A<: (B, C)] : Map[B, C] = {
> Map.empty[B, C] ++ (xs : List[Dummy])
> }
> }

That's horrendously beautiful, or ugly, or just plain deranged, or
something...

I've tried to think of a way of avoiding the use of Dummy but can't
think of one.

Wow, my head is still spinning. Thanks ;-)

--
Alan Burlison
--
Reply all
Reply to author
Forward
0 new messages