traits

Semantics of abstract traits in Scala

℡╲_俬逩灬. 提交于 2019-12-10 01:28:42
问题 I am wondering what the semantics of using the abstract keyword in combination with a trait is. If the trait does not define any abstract methods, the abstract keyword does not prevent me from creating an instance: scala> abstract trait T defined trait T scala> new T{} res0: java.lang.Object with T = $anon$1@12cd927d On the other hand, if the trait does define an abstract method, I cannot create an instance (without implementing this method of course) no matter if the abstract keyword is

What are stackable modifications?

纵饮孤独 提交于 2019-12-09 16:02:44
问题 I've been reading a book about Scala and there's mention of stackable modifications using traits . What are stackable modifications and for what purposes are they meant to be used? 回答1: The fundamental quality which distinguishes stackable modifications (as the terminology is used in scala anyway) is that "super" is influenced dynamically based on how the trait is mixed in, whereas in general super is a statically determined target. If you write abstract class Bar { def bar(x: Int): Int }

What are possible use scenarios for Traits in PHP? [duplicate]

三世轮回 提交于 2019-12-09 12:07:51
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: traits in php – any real world examples/best practices? In what kind of situations would one use Traits in PHP? I do have a pretty good overall idea of this, but I can't seem to think of a way to use them in an application I have written, but that may be because it does not need traits at the time. One scenario I have realized that needs traits: Events. Instead of having one class that implements the observer

What does “Box<Fn() + Send + 'static>” mean in rust?

十年热恋 提交于 2019-12-09 09:45:14
问题 What does Box<Fn() + Send + 'static> mean in rust? I stumbled upon this syntax while reading advanced types chapter. Send is a trait but what does it mean to + a lifetime to a trait ( 'static in this case) in type parametrization ? Also what is Fn() ? 回答1: Let's decompose it one-by-one. Box Box<T> is a pointer to heap-allocated T . We use it here because trait objects can only exist behind pointers. Trait objects In Box<Fn() + Send + 'static> , Fn() + Send + 'static is a trait object type. In

What are the typical use cases of an iterator_trait

北城以北 提交于 2019-12-09 07:52:06
问题 I am new to C++ so please bear with me. I am trying to understand STL iterator_traits. In the book "The C++ Standard Library" the structure iterator_traits is defined as follows: template <class T> struct iterator_traits{ typedef typename T::value_type value_type typedef typename T::difference_type difference_type typedef typename T::iterator_category iterator_category typedef typename T::pointer pointer typedef typename T::reference reference } So it seems to me that it is re-exposing the

Get the signed/unsigned variant of an integer template parameter without explicit traits

怎甘沉沦 提交于 2019-12-09 05:08:45
问题 I am looking to define a template class whose template parameter will always be an integer type. The class will contain two members, one of type T , and the other as the unsigned variant of type T -- i.e. if T == int , then T_Unsigned == unsigned int . My first instinct was to do this: template <typename T> class Range { typedef unsigned T T_Unsigned; // does not compile public: Range(T min, T_Unsigned range); private: T m_min; T_Unsigned m_range; }; But it doesn't work. I then thought about

Traits - property conflict with parent class

天大地大妈咪最大 提交于 2019-12-09 02:21:58
问题 I have this class Zgh\FEBundle\Entity\User which extends FOS\UserBundle\Model\User . use FOS\UserBundle\Model\User as BaseUser; class User extends BaseUser implements ParticipantInterface { use BasicInfo; // .. } And BaseUser class: abstract class User implements UserInterface, GroupableInterface { protected $id; // .. } And BaseInfo trait: trait BasicInfo { /** * @ORM\Column(type="string", length=255) * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ protected $id; // .. } But when I run

Why do I get a `trait bound `[T]: std::marker::Sized` is not satisfied when I try and implement Ord and Eq manually on unsized types?

亡梦爱人 提交于 2019-12-08 18:23:11
问题 When creating a struct which stores a DST (e.g., a raw slice), I can use the normal #[derive(Eq, PartialEq, Ord, PartialOrd)] facility to get implementations of this trait on my type: #[derive(PartialEq, Eq, PartialOrd, Ord)] struct A([u8]); However, if I implement them manually, then the compiler will complain that my type does not implement Sized : struct A([u8]); impl AsRef<[u8]> for A { fn as_ref(&self) -> &[u8] { &self.0 } } impl<S: AsRef<[u8]>> PartialEq<S> for A { fn eq(&self, other:

Group class template specializations

﹥>﹥吖頭↗ 提交于 2019-12-08 15:21:17
问题 Is there a technique / best style to group class template specializations for certain types ? An example : Lets say I have a class template Foo and I need to have it specialized the same for the typeset A = { Line, Ray } and in another way for the typeset B B = { Linestring, Curve } What I'm doing so far : (the technique is also presented here for functions) #include <iostream> #include <type_traits> using namespace std; // 1st group struct Line {}; struct Ray {}; // 2nd group struct Curve {}

Why does returning `Self` in trait work, but returning `Option<Self>` requires `Sized`?

别等时光非礼了梦想. 提交于 2019-12-08 15:05:01
问题 This trait definition compiles fine: trait Works { fn foo() -> Self; } This, however, does lead to an error: trait Errors { fn foo() -> Option<Self>; } error[E0277]: the size for values of type `Self` cannot be known at compilation time --> src/lib.rs:6:5 | 6 | fn foo() -> Option<Self>; | ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `Self` = note: to learn more, visit <https://doc.rust-lang.org/book/second