Re: Equivalents to policy classes in D

2012-04-21 Thread Joseph Rushton Wakeling

On 20/04/12 04:36, Joseph Rushton Wakeling wrote:

On 03/04/12 02:24, Cristi Cobzarenco wrote:

Mixins templates would be the answer:


OK, I've had a go at writing up some mixin template-based policy code of my own.


Oh, fun.  You can even have the constructor provided by a template mixin ... ! 
:-D


Re: Equivalents to policy classes in D

2012-04-19 Thread Andrej Mitrovic
On 4/20/12, Joseph Rushton Wakeling  wrote:
> That said, I'm surprised that it accepts the () given that the constructor
> does
> ask for a number.

You can use:
@disable this();

But it doesn't work in all cases:

struct Foo
{
@disable this();
this(int i) { }
}

void main()
{
Foo foo;  // error, OK
auto foo = Foo();  // no error
}

I don't know if this is by design..


Re: Equivalents to policy classes in D

2012-04-19 Thread Joseph Rushton Wakeling

On 20/04/12 04:41, Andrej Mitrovic wrote:

You're doing the equivalent of:
auto one = Foo;
but you need:
auto one = Foo();


Ah, clear.  I'm an idiot -- I wrote my own constructor requiring a number as 
input!

That said, I'm surprised that it accepts the () given that the constructor does 
ask for a number.  Even if I put in place an in-contract with assert(n > 0), it 
doesn't throw an exception ...


Re: Equivalents to policy classes in D

2012-04-19 Thread Andrej Mitrovic
On 4/20/12, Joseph Rushton Wakeling  wrote:
> void main()
> {
>   auto oneOne = MyStruct!(size_t, GenOne, WriteOne);
>   auto oneTwo = MyStruct!(double, GenOne, WriteTwo);
>   auto threeOne = MyStruct!(double, GenThree, WriteOne);
>   auto threeTwo = MyStruct!(long, GenThree, WriteTwo);
> }

You're doing the equivalent of:
auto one = Foo;
but you need:
auto one = Foo();

so:
auto oneOne   = MyStruct!(size_t, GenOne, WriteOne)();
auto oneTwo   = MyStruct!(double, GenOne, WriteTwo)();
auto threeOne = MyStruct!(double, GenThree, WriteOne)();
auto threeTwo = MyStruct!(long, GenThree, WriteTwo)();


Re: Equivalents to policy classes in D

2012-04-19 Thread Joseph Rushton Wakeling

On 03/04/12 02:24, Cristi Cobzarenco wrote:

Mixins templates would be the answer:


OK, I've had a go at writing up some mixin template-based policy code of my own.

However ... it winds up with compilation errors that I can't decipher:

  mixin.d(55): Error: type MyStruct!(ulong,GenOne,WriteOne) has no value
  mixin.d(56): Error: type MyStruct!(double,GenOne,WriteTwo) has no value
  mixin.d(57): Error: type MyStruct!(double,GenThree,WriteOne) has no value
  mixin.d(58): Error: type MyStruct!(long,GenThree,WriteTwo) has no value
  /usr/local/include/d2/std/conv.d(244): Error: template std.conv.toImpl does 
not match any function template declaration
  /usr/local/include/d2/std/conv.d(252): Error: template std.conv.toImpl cannot 
deduce template function from argument types !(string)(ubyte)
  /usr/local/include/d2/std/conv.d(244): Error: template instance 
toImpl!(string) errors instantiating template



Here's the code.  What am I doing wrong ... ?

/
import std.stdio;

mixin template GenOne(T)
{
void generate()
{
foreach(size_t i, ref T x; this.array)
x = i;
}
}

mixin template GenThree(T)
{
void generate()
{
foreach(size_t i, ref T x; this.array)
x = 3*i;
}
}

mixin template WriteOne(T)
{
void myWrite()
{
foreach(T x; this.array)
writeln(x);
}
}

mixin template WriteTwo(T)
{
void myWrite()
{
foreach(T x; this.array)
writeln(2*x);
}
}

struct MyStruct(T, alias GeneratePolicy, alias WritePolicy)
{
private T[] array;

this(size_t n)
{
array.length = n;
generate;
}

mixin GeneratePolicy!T;
mixin WritePolicy!T;
}

