ref return function using foreach ref result segfaults. Compiler bug?

2012-11-13 Thread Rob T

Hard to describe this problem, see code and read comments below.

class A
{
   private int _v;

   this( int a_v )
   {
  _v = a_v;
   }

   @property size_t length()
   {
return 1;
   }
   int opApply( int delegate( ref int a_v ) a_dg )
   {
  int result = 0;
  for ( ulong i = 0; i < length; ++i )
  {
 result = a_dg( this.opIndex( i ) );
 if ( result ) break;
  }
  return result;
   }
   ref int opIndex( size_t a_iPos )
   {
  return _v;
   }

}

class B : A
{
   this( int a_v )
   {
  super(a_v);
   }

   ref int find( int a_What )
   {
  foreach( val; super )
  {
 if ( val == a_What )
return val;
  }
  throw new Exception("value not found");
   }
}

main()
{
auto v_B = new B(500);
writefln("Search = %d", v_B.find(500) );
return 0;
}


When the return value of find() is ref, it segfaults or returns 
garbage. If the return value is a copy it works OK.


The only oddity I can see is that 'val' goes out of scope, but 
it's a ref return value (pointer) to _v (right?), so it should 
work anyway.


This looks like a bug in the compiler to me.

What do you guys think?

--rt



Re: Compilable Recursive Data Structure ( was: Recursive data structure using template won't compile)

2012-11-13 Thread Don Clugston

On 13/11/12 06:51, Rob T wrote:

On Monday, 12 November 2012 at 14:28:53 UTC, Andrej Mitrovic wrote:

On 11/12/12, Andrej Mitrovic  wrote:

On 11/12/12, Don Clugston  wrote:

Yeah. Though note that 1000 bug reports are from bearophile.


Actually only around 300 remain open:
http://d.puremagic.com/issues/buglist.cgi?query_format=advanced&emailreporter2=1&emailtype2=substring&order=Importance&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&bug_status=VERIFIED&email2=bearophile




Oh wait, that's only for DMD. It's 559 in total:
http://d.puremagic.com/issues/buglist.cgi?query_format=advanced&emailreporter2=1&emailtype2=substring&order=Importance&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&bug_status=VERIFIED&email2=bearophile



Issue 8990 and 6969, both related to this thread and unresolved, are not
in the list, so I suspect there's a lot more missing too.

PS: I could not figure out how to make a useful report using that bug
report tool either.

--rt


I recommend deskzilla lite. D is on its list of supported open-source 
projects. It maintains a local copy of the entire bugzilla database, so 
you're not restricted to the slow and horrible html interface.







Re: ref return function using foreach ref result segfaults. Compiler bug?

2012-11-13 Thread Kenji Hara

On Tuesday, 13 November 2012 at 08:50:16 UTC, Rob T wrote:

Hard to describe this problem, see code and read comments below.

class A
{
   private int _v;

   this( int a_v )
   {
  _v = a_v;
   }

   @property size_t length()
   {
return 1;
   }
   int opApply( int delegate( ref int a_v ) a_dg )
   {
  int result = 0;
  for ( ulong i = 0; i < length; ++i )
  {
 result = a_dg( this.opIndex( i ) );
 if ( result ) break;
  }
  return result;
   }
   ref int opIndex( size_t a_iPos )
   {
  return _v;
   }

}

class B : A
{
   this( int a_v )
   {
  super(a_v);
   }

   ref int find( int a_What )
   {
  foreach( val; super )
  {
 if ( val == a_What )
return val;
  }
  throw new Exception("value not found");
   }
}

main()
{
auto v_B = new B(500);
writefln("Search = %d", v_B.find(500) );
return 0;
}


When the return value of find() is ref, it segfaults or returns 
garbage. If the return value is a copy it works OK.


The only oddity I can see is that 'val' goes out of scope, but 
it's a ref return value (pointer) to _v (right?), so it should 
work anyway.


This looks like a bug in the compiler to me.

What do you guys think?


This issue looks like bug8093.
http://d.puremagic.com/issues/show_bug.cgi?id=8093

And the code works correctly in git head (dmd2.061alpha). 
Therefore, I think that the bug is fixed very recently.


Kenji Hara


Re: Compilable Recursive Data Structure ( was: Recursive data structure using template won't compile)

2012-11-13 Thread Andrej Mitrovic
On 11/13/12, Don Clugston  wrote:
> I recommend deskzilla lite. D is on its list of supported open-source
> projects. It maintains a local copy of the entire bugzilla database, so
> you're not restricted to the slow and horrible html interface.

Wow, I had no idea they had this. I've added a note about it here:
http://prowiki.org/wiki4d/wiki.cgi?D__Tutorial/BugReports Thanks!


Re: Inferring function argument types from other argument types

2012-11-13 Thread Vijay Nayar
I believe this question was asked before, but here is the 
solution again.


struct Foo(_T1, _T2)
{
  alias _T1 T1;
  alias _T2 T2;
  T1 a;
  T2 b;
}

FooT.T1 func(FooT, T)(FooT foo, T x)
  if (is(FooT.T1) && is(T : FooT.T1))
{
  return x * foo.a;
}

void main() {
  auto foo = Foo!(size_t, string)(8, "bobcat");
  int value = 3;
  assert(foo.func(value) == 24);
  assert(is(typeof(foo.func(value)) == size_t));
}

 - Vijay

On Monday, 12 November 2012 at 12:42:31 UTC, Joseph Rushton 
Wakeling wrote:
Suppose that I've got a struct which internally defines a 
number of types:


struct Foo(_T1, _T2)
{
alias _T1 T1;
alias _T2 T2;
T1 a;
T2 b;
}

... and now I want to define a function which takes as input an 
instance of one of these structs, and a variable of type T1.


I tried the following:

T func(FooT, T = FooT.T1)(FooT foo, T x)
{
return x * foo.a;
}

but found that the type of whatever I was passing would 
override the default: e.g. if I called func(fooInstance, 1) 
then the second argument would be interpreted as an int even 
though Foo.T1 is size_t.


I also tried,

T func(FooT, T : FooT.T1)(FooT foo, T x)
{
return x * foo.a;
}

but this generates a different error: "no property 'T1' for 
type 'FooT'".  I tried replacing FooT with alias FooT to no 
avail.


Obviously I could get round the problems of the first example 
with something based around CommonType etc. but I'm just 
wondering if it's possible to make an argument dependent on 
another template parameter in the way I'm looking for here.





Re: Regarding ranges

2012-11-13 Thread Vijay Nayar

On Tuesday, 13 November 2012 at 04:50:54 UTC, bearophile wrote:
Do you know if it's possible to write similar code nicely & 
efficiently with ranges?


Bearophile, do you know of a good reference to learn about ranges 
in D?  Due to the language name, Googling for specific topics in 
D is a bit difficult and though I've heard the topic come up, I 
never found a way to really understand it.


 - Vijay


Re: Compilable Recursive Data Structure ( was: Recursive data structure using template won't compile)

2012-11-13 Thread Nick Sabalausky
On Tue, 13 Nov 2012 11:56:57 +0100
Don Clugston  wrote:
>
> I recommend deskzilla lite. D is on its list of supported open-source 
> projects. It maintains a local copy of the entire bugzilla database,
> so you're not restricted to the slow and horrible html interface.
> 

Awesome! I wish GitHub had something like that. (Hell, I wish every web
"app" had something like that.)

Although, $189 for anything with > 2k issues or anything non-OSS? Geez
that's expensive, what are they, Adobe?



Shared value types can not be destructed

2012-11-13 Thread Benjamin Thaut

Apperently this is by design:
http://d.puremagic.com/issues/show_bug.cgi?id=8295

To clarify: It is not possible to define a destructor that will be 
called on the destruction of a shared struct.


In a different thread Walter commeted this bug with:
"If you include an object designed to work only in a single thread 
(non-shared), make it shared, and then destruct it when other threads 
may be pointing to it ...


What should happen?"

I did try to think of a case where his scenario would actually break 
something, but couldn't find one. If someone has more knowdelge about 
this situation some clarification would be great. Maybe even a small 
code sample that illustrates the problem.


Kind Regards
Benjamin Thaut


Re: Regarding ranges

2012-11-13 Thread Namespace

On Tuesday, 13 November 2012 at 16:18:43 UTC, Vijay Nayar wrote:

On Tuesday, 13 November 2012 at 04:50:54 UTC, bearophile wrote:
Do you know if it's possible to write similar code nicely & 
efficiently with ranges?


Bearophile, do you know of a good reference to learn about 
ranges in D?  Due to the language name, Googling for specific 
topics in D is a bit difficult and though I've heard the topic 
come up, I never found a way to really understand it.


 - Vijay


http://ddili.org/ders/d.en/ranges.html


Re: Compilable Recursive Data Structure ( was: Recursive data structure using template won't compile)

2012-11-13 Thread Nick Sabalausky
On Tue, 13 Nov 2012 11:19:36 -0500
Nick Sabalausky  wrote:

> On Tue, 13 Nov 2012 11:56:57 +0100
> Don Clugston  wrote:
> >
> > I recommend deskzilla lite. D is on its list of supported
> > open-source projects. It maintains a local copy of the entire
> > bugzilla database, so you're not restricted to the slow and
> > horrible html interface.
> > 
> 
> Awesome! I wish GitHub had something like that. (Hell, I wish every
> web "app" had something like that.)
> 
> Although, $189 for anything with > 2k issues or anything non-OSS? Geez
> that's expensive, what are they, Adobe?
> 

Oh, upon using it, it looks like the free version is for OSS projects
with any number of issues *and* non-OSS projects with <= 2k issues.
Guess I misread their download page.


Re: Inferring function argument types from other argument types

2012-11-13 Thread Joseph Rushton Wakeling

On 11/13/2012 05:05 PM, Vijay Nayar wrote:

I believe this question was asked before, but here is the solution again.


The actual reality of what I'm trying to do is slightly more complex: it's more 
like

struct Foo(_T1, _T2)
{
alias _T1 T1;
alias _T2 T2;
// etc.
}

FooT.T1 func(FooT, T)(FooT foo, T x)
{
return func2(x);
}

... where func2() is also a templated function, and it's important that it take 
a type of FooT.T1 and not T.


So, I can't see what the solution is apart from casting x to FooT.T or 
explicitly indicating FooT.T1 as a template parameter for func2.


Re: ref return function using foreach ref result segfaults. Compiler bug?

2012-11-13 Thread Rob T

On Tuesday, 13 November 2012 at 12:31:26 UTC, Kenji Hara wrote:


This issue looks like bug8093.
http://d.puremagic.com/issues/show_bug.cgi?id=8093

And the code works correctly in git head (dmd2.061alpha). 
Therefore, I think that the bug is fixed very recently.


Kenji Hara


Thanks for the response Kenji! I'll implement a work-a-round for 
now and won't bother filing a bug report.


--rt



Re: Inferring function argument types from other argument types

2012-11-13 Thread Vijay Nayar

Ok, I get it.

My understanding is that you have basically two options.
  * If you want the function to be called with any implicitly
castable type, then you must cast it in the function.
  * If you only want the function to accept the one type,
then use is(T == FooT.T1) and you must cast the input
the function.

However, all this feels like an over complication and round-about 
way of
doing things.  If you have exact types that must be specified in 
the
struct, you probably don't want an external template at all.  
Simply place

the function in the struct.

struct Foot(_T1, _T2) {
  ...
  _T1 func(_T1 x) {
return func2(x);
  }
}

 - Vijay

On Tuesday, 13 November 2012 at 16:39:55 UTC, Joseph Rushton 
Wakeling wrote:

On 11/13/2012 05:05 PM, Vijay Nayar wrote:
I believe this question was asked before, but here is the 
solution again.


The actual reality of what I'm trying to do is slightly more 
complex: it's more like


struct Foo(_T1, _T2)
{
alias _T1 T1;
alias _T2 T2;
// etc.
}

FooT.T1 func(FooT, T)(FooT foo, T x)
{
return func2(x);
}

... where func2() is also a templated function, and it's 
important that it take a type of FooT.T1 and not T.


So, I can't see what the solution is apart from casting x to 
FooT.T or explicitly indicating FooT.T1 as a template parameter 
for func2.





Re: Inferring function argument types from other argument types

2012-11-13 Thread Ali Çehreli

On 11/12/2012 06:55 AM, Joseph Rushton Wakeling wrote:

> I'm just curious
> if there is any other way to meaningfully determine the type of one
> function argument based on the type of another. Hence the playing with
> different template argument formulations.
>
> It just feels really difficult to believe that the generic programming
> in D doesn't extend to allowing you to infer one argument type based on
> another.

I am pretty sure that should work according to spec under "Argument 
Deduction":


  http://dlang.org/template.html

"
2. If the type specialization is dependent on a type parameter, the type 
of that parameter is set to be the corresponding part of the type argument.

"

struct Foo(_T1, _T2)
{
alias _T1 T1;
T1 t1;
}

void foo(T)(T foo, T.T1 x)// <-- Note T.T1
{
return func2(x);
}

void main()
{
auto f = Foo!(size_t, string)();
foo(f, 42);
}

Although, there is the issue of compiler's not being sure whether the T1 
in T.T1 is a typename or a member variable. (That's why the 'typename' 
keyword is used in such contexts in C++.)


Still, please create a bug report: :)

  http://d.puremagic.com/issues/

Ali



Re: Regarding ranges

2012-11-13 Thread Vijay Nayar

This is very well written.  Thanks for the link!

 - Vijay

On Tuesday, 13 November 2012 at 16:29:41 UTC, Namespace wrote:

On Tuesday, 13 November 2012 at 16:18:43 UTC, Vijay Nayar wrote:

On Tuesday, 13 November 2012 at 04:50:54 UTC, bearophile wrote:
Do you know if it's possible to write similar code nicely & 
efficiently with ranges?


Bearophile, do you know of a good reference to learn about 
ranges in D?  Due to the language name, Googling for specific 
topics in D is a bit difficult and though I've heard the topic 
come up, I never found a way to really understand it.


- Vijay


http://ddili.org/ders/d.en/ranges.html





Extent of tail call optimization in D?

2012-11-13 Thread J. Jenkins
In Dr Alexandrescu's "The D Programming Language", on page 12, it 
is noted that the D compiler will rewrite tail calls within a 
procedure as loops.  Does the compiler rewrite tail calls between 
procedures as jumps?


For example, in pseudo-D:

void foo(K)(K cont, stuff..) {
// Do things with stuff.
cont(things);
}

Is the call `cont(things)' optimized?



Re: Is there a way to initialize a non-assigned structure declaration (or is it a definition)?

2012-11-13 Thread Vijay Nayar
This is merely a syntactic difference in how structs are handled. 
 In D, structs are more akin to low level types like int and have 
most of the same symantics.


So
  SnonParameterized cnp(5, 3.303);
makes about as much sense as
  int cnp(3);

You have two syntax choices to pick from in the D version of the 
code you posted:

  struct SnonParameterized
  {
int t;
float u;
  };

  void main() {
auto snon1 = SnonParameterized(3, 4.5);  // Option 1
SnonParameterized snon2 = {3, 4.5};  // Option 2
  }

 - Vijay


On Saturday, 10 November 2012 at 17:41:00 UTC, Too Embarrassed To 
Say wrote:
I appreciate all the helpful replies, but I've simplified 
things to what I belive is the core issue. In C++ (at the risk 
of becoming a heretic) the language allows me to do the 
following:


struct SnonParameterized
{
public:
   int t;
   float u;
   SnonParameterized(int tparam, float uparam);
};

SnonParameterized::SnonParameterized(int tparam, float uparam)
{
   t = tparam;
   u = uparam;
}

SnonParameterized snp(5, 3.303);  // this compiles with Visual 
C++ 2010



===

Now with D, I try (what I think is identical semantics) the 
following:



struct SnonParameterized
{
   int t;
   float u;
   this(int t, float u)
   {
  this.t = t
  this.u = u;
   }
}

SnonParameterized cnp(5, 3.303);  // fails compile with Error: 
found 'cnp' when expecting ';' following statement


auto hi = SnonParameterized(5, 3.303);  // compiles of course.


I'm just trying to understand why D disallows the 
non-assignment syntax.  Probably for a very good (and obvious) 
reason.





Re: Is Override Still Mandatory?

2012-11-13 Thread Rob T
On Monday, 12 November 2012 at 03:08:35 UTC, Jonathan M Davis 
wrote:

On Monday, November 12, 2012 03:58:17 Vijay Nayar wrote:

I was under the impression that the attribute "override" was
mandatory when replacing a virtual function in a base class.
However, code that leaves this out has no errors using dmd 
v2.060.


It's mandatory if you compile with -w. Eventually, it will be 
mandatory all
the time. It's just that it's being phased in via -w rather 
than requiring it
immediately. That way, people have time to fix their code to 
use it before it's
required. Unfortunately though, a lot of people fail to compile 
with either -w
or -wi, so they're happily writing _new_ code which will break 
once override
is always required, which means that phasing like this doesn't 
necessarily

actually fix the problem.

- Jonathan M Davis


I was about to ask this same question today! Thanks for the 
answer.


--rt


Re: Is Override Still Mandatory?

2012-11-13 Thread Rob T

On Monday, 12 November 2012 at 03:29:09 UTC, bearophile wrote:
I agree. Generally I think warnings should be active on default 
and disabled on request with a switch :-)


Bye,
bearophile


Agreed! Fortunately I have not written all that much broken code 
yet. I recall a lot of discussion about how "depreciated" should 
work, and it's clear what we have now is next to useless, so I 
hope the suggestion that was made to improve depreciated with 
warnings and so forth, is implemented.


--rt


Re: Is Override Still Mandatory?

2012-11-13 Thread bearophile

Rob T:

so I hope the suggestion that was made to improve depreciated 
with warnings and so forth, is implemented.


"Deprecated features as warnings" is in the D front since about 7 
hours:

https://github.com/D-Programming-Language/dmd/commit/5881617a34adc172b830314c17da21d5c834ffd0

Bye,
bearophile


Re: Is Override Still Mandatory?

2012-11-13 Thread Rob T

On Wednesday, 14 November 2012 at 02:33:47 UTC, bearophile wrote:

Rob T:

so I hope the suggestion that was made to improve depreciated 
with warnings and so forth, is implemented.


"Deprecated features as warnings" is in the D front since about 
7 hours:

https://github.com/D-Programming-Language/dmd/commit/5881617a34adc172b830314c17da21d5c834ffd0

Bye,
bearophile


That's good news. If I were to use the latest pre-release version 
of dmd that was relatively safe, how will I find it, or is it OK 
to use the master branch?


--rt


Re: Is Override Still Mandatory?

2012-11-13 Thread Rob T

On Wednesday, 14 November 2012 at 02:33:47 UTC, bearophile wrote:

Rob T:

so I hope the suggestion that was made to improve depreciated 
with warnings and so forth, is implemented.


"Deprecated features as warnings" is in the D front since about 
7 hours:

https://github.com/D-Programming-Language/dmd/commit/5881617a34adc172b830314c17da21d5c834ffd0

Bye,
bearophile


That's good news. If I were to use the latest pre-release version 
of dmd that was relatively safe, how will I find it, or is it OK 
to use the master branch?


--rt