简体   繁体   English

将类型参数传递给Eiffel方法

[英]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. 我想在Eiffel中编写一个类型转换方法,该方法将“要转换的类型”作为类型参数。 Is there a way to pass a type into a method in Eiffel. 有没有一种方法可以将类型传递给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. 在Eiffel中有没有更好的方法可以做到这一点。 In C# I can specify that a method takes type parameters separate from the class type parameters. 在C#中,我可以指定一个方法采用与类类型参数分开的类型参数。 Is there a similar feature in Eiffel? 埃菲尔铁塔有类似的功能吗?

I am using EiffelStudio 6.6.8.3873 GPL. 我正在使用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. 不可能有通用方法,其中返回类型由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. 您将返回一个公共的超类,并使用分配尝试获取对具有正确类型的返回对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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