Re: string initialization question.

2010-07-30 Thread Justin Spahr-Summers
On Fri, 30 Jul 2010 11:35:15 -0400, Steven Schveighoffer 
schvei...@yahoo.com wrote:
 
 On Fri, 30 Jul 2010 11:24:41 -0400, dcoder dco...@devnull.com wrote:
 
  Hello.
 
  Is there anyway in D to convenient fill a string variable with a char  
  say X times?
 
  So, I'd like to do something like:
 
  string divider( size, '-');// C++ notation.
  $divider = '-' x $size;// perl notation.
 
 
  I thought I could do the following:
 
  const char divider[rowstr.length] = '-';
 
  but the compiler complains about not having a constant integer  
  expression.
 
  thanks.
 
 It's most likely complaining about rowstr.length not being a constant, not  
 the '-'.  This works:
 
 const char divider[5] = '-';
 
 If you want to allocate a new array on the heap with '-' in it, I think  
 there is a way, but I'm not sure how to do it.  I'm pretty sure there's a  
 runtime function to do it.
 
 -Steve

Something like this will work on the heap:

char[] divider = new char[5];
divider[] = '-';


Re: Single alias this

2010-07-25 Thread Justin Spahr-Summers
On Sun, 25 Jul 2010 10:53:51 -0400, bearophile 
bearophileh...@lycos.com wrote:
 Deokjae Lee:
 
  //interfaces first, base class last
  class Foo : I1, I2, Base {}
  
  This doesn't compile.
  I didn't know the order of base class and interfaces matter.
 
 If not already present, that looks good for Bugzilla.

That's as specified in the language spec: 
http://digitalmars.com/d/2.0/class.html#BaseClassList

You could argue that it shouldn't be that way, but it's correct behavior 
as the language stands now.


Re: Grokking concurrency, message passing and Co

2010-07-12 Thread Justin Spahr-Summers
On Mon, 12 Jul 2010 00:03:53 +0200, BLS windev...@hotmail.de wrote:
 
 On 11/07/2010 21:29, Philippe Sigaud wrote:
  I tried this because I was reading an article on Scala's actors, where
  they talk about millions of actors. I guess they are quite different.
 
 Google for fibers or have a look at the dreactor project on dsource.
 Tango has fiber support (afaik).
 
 hth bjoern

Phobos2 has a Fiber class in core.thread as well.


Re: Any special linking for _NSGetExecutablePath on OSX?

2010-07-08 Thread Justin Spahr-Summers
On Thu, 8 Jul 2010 19:24:52 -0400, Nick Sabalausky a...@a.a wrote:
 
 I need to use OSX's _NSGetExecutablePath, and I've declared it:
 
 extern(C) int _NSGetExecutablePath(char* buf, uint* bufsize);
 
 I don't have access to a OSX box to test it on ATM, so I need to know: Is 
 there anything I need to tell the linker (like, anything special I need to 
 explicitly link in) in order to get that to work, or should it just work? 
 (ie, is whatever library is needed for that already linked by default?)

I haven't used the function before, but after looking it up really 
quickly, that declaration looks good to go. Since it's part of the 
dynamic linker, I can't imagine that a library would have to be linked 
in for it to work.


Re: A module comprehensive template-specialization

2010-06-28 Thread Justin Spahr-Summers
On Sun, 27 Jun 2010 18:51:35 +0200, Matthias Walter 
xa...@xammy.homelinux.net wrote:
 
 Hi list,
 
 I tried to write a traits class comparable to iterator_traits in C++ STL
 or graph_traits in Boost Graph Library in D 2.0, but failed to do so via
 template specialization which is put into different modules. Putting
 everything into one module interferes with extensibility. I tried the
 following:
 
 == Module a ==
 | module a;
 |
 | template Base (T)
 | {
 |   alias T Base;
 | }
 
 == Module b ==
 | module b;
 |
 | import a;
 |
 | template Base(T: T*)
 | {
 |   alias Base !(T) Base;
 | }
 
 == Main module ==
 |
 |  import a, b;
 |
 | int main(char[][] args)
 | {
 |   alias Base !(int*) foo;
 |
 |   return 0;
 | }
 
 The error message is:
 bug.d(8): Error: template instance ambiguous template declaration
 b.Base(T : T*) and a.Base(T)
 
 Can I handle this in another way (like making the template a conditional
 one)?
 
 best regards
 Matthias Walter

I believe this is intended behavior, as it prevents template hijacking 
and the like. Using alias to import the two templates into the same 
scope might help, though I'm not sure exactly how it should be done.

On another note, though, have you looked at __traits() and std.traits?


Re: ERROR - cannot implicitly convert expression (s) of type int[3u] to int*

