Menu

#47 Easier way to cast to a superclass

open
nobody
None
5
2006-07-02
2006-07-02
B S
No

Currently you can often end up with ambiguous method
calls. Currently the only way to resolve this is to
define a new method, either on the class with the
ambiguity, or by defining a new method that acts as a
simple cast to a superclass. If this occurs only once
or twice for a given class, it can be annoying to
define a new method just for that call. A way to easily
cast a reference to be a superclass reference could
resolve many of these problems.

Discussion

  • Artem Grinblat

    Artem Grinblat - 2006-07-12

    Logged In: YES
    user_id=289741

    Do you have an example?
    I know there are problems with ambiguous methods, but
    usually there is a workaround.

     
  • B S

    B S - 2006-07-13

    Logged In: YES
    user_id=929365

    This isn't (I think) where I was annoyed enough by the
    problem to post it, but it demonstrates it rather well:

    There is currently a bug in the list slicing in
    nice.functional where list[1..4] (for example) will not
    compile because 1..4 is determined to be a Range, which
    implements both List and Slice. Since there exist both
    get(List,Slice) and get(List,List), it fails due to
    ambiguity (as it should). To fix the problem, I wrote a
    get(List,Range) method that just recalled get. However I had
    to write this to do so:

    <T> Slice<T> sl(Slice<T> t) = t;
    override <T> List<T> get(List<T> list, Range<int> slice){
    return list[sl(slice)];
    }

    This is I believe the standard way of doing an upcast, and
    it works just fine. It is just annoying to have to write an
    equivalent to sl for every type you need to upcast. There
    may also be a (small and probably insignifcant) performance
    penalty here, as I would assume that a java style upcast
    doesn't involve a method call in the bytecode.

     
  • B S

    B S - 2006-07-13

    Logged In: YES
    user_id=929365

    An alternative would be

    override <T> List<T> get(List<T> list, Range<int> slice){
    Slice<int> s = slice
    return list[s];
    }

    but thats even worse if you are doing the cast multiple times.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.