void main()
{
auto oneOne = MyStruct!(size_t, GenOne, WriteOne);
auto oneTwo = MyStruct!(double, GenOne, WriteTwo);
auto threeOne = MyStruct!(double, GenThree, WriteOne);
auto threeTwo = MyStruct!(long, GenThree, WriteTwo);

oneOne.myWrite;
writeln;
oneTwo.myWrite;
writeln;
threeOne.myWrite;
writeln;
threeTwo.myWrite;
writeln;
}

/


Re: Equivalents to policy classes in D

2012-04-04 Thread Joseph Rushton Wakeling
Thanks to all for the useful suggestions here.  I'll have a play with the ideas 
suggested and come back if problems arise ... :-)


Re: Equivalents to policy classes in D

2012-04-03 Thread Jacob Carlborg

On 2012-04-03 01:15, Joseph Rushton Wakeling wrote:

Hello all,

I'm coming to D from a background programming in C and C++, though I
wouldn't describe myself as an expert in either.

One of the C++ techniques I picked up over the last couple of years was
the use of policy classes, and I'm wondering how D addresses this issue
of combining various small components together to implement a given
interface.

D's interfaces seem an obvious starting point, but from the
documentation I've read, it seems like each implementation has to be
written separately. So, if I have an interface,

interface FooBar {
void foo();
void bar();
}

... I can of course write two different implementations,

class FooBarOne : FooBar {
override void foo() {
// Foo function implementation
...
}
override void bar() {
// Bar function implementation
...
}
}

class FooBarTwo : FooBar {
override void foo() {
// Foo function implementation
...
}
override void bar() {
// Bar function implementation
...
}
}

... but suppose that I'd like the foo() function to be identical in
both; how do I avoid rewriting the code?

In C++ I'd think of a policy class,

template 
class FooBar : public Foo, public Bar {
...
};

and then have,

typedef FooBar FooBarOne;

typedef FooBar FooBarTwo;

... but I don't see how to do something equivalent with D's interfaces
and implementations. Can anyone advise?

Thanks and best wishes,

-- Joe


Maybe you could use template mixins to implement "foo".

mixin template Foo ()
{
void foo () {}
}

class FooBarOne : FooBar
{
mixin Foo;
}

--
/Jacob Carlborg


Re: Equivalents to policy classes in D

2012-04-02 Thread Henry Robbins Gouk
I'm not all that familiar with policy classes but, if I 
understand what you are asking correctly, you can provide 
implementations in interfaces if the methods are final or static.


Is this the sort of thing you mean?

import std.stdio;

interface Foo
{
final string foo()
{
return "foo";
}
}

interface Bar
{
final string bar()
{
return "bar";
}
}

template IsInterface(T)
{
enum IsInterface = is(T == interface);
}

class Policy(T, U) if(IsInterface!T && IsInterface!U) : T, U
{

}

alias Policy!(Foo, Bar) FooBar;

void main()
{
auto fb = new FooBar();
writeln(fb.foo(), " and ", fb.bar());
}


Re: Equivalents to policy classes in D

2012-04-02 Thread James Miller
On 3 April 2012 12:24, Cristi Cobzarenco  wrote:
> Mixins templates would be the answer:
> import std.exception;
>
> mixin template UncheckedIndices( T ) {
>   ref T opIndex( int i ) {
>     return this.get_( i );
>   }
>
> }
>
> mixin template CheckedIndices( T ) {
>   ref T opIndex( int i ) {
>     enforce( i > 0 && i < this.size );
>     return this.get_( i );
>   }
> }
>
> struct Array( T, alias IndexPolicy ) {
>   private T* array;
>   private int size_;
>
>   this( size_t n ) {
>     array = (new T[ n ]).ptr;
>   }
>
>   @property size_t size() {
>      return size_;
>   }
>
>   private ref T get_( size_t i ) {
>     return array[ i ];
>   }
>
>  mixin IndexPolicy!T;
> }
>
> void main() {
> auto arr1 = Array!(int, UncheckedIndices)( 10 );
> auto arr2 = Array!(int, CheckedIndices)( 10 );
> arr2[ 11 ] = 5;
> }