2010-06-18 Thread Justin Spahr-Summers
On Fri, 18 Jun 2010 01:25:32 -0400, Chick Corea chick.zco...@gmail.com 
wrote:
 Those are the result of code that I pulled directly from the D v1 docs from
 
     http://www.digitalmars.com/d/1.0/arrays.html
 
 Specifically, the code is this.
 
         int* p;
         int[3] s;
         int[] a;
         p = s;
         p = a;
 

I can't speak as to why the D2 code doesn't work, but this example in 
the documentation is flat out wrong. To assign a D array to a pointer, 
you must use the .ptr property, so that the correct code is actually:

 int* p;
 int[3] s;
 int[] a;
 p = s.ptr;
 p = a.ptr;

Hope this clears things up.


Re: ERROR - cannot implicitly convert expression (s) of type int[3u] to int*

2010-06-18 Thread Justin Spahr-Summers
On Fri, 18 Jun 2010 08:41:17 -0700, Justin Spahr-Summers 
justin.spahrsumm...@gmail.com wrote:
 
 On Fri, 18 Jun 2010 01:25:32 -0400, Chick Corea chick.zco...@gmail.com 
 wrote:
  Those are the result of code that I pulled directly from the D v1 docs from
  
      http://www.digitalmars.com/d/1.0/arrays.html
  
  Specifically, the code is this.
  
          int* p;
          int[3] s;
          int[] a;
          p = s;
          p = a;
  
 
 I can't speak as to why the D2 code doesn't work, but this example in 
 the documentation is flat out wrong. To assign a D array to a pointer, 
 you must use the .ptr property, so that the correct code is actually:
 
  int* p;
  int[3] s;
  int[] a;
  p = s.ptr;
  p = a.ptr;
 
 Hope this clears things up.

I followed up last night, but my post didn't show up until this morning. 
Sorry for the redundant answer! :X


Re: Minimize lock time

