[nem-bug] [Nemerle 0000646]: Clarification req. about typing system

feedback at nemerle.org feedback at nemerle.org
Tue Apr 4 19:46:04 CEST 2006


A NOTE has been added to this issue.
======================================================================
<http://nemerle.org/bugs/view.php?id=646> 
======================================================================
Reported By:                SeamusL
Assigned To:                
======================================================================
Project:                    Nemerle
Issue ID:                   646
Category:                   Compiler
Reproducibility:            always
Severity:                   minor
Priority:                   normal
Status:                     new
======================================================================
Date Submitted:             04-03-2006 21:48 CEST
Last Modified:              04-04-2006 19:46 CEST
======================================================================
Summary:                    Clarification req. about typing system
Description: 
Where 'a : 'b, can it follow that Iface['a] : Iface['b]?

I can't implement several instances of a generic interface. Is this a
feature or not working or am I just doing things incorrectly? I would like
to implement an interface for several, but not all, subtypes of a base
type.

See attached commented code for my gripes.

Sorry if this is mis-classified. I am unsure about the nature of the
problems I face (features, bugs, my own stupidity.)
======================================================================

----------------------------------------------------------------------
 nazgul - 04-03-06 22:20 
----------------------------------------------------------------------
There are several issues:

1) you say that compiler did not infer IComparable interface for
DefContainsImpl, this is not true, as you can see in my attached example.
Probably the problem was the need for upcast to IComparable, which is
needed for type parameters (and explicitly implemented interfaces) - it
statically typesafe, but needed in Nemerle and C#.

2) the IComparable, which you added to DefContainsImpl causes for ugly
problems with compiler:
  - the internal compiler error you observed, which is of course a bug
  - CheckSTV, I guess it is a known bug, the message is very unclear, but
means that default type used (object) does not satisfy 'a : IComparable
['a]
  - the 'typing constraint on 'a not satisfied, upon instantiation of
DefContainsImpl', which is a little bit unclear, but true - Thing does not
implement IComparable [Thing]  (it just derives implementation of
IComparable [Entity])

3) the last obstacle is by design at the moment - the limitation (which is
not present in C#) is quite important for type inference algorithm used in
Nemerle compiler, because it simplifies reasoning about how given type
implements some interface. I guess changing this is possible, but Michal
who designed the alorithm would know better

----------------------------------------------------------------------
 SeamusL - 04-04-06 01:45 
----------------------------------------------------------------------
Thank you for clearing some of this up for me. It is unfortunate that the
side-effect is that I can't, as I'd hoped, write the following class, due
to the possible upcasting required, but I can certainly work around it.

The bit I do miss is the use of multiple interface instances, because I
can see copy-and-paste code in my near future, but I can see that this
could cause many cases which would effectively break type inference.
Possibly this should be documented (I haven't seen it mentioned); maybe
under  "Grok Parametric Polymorphism" or "Grok OOP", though that doesn't
quite seem the right place, since it's a side issue.



public class DefContainsImpl['a] : Contains['a]
  where 'a : Entity {
   
    el : 'a;
    _lst : Set['a];   // This must be Set[Entity], or possibly list['a]

    foo (x : 'a) : void {
      _ = (el : IComparable [Entity]).CompareTo (x);
    }
 }

----------------------------------------------------------------------
 malekith - 04-04-06 16:22 
----------------------------------------------------------------------
I think the multiple interface thing would require a major redesign of the
way we treat subtyping relation. But this will have to eventually be done.

----------------------------------------------------------------------
 nazgul - 04-04-06 19:46 
----------------------------------------------------------------------
When you try following C#:

------------
public class Entity : IComparable<Entity> {
  public int CompareTo( Entity e )  { retrun 0; }
}

class MySet <a> where a : IComparable <a> {
}

public class DefContainsImpl<a>
  where a : Entity {
   
    a el;
    MySet<a> lst; // This must be Set[Entity], or possibly list['a]

    void foo (a x)  {
      ((IComparable [Entity]) el).CompareTo (x);
    }
 }
-------------

the message is very similar:

t.cs(15,14): error CS0309: The type 'a' must be convertible to
'System.IComparable<a>' in order to use it as parameter 'a' in the generic
type or method 'MySet<a>'


And in fact both ncc and csc are right, the constraint
'a : Entity is saying that  'a : IComparable [Entity], not 'a :
IComparable ['a].

I guess currently the best solution is to just stick with 'a : IComparable
constraint everywhere (in Contains and DefContainsImpl) or to use Set
[Entity] in DefContainsImpl. In fact, I guess second option is very close
to the containment concept you are trying to implement - if 'a is always a
subtype of Entity, then you can use Set[Entity] or Set['a] and it shouldn't
make much different (tell me if I'm wrong).

Issue History
Date Modified  Username       Field                    Change              
======================================================================
04-03-06 21:48 SeamusL        New Issue                                    
04-03-06 21:48 SeamusL        File Added: test1.n                          
04-03-06 22:10 nazgul         File Added: t.n                              
04-03-06 22:20 nazgul         Note Added: 0001152                          
04-04-06 01:45 SeamusL        Note Added: 0001160                          
04-04-06 16:22 malekith       Note Added: 0001161                          
04-04-06 19:46 nazgul         Note Added: 0001162                          
======================================================================




More information about the bugs mailing list