Another option is to use alias this to forward method calls on.
Currently you can only have 1 alias this in a class/struct, but that
is a temporary and D is supposed to allow multiple alias this
declarations.

Depending on the scope of your situation, you could also use
conditional compilation in conjunction with templates to implement
your policies, often interfaces aren't needed when you only want/need
a minor change, I only use them for polymorphism in D.

--
James Miller


Re: Equivalents to policy classes in D

2012-04-02 Thread Cristi Cobzarenco
Mixins templates would be the answer:
import std.exception;

mixin template UncheckedIndices( T ) {
  ref T opIndex( int i ) {
return this.get_( i );
  }

}

mixin template CheckedIndices( T ) {
  ref T opIndex( int i ) {
enforce( i > 0 && i < this.size );
return this.get_( i );
  }
}

struct Array( T, alias IndexPolicy ) {
  private T* array;
  private int size_;

  this( size_t n ) {
array = (new T[ n ]).ptr;
  }

  @property size_t size() {
 return size_;
  }

  private ref T get_( size_t i ) {
return array[ i ];
  }

 mixin IndexPolicy!T;
}

void main() {
 auto arr1 = Array!(int, UncheckedIndices)( 10 );
auto arr2 = Array!(int, CheckedIndices)( 10 );
 arr2[ 11 ] = 5;
}




---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco



On 3 April 2012 00:15, Joseph Rushton Wakeling  wrote:

> Hello all,
>
> I'm coming to D from a background programming in C and C++, though I
> wouldn't describe myself as an expert in either.
>
> One of the C++ techniques I picked up over the last couple of years was
> the use of policy classes, and I'm wondering how D addresses this issue of
> combining various small components together to implement a given interface.
>
> D's interfaces seem an obvious starting point, but from the documentation
> I've read, it seems like each implementation has to be written separately.
>  So, if I have an interface,
>
>  interface FooBar {
>void foo();
>void bar();
>  }
>
> ... I can of course write two different implementations,
>
>  class FooBarOne : FooBar {
>override void foo() {
>  // Foo function implementation
>  ...
>}
>override void bar() {
>  // Bar function implementation
>  ...
>}
>  }
>
>  class FooBarTwo : FooBar {
>override void foo() {
>  // Foo function implementation
>  ...
>}
>override void bar() {
>  // Bar function implementation
>  ...
>}
>  }
>
> ... but suppose that I'd like the foo() function to be identical in both;
> how do I avoid rewriting the code?
>
> In C++ I'd think of a policy class,
>
>  template 
>  class FooBar : public Foo, public Bar {
>...
>  };
>
> and then have,
>
>  typedef FooBar FooBarOne;
>
>  typedef FooBar FooBarTwo;
>
> ... but I don't see how to do something equivalent with D's interfaces and
> implementations.  Can anyone advise?
>
> Thanks and best wishes,
>
>-- Joe
>


Equivalents to policy classes in D

2012-04-02 Thread Joseph Rushton Wakeling

Hello all,

I'm coming to D from a background programming in C and C++, though I wouldn't 
describe myself as an expert in either.


One of the C++ techniques I picked up over the last couple of years was the use 
of policy classes, and I'm wondering how D addresses this issue of combining 
various small components together to implement a given interface.


D's interfaces seem an obvious starting point, but from the documentation I've 
read, it seems like each implementation has to be written separately.  So, if I 
have an interface,


  interface FooBar {
void foo();
void bar();
  }

... I can of course write two different implementations,

  class FooBarOne : FooBar {
override void foo() {
  // Foo function implementation
  ...
}
override void bar() {
  // Bar function implementation
  ...
}
  }

  class FooBarTwo : FooBar {
override void foo() {
  // Foo function implementation
  ...
}
override void bar() {
  // Bar function implementation
  ...
}
  }

... but suppose that I'd like the foo() function to be identical in both; how do 
I avoid rewriting the code?


In C++ I'd think of a policy class,

  template 
  class FooBar : public Foo, public Bar {
...
  };

and then have,

  typedef FooBar FooBarOne;

  typedef FooBar FooBarTwo;

... but I don't see how to do something equivalent with D's interfaces and 
implementations.  Can anyone advise?


Thanks and best wishes,

-- Joe