"down-casting" in Eiffel
- Tags:
- generics
Hi everyone.
I am new to Eiffel and have worked through the book: "Touch of Class". Great book. I am now looking forward to writing more substantial code in Eiffel.
While playing around with the language I came across the need to "cast" the type of a list from a more general type to a more specific type. The mechanism I came up with is a re-usable component:
Using this CAST class looks like this:
As I am new to Eiffel, I would like more experienced developers to guide me in this pattern. Is there a better (more idiomatic) way of doing this type of down-casting in Eiffel? It is something I need to do a lot because of the type of code I am writing. Some of the down-casting can be replaced by the Visitor design pattern, but the Visitor pattern will not always work and I need to do narrowing-casts.
Any comments are appreciated.
The object test is the only way to do a downcast. There used to be a conditional assignment but that was deprecated in favor of the object test. If any of the items in the list aren't an {H} the resultant list will be a different length. I probably wouldn't make a generic CAST class since this really isn't modeling any data, it's just holding functions. Maybe something like:
This way your class is modeling data, and the data is the list.