简体   繁体   中英

Passing type argument to Eiffel method

I would like to write a cast method in Eiffel which takes 'the type to cast to' as a type parameter. Is there a way to pass a type into a method in Eiffel.

The only alternative I can think of is to create a new class for the conversion. Something like:

class
   CAST [G, H]

feature

   cast (in: LIST [G]): LIST [H]
      do
         -- cast code here
      end

Is there a better way to do this in Eiffel. In C# I can specify that a method takes type parameters separate from the class type parameters. Is there a similar feature in Eiffel?

I am using EiffelStudio 6.6.8.3873 GPL.

Thanks.

It's not possible to have generic methods, where the return type is determined by a passed parameter in Eiffel. Sorry. I ran into the same issue. You will have return a common superclass and use an assignment attempts to get a reference to the returned object with the right type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM