D versus C#/.NET module systems

2012-05-17 Thread Eduardo Cavazos
This question on "programmers.stackexchange.com" questions the complexity
of the C#/.NET namespace/module/dll system in relation to more elegant
approaches taken by other languages.

http://programmers.stackexchange.com/questions/149056/why-do-net-modules-separate-module-file-names-from-namespaces

I'm not an expert in the D programming language, but i's module system
seems to be nowhere near as baroque as the C#/.NET way.

If anyone would care to chime in regarding D on that stack exchange
question, it would be appreciated.


Overloading + on points

2010-09-02 Thread Eduardo Cavazos

Eduardo Cavazos  wrote:


Here's a short program which creates a type for points and overloads

'+'

to do element wise addition as well as addition to floats, however it
produces an error:

--
import std.stdio ;

struct Pt
{
   float x , y ;

   Pt opBinary ( string op ) ( Pt a ) if ( op == "+" )
 { return Pt ( x + a.x , y + a.y ) ; }

   Pt opBinary ( string op ) ( float a ) if ( op == "+" )
 { return Pt ( x + a , y + a ) ; }
}

void main () { Pt ( 1.0 , 2.0 ) + Pt ( 3.0 , 4.0 ) ; }
--

The error:

pt_overload_test_a.d(15): Error: template instance opBinary!("+")
matches more than one template declaration,
pt_overload_test_a.d(8):opBinary(string op) if (op == "+") and
pt_overload_test_a.d(11):opBinary(string op) if (op == "+")

So, how should I go about this? :-)


Simen kjaeraas wrote:


That is indeed a perplexing error. It is caused by dmd not knowing
how to overload template functions based on both normal and template
parameters.

As for the solution:

Pt opBinary( string op : "+", T : Pt )( T a ) {...}
Pt opBinary( string op : "+", T : float )( T a ) {...}

should work. More explicitly:

Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == Pt ) )
{...}
Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == float )
) {...}


Is either one of those solutions preferrable? Or are they equivalent? 
I'd like to get these right now since there are a bunch more like this 
(for all the various operators and combinations).


For this experiment, I was going off of the examples in TDPL and I 
hadn't noticed yet one which covers a case like this; perhaps for 2nd 
edition. ;-)


Ed


Overloading + on points

2010-08-31 Thread Eduardo Cavazos
(Discussion originally on digitalmars-d-learn. Moving to digitalmars-d 
list.)


Eduardo Cavazos  wrote:

> Here's a short program which creates a type for points and overloads 
'+'

> to do element wise addition as well as addition to floats, however it
> produces an error:
>
> --
> import std.stdio ;
>
> struct Pt
> {
>float x , y ;
>
>Pt opBinary ( string op ) ( Pt a ) if ( op == "+" )
>  { return Pt ( x + a.x , y + a.y ) ; }
>
>Pt opBinary ( string op ) ( float a ) if ( op == "+" )
>  { return Pt ( x + a , y + a ) ; }
> }
>
> void main () { Pt ( 1.0 , 2.0 ) + Pt ( 3.0 , 4.0 ) ; }
> --
>
> The error:
>
> pt_overload_test_a.d(15): Error: template instance opBinary!("+")
> matches more than one template declaration,
> pt_overload_test_a.d(8):opBinary(string op) if (op == "+") and
> pt_overload_test_a.d(11):opBinary(string op) if (op == "+")
>
> So, how should I go about this? :-)

Simen kjaeraas wrote:


That is indeed a perplexing error. It is caused by dmd not knowing
how to overload template functions based on both normal and template
parameters.

As for the solution:

Pt opBinary( string op : "+", T : Pt )( T a ) {...}
Pt opBinary( string op : "+", T : float )( T a ) {...}

should work. More explicitly:

Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == Pt ) )
{...}
Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == float )
) {...}


Thanks for the help Simen!

Is it a bug that D doesn't handle the orignal code?

Ed


map on fixed-size arrays

2010-08-21 Thread Eduardo Cavazos

Hello,

The 'map' from std.algorithm doesn't seem to work with fixed-size arrays:

--
import std.stdio ;
import std.math ;
import std.algorithm ;

T sq ( T ) ( T x ) { return x*x ; }

void main ()
{
  double [2] a = [ 1.0 , 2.0 ] ;

  writeln ( map ! ( sq ) ( a ) ) ;
}
--

$ rdmd test_map_sq_fixed_size_b.d
/usr/include/d/dmd/phobos/std/algorithm.d(108): Error: template instance 
Map!(sq,double[2u]) does not match template declaration Map(alias 
fun,Range) if (isInputRange!(Range))


Is this an intended limitation?

Ed


The type of an element-wise arithmetic expression

2010-08-21 Thread Eduardo Cavazos

Hello,

This program:

--
import std.stdio ;

void f0 ( double [2] a ) { writeln ( a ) ; }

void main ()
{
  double [2] a = [ 1.0 , 2.0 ] ;
  double [2] b = [ 3.0 , 4.0 ] ;

  f0 ( a[] - b[] ) ;
}
--

produces an error:

$ rdmd array_wise_a.d
array_wise_a.d(11): Error: function array_wise_a.f0 (double[2u] a) is 
not callable using argument types (double[])
array_wise_a.d(11): Error: cannot implicitly convert expression (a[] - 
b[]) of type double[] to double[2u]


Since the sub-expressions a[] and b[] are each of type double[2], I 
would expect the the type of a[]-b[] to also be of double[2], and thus 
compatible with f0.


Ed


re: Scope/block behaviour

2010-08-21 Thread Eduardo Cavazos

Andrei Alexandrescu wrote:

> Mind submitting a bug report please?

Done:

http://d.puremagic.com/issues/show_bug.cgi?id=4699

Ed


re: Scope/block behaviour

2010-08-20 Thread Eduardo Cavazos


> void main ()
> {
>   { int f0 () { return 10 ; } }
>
>   { int f0 () { return 20 ; } }
> }

Stewart Gordon wrote:

> Looks like a bug.

The compiler recognizes the situation and reports it as an error, so it 
seems like this is not a bug, but something which is not supported:


~/scratch $ rdmd test_scope_a.d
test_scope_a.d(6): Error: declaration f0 is already defined in
another scope in main

The rationale in TDPL that Andrei pointed to doesn't seem to cover the 
case above.


Can anyone confirm that the above really is against the rules of the 
language?


Ed


re: welcome!

2010-08-19 Thread Eduardo Cavazos

Thanks Graham!

D is a lot of fun. Using it (language and compiler) is like a breath of 
fresh air. It raises the bar in so many ways.


Ed
--
Programming languages are like revolutions
And revolutions are like bicycles
When they stop moving forward, they fall over



TDPL ebook

2010-08-19 Thread Eduardo Cavazos

Hello,

Is there a legal/official way to purchase a full PDF of TDPL?

The Safari site says you get 45 days of access to the book online if you 
purchase the book. It also says that you can download individual 
chapters but you have to collect enough "tokens"...


Ed


Scope/block behaviour

2010-08-19 Thread Eduardo Cavazos

Hello,

I was surprised that these seem to not be allowed in D:

void main ()
{
  auto a = 20 ;

  {
auto a = 30 ;
  }
}

void main ()
{
  { int f0 () { return 10 ; } }

  { int f0 () { return 20 ; } }
}

Perhaps I missed something in the FAQ.

Is there anywhere (manual or TDPL) I can read up on this language design 
decision? What other contemporary (or classic) languages feature this 
behaviour? Scheme and C both allow the above.


It seems like this would be something that might be nice for certain 
shops to enforce via a compiler switch, but not on by default.


Ed


Element-wise addition of arrays

2010-08-19 Thread Eduardo Cavazos

Hello,

Here's a short program which compares via 'benchmark' two ways to 
perform element-wise addition of two arrays.


--
import std.stdio ;
import std.date ;
import std.random ;

void main ()
{
  double [2] a ;
  double [2] b = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;
  double [2] c = [ uniform ( 0.0 , 1.0 ) , uniform ( 0.0 , 1.0 ) ] ;

  void add ( double [2] a , double [2] b , double [2] c )
  {
a[0] = b[0] + c[0] ;
a[1] = b[1] + c[1] ;
  }

  void f0 () { add ( a , b , c ) ; }

  void f1 () { a[] = b[] + c[] ; }

  writeln ( benchmark ! ( f0 , f1 ) ( 10_000_000 ) ) ;
}
--

On my system, it seems that 'f1' is slower than 'f0'. Let me know if 
somethings not right with the test program or if there's a better way to 
do the benchmark.


Anywho my question is, in principle, isn't enough information available 
to the compiler such that it can make 'f1' be as fast as 'f0'? I'm just 
wondering if I'll need to make functions like the above 'add' which are 
specific for 2 and 3 element arrays.


Ed