Log of the #nice channel on irc.freenode.net

Using timezone: Central European Time
* zannerman2924 leaves08:05
* CIA-3 leaves09:51
* CIA-3 joins09:53
* CIA-3 leaves13:03
* fcb joins13:47
I've got another question :)13:51
it relates to parametric types13:52
<fcb>I want to make a binary tree. So I can have: class Node<T>13:57
and then have instance variables ?Node left and ?Node right13:58
but I also need: T value;
this doesn't seem to be allowed.
<arjanb>you should write ?Node<T> left;14:01
class Node<T> {
T value;
?Node<T> left;
?Node<T> right;
<fcb>I actually did that but there was bug in my code elsewhere telling me that T wasn't declared and I misread the line number14:04
void insert(?Node<T> root, Node<T> node) {14:05
<arjanb>make that <T> void insert(...
<fcb>yeah, that works nicely - I don't quite understand why, though14:08
<arjanb>that T is a different type variable than the one of the class so you need to declare it first14:13
<fcb>what is its scope? just that method?14:24
<fcb>ok, thanks14:27
now I'm getting a similar problem with Comparator14:30
apparently, Comparator needs a type: Comparatator<T>
now I want to implement Comparator: class MyComparator<T> implements Comparator<T>14:31
<arjanb>class MyComparator<-T> 14:32
<fcb>what does that mean?
<arjanb>-T is a contravariant type parameter
<fcb>what does that means? :)14:33
<arjanb>normally you can't assign things which have different type parameters14:38
but for example comparators that is too restrictive14:40
Comparator<int> intComp = ...
Comparator<byte> byteComp = intComp; //this is possible because of contravariance14:41
<fcb>oh, I think I get it. So only ints can be compared in by a Comparator<int>, but you can assign it to a Comparator<byte> variable14:43
<arjanb>contravariance allows type params to be subtypes and supertypes for covariance14:44
no you can compare bytes with a Comparator<int>14:45
<fcb>because I byte is somewhat related to an int?14:47
sorry: because a byte...14:48
<arjanb>no it's because you can always pass a subtype to a method14:49
<fcb>I seem to struggling with this :) Is byte a subtype of int?14:51
<fcb>could I do: Comparator<int> intComp = ...14:54
Comparator<String> strComp = intComp;
<arjanb>no String is not a subtype of int
<fcb>ok. But if I Comparator was declared as Comparator<T> not Comparator<-T> then I could I do Comparator<byte> = intComp; ?14:57
<arjanb>then it wouldn't be possible14:58
<fcb>so <T> allows ONLY objects of type T, but <-T> allows subtypes as well?14:59
<fcb>cool - got it!!!
all right, one more thing... (thanks for your patience)15:01
class MyComparator<-T> implements Comparator {
compare(T t1, T t2) { ...
this tells me that T is not declared15:02
however, it doesn't mind if I do: int compare(T t1, T t2)
(but I then need to use the overrides keyword)
<arjanb>what things should you comparator compare?15:04
type params are not allowed as pattern for an implementation15:13
<arjanb>i think i found a bug15:17
<arjanb>class MyComparator<-T | T <: ?String> implements Comparator<T> {
compare(s1, s2) {
?String s = s1;
return 0;
this doesn't work
<fcb>I must admit that type declaration is a bit advanced for me15:19
<arjanb>T <: ?String means that T must be at least of type ?String15:20
<fcb>fair enough15:21
<arjanb>but the compiler seems to ignore that part15:23
<fcb>I'm glad something good came of all my questioning then!
<arjanb>yes though it doesn't give you working code yet15:26
<fcb>no worries, I'm enjoying myself anyway15:28
<arjanb>*away for half an hour*15:33
well, I might have to call it a day
I probably won't have time to be on here tomorrow, but hopefully I'll be able to say hello over the weekend16:23
* fcb leaves
* CIA-3 joins17:58
<Bluelive>how does the contravriance work ?18:05
the comparator things seems nice
but that isnt valid for a r/w collection 18:06
* arjanb leaves23:45

Generated by Sualtam