Re: forEach and Casting



On Apr 22, 3:54 pm, Tom Hawtin <use...@xxxxxxxxxxxxxxxxx> wrote:
Jason Cavett wrote:

Currently, the collection I'm using an Iterator over contains objects
of a Generic type (an abstract class). When I get the object from the
collection, I must cast it to the more specific type so I can use the
various methods. There is no way to change this.

I suggest you really make sure whether you can change it or not. If you
are using generics, casts often mean design issues.

// this is the part I'm not sure about - can I even do something like
this?
for(Specific o : (Specific) genericCollection) {

You can quite easily make a "safe" casting iterator.

/**
* <strong>{see Iterator#next}
* will throw ClassCastException...</strong>
*/
public static <T> Iterable<T> castIterable(
final Class<T> clazz, final Iterable<? super T> iterable
) {
return new Iterable<T>() {
public java.util.Iterator<T> iterator() {
return new java.util.Iterator<T>() {
private final java.util.Iterator<? super T> target =
iterable.iterator();
public boolean hasNext() {
return target.hasNext();
}
public T next() {
return clazz.cast(target.next());
}
public void remove() {
target.remove();
}
};
}
};
}

...
for (Specific thing : castIterable(Specific.class, generals)) {

(Disclaimer: Not tested.)

Tom Hawtin

Well, the reason it can't easily be changed is because I don't know
what class I'm going to want until runtime. It depends on where the
user is at in the tree structure since each component in the tree is
different (even though they all have the same superclass).

As such, I need a class that can return the class type I want.

.



Relevant Pages

  • Re: Why does this object need to be cast?
    ... Having just attended an all day 'generics' education day, ... why do I need to cast to comboboxes? ... >> know that this is an object of type combobox? ... > Example with cast operators on System.Byte and System.Int32: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What about turbos?
    ... In Delphi.NET TSomeClasssatisfies a compile time check but ALSO ... Type checking with a cast is at runtime. ... As maybe - I haven't really thought about the .NET generics implementation as ... ensure that they get that type (or NIL as you pointed out). ...
    (borland.public.delphi.non-technical)
  • Re: Casting generic collections & inheritance
    ... think the problem is that people who are new to generics (such as ... " which goes back to the English grouping problem. ... >> just a simple cast from Farmer to Person. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Method chaining with generics
    ... Casting does affect the declared type and type metadata of an object reference, but generics are a lower-level concept: they apply to the types cast from and to, which you must have before -- and irrespective of -- any consideration of casting. ... Listis a supertype of List<Foo> for any Foo, so you never have to actually write such a cast, and if you do then you can be certain that it is correct. ...
    (comp.lang.java.programmer)
  • Re: foreach considered dangerous?
    ... > taget type without warning. ... > Without generics this behaviour had the advantage of less typing for us ... > consider this feature of foreach as dangerous. ... > implicit cast would occur. ...
    (microsoft.public.dotnet.languages.csharp)