traits

C++ Design Question on template types

I have a templated class template <typename T> class MyContainerClass For types to be substituted for T, it has to satisfy many requirements: for example, get_id(), int data(), etc. Obviously none of the fundamental types (PODs) are substitutable. One way I can provide this is via wrappers for the PODs that provide these func...

C++ traits question

I have a templated class template <typename Data> class C { ..... } In most situations, I depend on the compiler to let me substitute types for Data. I call methods foo(), goo() on objects of type Data, so what I substitute needs to provide that. I now need to substitute int and string for my Data type. I do not want to specialize ...

How to tell if a class uses a Trait

If I have a Trait that some classes use but not others, how can I test an object to see if it is an instance of a class that uses that Trait? What I want is something like isMemberOf: or isKindOf: but for Traits. ...

C++ template specialization of templated types

I'm looking to help users of some of my templated code by using BOOST_STATIC_ASSERT to let them know that they've used an incompatible type with a simpler compile error message than the monster than is currently produced with an incompatible type. The example is a bit too complex to reproduce here, but hopefully this will capture the es...

Difference between Scala trait and C++ concepts

What is the difference between Scala traits Haskell type class and C++0x Concepts? ...

A question on traits

What is the difference between following two? 1# trait B extends A { } 2# trait B { self: A => } where A is an abstract class. >> EDIT: Please explain with respect to the following example of Ducks with pluggable flying and quacking behaviors: abstract class Duck { def fly(): Unit def quack(): Unit def swim() { pri...

C++ boost template parameter traits

hello. I believe I had seen macro in boost that recovers template template parameters, for example: template<class> struct parameters; #define parameters(T) template<class A> \ struct parameters<T<A> > { typedef A type1; }; is there one like this, or am I wrong? Thank you ...

Implementing an abstract method with a trait, inconsistent compiler behaviour?

I have a base class that comes from a Java library, whose code I cannot modify. This class (A) has an empty method (b) which should have been declared as abstract instead: class A { def b { } } I extend this class in Scala and override the method to make it abstract: abstract class AA extends A { override def b } Now I implemen...

With Traits, should I use a T prefix and if yes put it before or after any other prefix?

When creating Traits in Pharo+Squeak, is it proper to use a T prefix like TMyTrait and if yes, should the T go before any other prefix like TMPMyTrait (where "MP" is the other prefix), or after, like MPTMyTrait ...

Are Traits good or bad?

This is an open-ended question, but I would like to solicit some opinions from the SO community on Traits; do you think Traits in Squeak/Pharo are a good thing, or should you stay away from them and use composition and delegation instead? I ask because while I know how to use them (thanks to the Pharo book), I am not really sure how acce...

How do I create an instance of a trait in a generic method in scala?

I'm trying to create an instance of a trait using this method val inst = new Object with MyTrait This works well, but I'd like to move this creation in to a generator function, ie. object Creator { def create[T] : T = new Object with T } I'm obviously going to need the manifest to somehow fix the type erasure problems, but before...

Python: Metaclasses all the way down

I have an esoteric question involving Python metaclasses. I am creating a Python package for web-server-side code that will make it easy to access arbitrary Python classes via client-side proxies. My proxy-generating code needs a catalog of all of the Python classes that I want to include in my API. To create this catalog, I am using ...

CGAL half-edge data structure

Hello, I am trying to learn how to use half-edge data structures in CGAL, and I am confused by the following sample of code provided on their website #include <CGAL/HalfedgeDS_default.h> #include <CGAL/HalfedgeDS_decorator.h> struct Traits { typedef int Point_2; }; typedef CGAL_HALFEDGEDS_DEFAULT<Traits> HDS; typedef CGAL::HalfedgeDS...

Usign traits with a factory

I'm currently discovering scala and I was wondering if I could use traits with a factory. I tried this : abstract class Foo { ... } object Foo { def apply() = new Bar private class Bar extends Foo { ... } } Foo() with MyTrait // Not working I guess it's because with must be preceded by new. So is there any way to do t...

Use different sets of functions based on template parameters (C++ traits?)

I have defined a class in C++ which holds an array of scalars of type T for which I want to define operators like sin, cos, etc. For defining the meaning of sin applied on an object of this class I need to know the meaning of sin applied on the single scalar type T. This means I need to use appropriate math libraries (corresponding to th...

integer traits (is_integer, is_integral)

I need two traits concerning integers. The first one would be like std::is_integral (or boost::is_integral), but usable with user defined types (for example a class wrapping an int, say int_wrapper): true if the type behaves like an integer and whose representation is like standard integral types (e.g. sizeof(T) * CHAR_BITS == std::num...

Scala immutable objects and traits with val fields

Hi all, I would like to construct my domain model using immutable objects only. But I also want to use traits with val fields and move some functionality to traits. Please look at the following example: trait Versionable { val version = 0 def incrementVersion = copy(version=version+1) } Unfortunatelly such code doesn't work - copy ...

Extending built-in collections, issues with built-in methods

I am a Scala novice so forgive me if this is a stupid question, but here goes... Imagine I wish to create an extended Map type that includes additional methods. I can see a few ways to do this. The first would be with composition: class Path[V](val m: Map[V, Int]) { // Define my methods } Another would be via inheritance, e.g. c...

Can I order order method modifiers loaded as part of traits?

This is a follow up to a previous question. if I have multiple plugins/traits with around modifiers, is it possible to ensure a certain execution order (seeing as how I can't be sure which will actually get loaded)? or can I really only control that in code I write and with documentation? Example: I have 3 Roles each with an around and ...

Is there a way in scala to produce a generic instance without an example instance?

I was playing with creating a generic factory as follows: trait Factory[T] { def createInstance():T = new T() } val dateFactory = new Factory[Date](){} val myDate = dateFactory.createInstance() The 'new T()' doesn't compile, as T is undefined until runtime. I know that I can get it to work by passing in an instance of the class to so...