2010-06-11 Thread Justin Spahr-Summers
On Thu, 10 Jun 2010 12:42:09 +0200, Simen kjaeraas 
simen.kja...@gmail.com wrote:
 
 Kagamin s...@here.lot wrote:
 
  Let's consider the following code:
 
  synchronized(syncRoot)
  {
if(condition)opSuccess();
else writeln(possibly,slow);
  }
 
  Suppose the else close doesn't need to be executed in lock domain and  
  can be slow. How to minimize lock time here?
 
  synchronized(syncRoot)
  {
if(condition)opSuccess();
else goto Lwrite;
  }
  Lwrite: writeln(possibly,slow);
 
  We can do this... but...
 
 A flag, as has been mentioned, would work.
 
 Other solutions that might work:
 
 
 synchronized(syncRoot)
 {
if (condition)
{
  opSuccess();
  return;
}
 }
 writeln(possibly,slow);
 
 
 do
 {
synchronized(syncRoot)
{
  if (condition)
  {
opSuccess();
break;
  }
}
writeln(possibly, slow);

All good suggestions. synchronized(), in general, just doesn't scale 
very well (though no fault of D's). If you're doing very complex things, 
you might have better luck with a semaphore of some kind. As always, 
though, avoiding the need for synchronization is best.


Re: Minimize lock time

2010-06-10 Thread Justin Spahr-Summers
On Thu, 10 Jun 2010 02:54:37 -0400, Kagamin s...@here.lot wrote:
 
 Let's consider the following code:
 
 synchronized(syncRoot)
 {
   if(condition)opSuccess();
   else writeln(possibly,slow);
 }
 
 Suppose the else close doesn't need to be executed in lock domain and can be 
 slow. How to minimize lock time here?
 
 synchronized(syncRoot)
 {
   if(condition)opSuccess();
   else goto Lwrite;
 }
 Lwrite: writeln(possibly,slow);
 
 We can do this... but...

A common pattern is to use a boolean flag:

bool success;
synchronized (syncRoot)
{
  success = (condition);
}

if (success) opSuccess();
else writeln(possibly, slow);


Re: metaprogramming question

2010-04-18 Thread Justin Spahr-Summers
On Mon, 19 Apr 2010 07:28:09 +0200, Philippe Sigaud 
philippe.sig...@gmail.com wrote:
 
 On Mon, Apr 19, 2010 at 05:21, Justin Spahr-Summers 
 justin.spahrsumm...@gmail.com wrote:
 
  You can use some expression tuple magic to accomplish something like
  that:
 
  bool check(alias func, EL ...)() {
 GError* err;
 bool ok = func(EL, err);
  if (!ok)
 throw new Exception((*err).message);
 
  return ok;
  }
 
  // used like:
  check!(fooXXX, arg1, ..., argN);
 
 
 But in this case, you need to know the ELs at compile-time. You can make
 them run-time values that way:
 
 bool check(alias func, EL ...)(EL el) {
GError* err;
bool ok = func(el, err);
 if (!ok)
throw new Exception((*err).message);
 
 return ok;
 }
 
 // used like:
 check!fooXXX(arg1, ..., argN);

Yes, sorry. That's what I was trying to do originally, but I couldn't 
quite spit it out. Indeed, templating the actual arguments is a horrible 
idea.


Re: Confused about class equality

2010-04-07 Thread Justin Spahr-Summers
On Tue, 06 Apr 2010 23:35:01 -0400, strtr st...@spam.com wrote:
 
 Justin Spahr-Summers Wrote:
  
  Hmm, that is pretty weird. Are you doing any casts anywhere, or any 
  pointer arithmetic/tricks?
 A search for cast didn't show any related casts.
 Do you maybe know another thing to check? 
 I do throw references around and there are a lot of implicit casts to 
 extended interfaces.

Without actual explicit casting, I don't know how it'd be possible to 
invoke behavior like that. Maybe an object of class X getting implicitly 
converted to interface A and then explicitly cast to class Y? It'd have 
to be pretty convoluted.


Re: Confused about class equality

2010-04-06 Thread Justin Spahr-Summers
On Tue, 06 Apr 2010 22:41:43 -0400, strtr st...@spam.com wrote:
 
 Justin Spahr-Summers Wrote:
  
  I think he said that he has two distinct object references, but the 
  value stored in the object(s) changes by changing either one.
  
  In other words, we'd need to see the code.
 
 I've added this exact sequence:
 
   if( c1 !is null )
   {
   c1.value = 1;
   if( c2 !is null )
   {
   c2.value = 2;
   
   if( c1 !is c2 )
   {
   c1.value = 3;
   assert(c2.value == 2 );
   }
   c2.value = 0;
   }
   c1.value = 0;
   }
 
 To my understanding this should never fails, yet it does.
 AssertError Failure

Hmm, that is pretty weird. Are you doing any casts anywhere, or any 
pointer arithmetic/tricks?

The only thing that I can think of is that you might've somehow 
unintentionally fooled the compiler/runtime by coercing some types 
somewhere.

If not, it might comprise a valid bug report.


Re: Comparing Two Type Tuples

2010-04-05 Thread Justin Spahr-Summers
On Mon, 5 Apr 2010 15:47:10 + (UTC), BCS n...@anon.com wrote:
 
 Hello Justin Spahr-Summers,
 
  On Mon, 5 Apr 2010 02:59:15 + (UTC), BCS n...@anon.com wrote:
  
  Hello Daniel,
  
  Heya ppl!
  
  I was wondering how could I write a function that takes two Type
  Tuples as arguments and returns true if they are match.
  
  Could anyone help me with this?
  
  Thanks!
  
  here is my untested vertion:
  
  template Compare(T...)
  {
  template With(U...)
  {
  static if(T.length != U.lenght) const bool With = false;
  else static if(T.length == 0) const bool With = true;
  else static if(is(T[0] == U[0])) const bool With =
  Compare!(T[1..$]).With!(U[1..$]);
  else const bool With = false;
  }
  }
  use like: Compare!(int, float, myStruct).With(alias1,alias2,typeArg)
  
  Definitely a lot cleaner. I'm curious, though... is there a reason to
  avoid is(T == U)?
  
 
 I dind't know it worked?

It seemed to when I tested the snippet that I sent, but it might've just 
been luck of the draw, and in reality fail silently on certain edge 
cases. I'm really not sure.


Re: Comparing Two Type Tuples

2010-04-04 Thread Justin Spahr-Summers
On Sun, 4 Apr 2010 21:05:49 + (UTC), Daniel Ribeiro Maciel 
danielmac...@gmail.com wrote:
 
 Heya ppl!
 
 I was wondering how could I write a function that takes two Type
 Tuples as arguments and returns true if they are match.
 
 Could anyone help me with this?
 
 Thanks!

You can really only pass a single type tuple to a template, but you can 
work around it by passing a length and then comparing two parts of a 
combined tuple. For instance:

import std.stdio;
import std.typetuple;

void compare(uint LEN, TL ...) () {
writefln(%s, is(TL[0 .. LEN] == TL[LEN .. $]));
}

void main() {
alias TypeTuple!(int, double, char[]) tupleA;
alias TypeTuple!(int, double, char[]) tupleB;
alias TypeTuple!(int, double, char*) tupleC;

compare!(tupleA.length, tupleA, tupleB);
compare!(tupleA.length, tupleA, tupleC);
}

will output true then false.