Scala:如何在编译时不知道类型的情况下调用带有类型参数和清单的方法? [英] Scala: How to invoke method with type parameter and manifest without knowing the type at compile time?

查看:107
本文介绍了Scala:如何在编译时不知道类型的情况下调用带有类型参数和清单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下签名的函数:

I have a function with the following signature:

myFunc[T <: AnyRef](arg: T)(implicit m: Manifest[T]) = ???

如果我在编译时不知道参数的确切类型,该如何调用该函数?

How can I invoke this function if I do not know the exact type of the argument at the compile time?

例如:

val obj: AnyRef = new Foo()    // At compile time obj is defined as AnyRef,
val objClass = obj.getClass    // At runtime I can figure out that it is actually Foo
// Now I would need to call `myFunc[Foo](obj.asInstanceOf[Foo])`,
// but how would I do it without putting [Foo] in the square braces?

我想写一些逻辑类似的东西

I would want to write something logically similar to:

myFunc[objClass](obj.asInstanceOf[objClass])

谢谢!

更新:

问题是无效的-正如@ DaoWen,@ Jelmo和@itsbruce正确指出的那样,我正在尝试做的事情完全是胡说八道!我只是认真地思考了这个问题. 感谢大伙们!太糟糕了,我不能接受所有答案都是正确的:)

The question is invalid - As @DaoWen, @Jelmo and @itsbruce correctly pointed, the thing I was trying to do was a complete nonsense! I just overthought the problem severely. THANK YOU guys! It's too bad I cannot accept all the answers as correct :)

因此,此问题是由以下情况引起的:

So, the problem was caused by the following situation:

我正在使用 Salat 库将对象与BSON/JSON表示形式进行序列化. Salat具有Grater[T]类,该类用于序列化和反序列化. BSON对反序列化的方法调用看起来是这样的:

I am using Salat library to serialize the objects to/from BSON/JSON representation. Salat has an Grater[T] class which is used for both serialization and deserialization. The method call for deserialization from BSON looks this way:

val foo = grater[Foo].asObject(bson)

在这里,类型参数的作用很明显.然后,我想做的就是使用相同的Grater从我的域模型中序列化任何实体.所以我写道:

Here, the role of type parameter is clear. What I was trying to do then is to use the same Grater to serialize any entity from my domain model. So I wrote:

val json = grater[???].toCompactJSON(obj)

我立即冲上去思考,只是没有看到表面上有明显的解决方案.这是:

I immediately rushed for reflection and just didn't see an obvious solution lying on the surface. Which is:

grater[Entity].toCompactJSON(obj)  // where Entity...

@Salat trait Entity                // is a root of the domain model hierarchy

有时候事情比我们想象的要容易得多! :)

Sometimes things are much easier than we think they are! :)

推荐答案

这是使用类型安全的OO语言的错误方法.如果您需要这样做,则您的设计是错误的.

This is the wrong way to work with a type-safe OO language. If you need to do this, your design is wrong.

myFunc[T <: AnyRef](arg: T)(implicit m: Manifest[T]) = ???

这可能毫无用处,正如您可能已经发现的那样.您可以对可能是任何东西的对象调用哪种有意义的功能?您不能直接引用其属性或方法.

This is, of course, useless, as you have probably discovered. What kind of meaningful function can you call on an object which might be anything? You can't make any direct reference to its properties or methods.

我想写一些逻辑类似的东西

I would want to write something logically similar to:

myFunc[objClass](obj.asInstanceOf[objClass])

为什么?通常仅在非常特殊的情况下才需要这种事情.例如,您是否正在编写一个将使用依赖项注入的框架?如果您不对Scala的功能进行某种高度技术性的扩展,则不必这样做.

Why? This kind of thing is generally only necessary for very specialised cases. Are you writing a framework that will use dependency injection, for example? If you're not doing some highly technical extension of Scala's capabilities, this should not be necessary.

我敢打赌,您会对该类有更多了解,因为您说您不知道确切类型.基于类的OO的工作方式的很大一部分是,如果您想对通用类型的对象(包括其所有子类型)进行某些操作,则可以将该行为放入属于该类的方法中.让子类在需要时覆盖它.

I bet you know something more about the class, since you say you don't know the exact type. One big part of the way class-based OO works is that if you want to do something to a general type of objects (including all its subtypes), you put that behaviour into a method belonging to the class. Let subclasses override it if they need to.

坦率地说,要执行的操作的方法是在对类型足够了解的上下文中调用函数.

Frankly, the way to do what you are attempting is to invoke the function in a context where you know enough about the type.

这篇关于Scala:如何在编译时不知道类型的情况下调用带有类型参数和清单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