traits

java traits or mixins pattern?

别等时光非礼了梦想. 提交于 2019-11-29 03:31:10
Is there a way to emulate mixins or traits in java? basically, I need a way to do multiple inheritance so I can add common business logic to several classes Alex B I would encapsulate all of the business logic into a new class BusinessLogic and have each class that needs BusinessLogic make calls to the class. If you need a single rooted heirarchy for your classes that make calls to BusinessLogic , you'll have to create an interface as well ( BusinessLogicInterface ?) In pseudo-code: interface BusinessLogicInterace { void method1(); void method2(); } class BusinessLogic implements

Get all the classes that implements a trait in Scala using reflection

柔情痞子 提交于 2019-11-29 02:08:06
I want to list out all the case classes which implements a particular trait. I am currently using Clapper ClassUtil for doing that. I am able to get the case classes that are directly implementing a trait. However, I am not able to get the other classes which are not directly implementing the trait. How can I get all classes which directly or indirectly implements a trait. ? val finder = ClassFinder() finder.getClasses().filter(_.isConcrete).filter(_.implements("com.myapp.MyTrait")) Scala Version : 2.11 Clapper Class Util Version : 1.0.6 Is there any other way I can get these information? Can

Difference between <T: Trait> and where T: Trait

爱⌒轻易说出口 提交于 2019-11-29 02:06:00
In the docs for the Send trait, I see both impl<T> Send for LinkedList<T> where T: Send, and impl<T: Send> Send for LinkedList<T> What is the difference between these two syntaxes, and how would it impact my code if I was writing impl declarations for my own trait? Trait bounds defined inside a where clause are a superset of the trait bounds declared inline. The inline style existed before the where clause; the where clause was introduced in RFC 135 : Add where clauses, which provide a more expressive means of specifying trait parameter bounds. [...] The existing bounds notation would remain

篮子、水果和鸡蛋——关于C++的模板偏特化和萃取编程技法

江枫思渺然 提交于 2019-11-29 01:48:52
最近在读《STL源码剖析》。读这本书的时候发现自己的C++的知识其实是非常匮乏的。 从大学的C++教材上学到一些C++基本的语法、内存管理、继承、多态等方面的基础知识。这些只是是一棵大树的根。而读STL的源码和侯捷的解析的时候,发现C++还有很多丰富的细节和技巧。这些是大树上的枝叶。学习C++,不仅要学习根,也要学习枝叶,这样才能让大树茂盛起来。虽然C++语法一些用法较为晦涩,但读完这些代码之后觉得思路比以前更开阔,另外可以活动脑筋。 比如模板的偏特化这个特性。侯捷的《STL源码剖析》中对于模板的偏特化(partial specialization)的解释为: 如果class template拥有一个以上的template参数,我们可以针对其中某个或多个 template参数进行特化工作。template是一个很抽象的东西。template偏特化之后就让模板变得具体那么一点点。 用一个形象一点的比喻吧。我们把template比作一个装东西的篮子。这个篮子既可以装鸡蛋,也可以装苹果。那么所谓偏特化就是让你用一个篮子专门装水果,这就是template水果篮。以后你就只能使用水果篮来装苹果、装梨,而不能使用其他的篮子来装这些水果了。 看一个STL例子: 有一个“篮子” iterator_traits, 它内部typedef 了value_type类型,用来定义模板的参数类型class I

Does Objective-C support traits/mixins?

女生的网名这么多〃 提交于 2019-11-29 00:49:31
问题 Are there any techniques for emulating traits or mixins in Objective-C? In Scala, for example, I can do something like this: trait ControllerWithData { def loadData = ... def reloadData = ... def elementAtIndex = ... } trait ControllerWithStandardToolbar { def buildToolbar = ... def showToolbar = ... def hideToolbar = ... } class MyTableController extends ControllerWithData with ControllerWithStandardToolbar { def loadView = { super.loadView loadData buildBar } } It's basically a way to

PHP instanceof for traits

自古美人都是妖i 提交于 2019-11-29 00:08:22
问题 What is the proper way to check if a class uses a certain trait? 回答1: While nothing stops you from using instanceof with traits, the recommended approach is to pair traits with interfaces. So you'd have: class Foo implements MyInterface { use MyTrait; } Where MyTrait is an implementation of MyInterface . Then you check for the interface instead of traits like so: if ($foo instanceof MyInterface) { ... } And you can also type hint, which you can't with traits: function bar(MyInterface $foo) {

What does “trait A <: B” mean?

和自甴很熟 提交于 2019-11-28 23:19:04
In Scala, what does trait A <: B mean? Is it just the same as trait A extends B ? Edited to add: I'm familiar with the syntax for type parameters, and what <: means in that context. However, in the above example it would seem to me that A is the name of the trait being declared, not a type parameter. retronym NOTE As of Scala 2.12.5 using <: for extends is deprecated scala -deprecation -e 'trait B; trait A <: B' /var/folders/0w/kb0d3rqn4zb9fcc91pxhgn8w0000gn/T/scalacmd2374381600671257557.scala:1: warning: Using `<:` for `extends` is deprecated trait B; trait A <: B ^ one warning found Seems to

How to use stackable trait pattern with Akka actors?

折月煮酒 提交于 2019-11-28 21:17:51
问题 I'm trying to implement a Pub/Sub trait to mix into other akka actors using a stackable trait. Here is what I came up with: trait PubSubActor extends Actor { abstract override def receive = super.receive orElse { case Subscribe(topic) => /* ... */ case Publish(topic, msg) => /* ... */ } } class MyActor extends Actor with PubSubActor { override def receive = { case SomeMessage(a, b, c) => /* ... */ } } At which point, the compiler throws back an error: error: overriding method receive in trait

Building a Singleton Trait with PHP 5.4

落爺英雄遲暮 提交于 2019-11-28 19:20:35
We recently had a discussion if it was possible to build a trait Singleton PHP Traits and we played around with it a possible Implementation but ran into issues with building one. This is an academic exercise. I know that Singletons have very little - if not to say no - use in PHP and that one should 'just create one' but just for exploring the possibilities of traits: <?php trait Singleton { protected static $instance; final public static function getInstance() { return isset(static::$instance) ? static::$instance : static::$instance = new static; } final private function __construct() {

Scala case class extending Product with Serializable

谁说我不能喝 提交于 2019-11-28 17:17:52
I am learning scala and tried following form Scala Cookbook: trait Animal trait FurryAnimal extends Animal case class Dog(name:String) extends Animal case class Cat(name:String) extends Animal Now when I did following as : val x = Array(Dog("Fido"),Cat("Felix")) it show result as : x:Array[Product with Serializable with Animal] = Array(Dog(Fido),Cat(Felix)) Although I know that a case class is mixed in with Product trait What I am not getting is : Product with Serializable with Animal As per my understanding Product has something to do with Pattern matching I did google it but didn't get