context-bound

How do I get an instance of the type class associated with a context bound?

天大地大妈咪最大 提交于 2019-12-17 10:24:13
问题 Note: I'm posing this question to answer it myself, but other answers are welcome. Consider the following simple method: def add[T](x: T, y: T)(implicit num: Numeric[T]) = num.plus(x,y) I can rewrite this using a context bound as follows def add[T: Numeric](x: T, y: T) = ??.plus(x,y) but how do I get an instance of the Numeric[T] type so that I can invoke the plus method? 回答1: Using the implicitly method The most common and general approach is to use the implicitly method, defined in Predef:

How to get ClassTag form TypeTag, or both at same time?

我的未来我决定 提交于 2019-12-17 07:43:06
问题 I have some code like this: class ReflectiveJsonFormat[T:TypeTag] extends JsonFormat[T] { def write(x: T) : JsValue = { val t = typeOf[T] val getters = t.declarations.filter { s => s.isMethod && s.asMethod.isGetter } val mirror = runtimeMirror(this.getClass.getClassLoader) val instanceMiror = mirror.reflect(x) } } That last line fails with: No ClassTag available for T I thought TypeTag was more info than a ClassTag ? Can I get the ClassTag from the TypeTag ? If not, is there some syntax for

Chain of events / Proxy to original object

孤街醉人 提交于 2019-12-10 11:07:37
问题 I have a class which is inherited from context bound object. Class has attribute on some properties. When some property is changed, PostProcess(IMessage msg, IMessage msgReturn) raise an event and from the event again a new property with same attribute is fired. Second change should also call PostProcess , but it is not happening. Probably because, the object where second property is changed is not original .net object but MarshalByRefObject / ContextBoundObject / Proxy Object . My query is

“:” in type parameter

时间秒杀一切 提交于 2019-12-10 02:56:34
问题 In scala-arm project, I see code like this: def managed[A : Resource : Manifest](opener : => A) : ManagedResource[A] = new DefaultManagedResource(opener) Can someone explain the meaning of [A : Resource : Manifest] ? 回答1: def managed[A : Resource : Manifest](opener : => A) : ManagedResource[A] = new DefaultManagedResource(opener) means def managed[A](opener : => A)(implicit r: Resource[A], m: Manifest[A]) : ManagedResource[A] = new DefaultManagedResource(opener) You can look link text 7.4

Situations when Manifest not available

瘦欲@ 提交于 2019-12-08 20:22:49
问题 def bar[T: Manifest](a: Array[T]) = Array.ofDim[T](3) class Foo bar(Array(new Foo)) //Array[Foo] = Array(null, null, null) Manifests seem to exist implicitly for arbitrary types, as shown above. Since we have a context bound, this implies that there will be some types for which there is no implicit Manifest - what are they? 回答1: A Manifest has to be "carried" from the point where the concrete type last appears in the source code, all the way through type parameters to the place where it is

Transparent proxy to original type

十年热恋 提交于 2019-12-07 13:11:00
问题 I have an run time object of type {System.Runtime.Remoting.Proxies.__TransparentProxy} which is created from an instance of class which is inherited from ContextBoundObject. This class raise an event to some other object, I need to convert this proxy object to original object. All objects are in default AppDomain on single system. public abstract class ObjectBase : ContextBoundObject, IObjectBase { } public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next) { _context =

“:” in type parameter

拟墨画扇 提交于 2019-12-05 05:25:55
In scala-arm project, I see code like this: def managed[A : Resource : Manifest](opener : => A) : ManagedResource[A] = new DefaultManagedResource(opener) Can someone explain the meaning of [A : Resource : Manifest] ? Eastsun def managed[A : Resource : Manifest](opener : => A) : ManagedResource[A] = new DefaultManagedResource(opener) means def managed[A](opener : => A)(implicit r: Resource[A], m: Manifest[A]) : ManagedResource[A] = new DefaultManagedResource(opener) You can look link text 7.4 Context Bounds and View Bounds for more information. Using a simpler example to illustrate: def method

How do I get an instance of the type class associated with a context bound?

眉间皱痕 提交于 2019-11-27 11:30:17
Note: I'm posing this question to answer it myself, but other answers are welcome. Consider the following simple method: def add[T](x: T, y: T)(implicit num: Numeric[T]) = num.plus(x,y) I can rewrite this using a context bound as follows def add[T: Numeric](x: T, y: T) = ??.plus(x,y) but how do I get an instance of the Numeric[T] type so that I can invoke the plus method? Aaron Novstrup Using the implicitly method The most common and general approach is to use the implicitly method , defined in Predef: def add[T: Numeric](x: T, y: T) = implicitly[Numeric[T]].plus(x,y) Obviously, this is

Context bounds shortcut with higher kinded-types

徘徊边缘 提交于 2019-11-27 11:20:56
问题 Is it possible to use the context bounds syntax shortcut with higher kinded-types? trait One { def test[W : ClassManifest]: Unit } // first-order ok trait Two { def test[W[_]: ClassManifest]: Unit } // not possible?? trait Six { def test[W[_]](implicit m: ClassManifest[W[_]]): Unit } // hmm... 回答1: Yes, it is, but your context bound type must have a higher kinded type parameter (which ClassManifest doesn't). scala> trait HKTypeClass[CC[_]] defined trait HKTypeClass scala> implicit def

How to get ClassTag form TypeTag, or both at same time?

怎甘沉沦 提交于 2019-11-27 05:23:54
I have some code like this: class ReflectiveJsonFormat[T:TypeTag] extends JsonFormat[T] { def write(x: T) : JsValue = { val t = typeOf[T] val getters = t.declarations.filter { s => s.isMethod && s.asMethod.isGetter } val mirror = runtimeMirror(this.getClass.getClassLoader) val instanceMiror = mirror.reflect(x) } } That last line fails with: No ClassTag available for T I thought TypeTag was more info than a ClassTag ? Can I get the ClassTag from the TypeTag ? If not, is there some syntax for saying that T has two context bounds -- both TypeTag and ClassTag ? Or, how would you otherwise fix this