Scala mixin到类实例 [英] Scala mixin to class instance

查看:86
本文介绍了Scala mixin到类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala中可以对类实例进行一些混合吗?

Is it possible in Scala to make some mixin to class instance?

例如:我有一些MyClass实例var x = new MyClass,我想在某种方法或特征上扩展它而不复制它.

Eg: I have some MyClass instance var x = new MyClass and I want to extend it on some method or trait without copying it.


我正在寻找实例化后扩展x的方法. 因此,例如在函数方法中,该方法将x作为参数.


I'm looking the way to extend x after it has been instantiated. So, for example in a function method, which gets x as a parameter.

[背后是什么]
我只是想知道是否存在一些带有隐式对象和Manifest的魔术来实现类型类模式而无需显式调用隐式对象(例如在Haskell中). 但仅适用于单个对象.

[What is behind]
I've just wonder if there is some magic with implicit objects and Manifest to achieve the typeclass pattern without explicit call implicit object (like in Haskell). But only for single object.

我知道这是否是人造的,但我只是想知道是否有可能,因为混合了Scala功能的魔术很多.

I know if is artificial, but I've just wonder if it's possible because of lot of magic with mixing Scalas features.

推荐答案

这是不可能的.您可以使用 Dynamic特质或凯文·赖特(Kevin Wright)的自动代理插件,但是,无论哪种方式,您将创建一个 new 对象,该对象还可以通过代理来回答原始方法.

It is not possible. You may look at using the Dynamic trait or Kevin Wright's auto-proxy plugin, but, either way, you'll create a new object that also answers to the original one's method through proxying.

这篇关于Scala mixin到类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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