ushort arithmetic question

2013-10-25 Thread Jonathan Crapuchettes
Shouldn't this code work? Looking at the arithmetic conversions section 
of http://dlang.org/type.html, point 4.1 makes me think that I shouldn't 
be getting the error since they are the same type.

void main()
{
ushort v1 = 1;
ushort v2 = 1;
ushort v3 = v1 + v2;
}

test.d(5): Error: cannot implicitly convert expression (cast(int)v1 + cast
(int)v2) of type int to ushort


Re: ushort arithmetic question

2013-10-25 Thread Jonathan Crapuchettes
On Fri, 25 Oct 2013 11:51:03 -0700, Ali Çehreli wrote:

 On 10/25/2013 11:45 AM, Jonathan Crapuchettes wrote:
 Shouldn't this code work? Looking at the arithmetic conversions section
 of http://dlang.org/type.html, point 4.1 makes me think that I
 shouldn't be getting the error since they are the same type.

 void main()
 {
  ushort v1 = 1; ushort v2 = 1; ushort v3 = v1 + v2;
 }

 test.d(5): Error: cannot implicitly convert expression (cast(int)v1 +
 cast (int)v2) of type int to ushort


 But there is 4.0 before 4.1: :)
 
 4. Else the integer promotions are done on each operand, followed by:
 
 1. If both are the same type, no more conversions are done.
 
 Note integer promotions are done on each operand. In other words, e.g.
 there is no arithmetic operation on a ushort. The expression v1 + v2 is
 performed as two ints.
 
 Ali

Thank you for pointing that out for me. I missed that part.

Jonathan


inout method is not callable using a const object

2013-09-06 Thread Jonathan Crapuchettes
Can someone help me understand how to correct this error?

Error: inout method ...ValidSparseDataStore.opIndex is not callable using 
a const object

The specific method is defined as:

struct ValidSparseDataStore
{
inout(DataT*) opIndex(const Address addr) inout
{
if (auto node = findNode(addr))
return cast(inout)(node.data);

return null;
}

private ValidSparseNode* findNode(const Address ids) const
{
...
}
}

Thank you,
JC


Re: Defining inout returned values for ranges

2013-09-06 Thread Jonathan Crapuchettes
On Thu, 05 Sep 2013 23:32:10 +0200, anonymous wrote:

 On Thursday, 5 September 2013 at 19:19:42 UTC, Jonathan Crapuchettes
 wrote:
 On Wed, 04 Sep 2013 11:49:41 +0200, anonymous wrote:
 [...]
 You can use a Template This Parameter [1] instead of inout:
 
 auto opSlice(this This)()
 {
 static if(is(This == const)) alias QualifiedT = const
 T;
 else alias QualifiedT = T;
 
 static struct Range {
 QualifiedT front() @property {
 
 
 [1] http://dlang.org/template.html#TemplateThisParameter

 Thank you for the help. That worked, but now I ran into another similar
 issue. What if the Range struct is being defined outside of the opSlice
 method? I have a number of methods in a struct that return the same
 range type and would rather not have to have duplicate code or need to
 use mixins.

 JC
 
 Templatize Range and pass it This:
 
 static struct Range(This)
 {
 static if(is(This == const)) alias QualifiedT = const T;
 else alias QualifiedT = T;
 
 QualifiedT front() @property {...}
 }
 
 auto opSlice(this This)()
 {
 return Range!This();
 }
 
 ... more methods in the style of opSlice ...
 
 
 If you need QualifiedT in opSlice, make it a template over This,
 too.

Thank you again. It all appears to be working correctly.

JC


Re: inout method is not callable using a const object

2013-09-06 Thread Jonathan Crapuchettes
On Fri, 06 Sep 2013 13:27:20 -0700, Ali Çehreli wrote:

 On 09/06/2013 01:14 PM, Jonathan Crapuchettes wrote:
 
   Can someone help me understand how to correct this error?
  
   Error: inout method ...ValidSparseDataStore.opIndex is not callable
   using a const object
 
 That error is about opIndex but we don't see any code that makes that
 call.
 
   The specific method is defined as:
  
   struct ValidSparseDataStore {
inout(DataT*) opIndex(const Address addr) inout {
if (auto node = findNode(addr))
return cast(inout)(node.data);
  
return null;
}
  
private ValidSparseNode* findNode(const Address ids) const {
...
}
   }
  
   Thank you,
   JC
 
 I can reproduce it with this code:
 
 struct S {
  void foo(int) inout {}
 }
 
 void main()
 {
  const s = S();
  s.foo(42); // -- works s.foo();   // -- ERROR
 }
 
 Error: inout method deneme.S.foo is not callable using a const object
 
 I can't know whether this matches your case but the compiler should
 error about not finding a matching foo() overload instead of bringing
 the inout into the discussion.
 
 Ali

Sorry for not including the call site. Here is an example of the call:

void main()
{
auto store = ...;
//add items to the storage
const cStore = store;
auto dp = cStore[16057];//-- ERROR
}

I can try to minimize the code to a working example if needed.

Thank you,
JC


Re: inout method is not callable using a const object

2013-09-06 Thread Jonathan Crapuchettes
On Fri, 06 Sep 2013 14:08:19 -0700, Ali Çehreli wrote:

 I think it is the same issue then.
 
 On 09/06/2013 02:00 PM, Jonathan Crapuchettes wrote:
 
   inout(DataT*) opIndex(const Address addr) inout {
 
 Unless Address is an alias of int, there is no matching opIndex overload
 for the following call:
 
  auto dp = cStore[16057];//-- ERROR
 
 Ali

Thank you for your help. For some reason, no one else in the office 
noticed that problem. In my code Address was something like TypeTuple!
(int, char) and I was trying to call opIndex(int, int).

Thank you again.


Re: Defining inout returned values for ranges

2013-09-05 Thread Jonathan Crapuchettes
On Wed, 04 Sep 2013 11:49:41 +0200, anonymous wrote:

 On Wednesday, 4 September 2013 at 00:56:39 UTC, Jonathan Crapuchettes
 wrote:
 If a range struct (Range) is defined inside another struct (Test), how
 can the constness or mutability of Test be attributed to the return
 type of Range.front? I'm running into this problem because I need the
 range to be iterated, but I need the pointer in T to be marked const
 when appropriate.

 Thank you,
 JC

 Pseudo-Example:
 struct Test {
 static struct T {
 uint* ptr;
 }
 ...

 auto opSlice() inout {
 static struct Range {
 inout(T) front() @property {
 ...
 }
 ...
 }

 return Range();
 }
 }
 
 
 You can use a Template This Parameter [1] instead of inout:
 
 auto opSlice(this This)()
 {
 static if(is(This == const)) alias QualifiedT = const T;
 else alias QualifiedT = T;
 
 static struct Range {
 QualifiedT front() @property {
 
 
 [1] http://dlang.org/template.html#TemplateThisParameter

Thank you for the help. That worked, but now I ran into another similar 
issue. What if the Range struct is being defined outside of the opSlice 
method? I have a number of methods in a struct that return the same range 
type and would rather not have to have duplicate code or need to use 
mixins.

JC


Defining inout returned values for ranges

2013-09-03 Thread Jonathan Crapuchettes
If a range struct (Range) is defined inside another struct (Test), how 
can the constness or mutability of Test be attributed to the return type 
of Range.front? I'm running into this problem because I need the range to 
be iterated, but I need the pointer in T to be marked const when 
appropriate.

Thank you,
JC

Pseudo-Example:
struct Test
{
static struct T
{
uint* ptr;
}
...

auto opSlice() inout
{
static struct Range
{
inout(T) front() @property
{
...
}
...
}

return Range();
}
}


Re: Templated Function can't deduce function arguments

2013-05-23 Thread Jonathan Crapuchettes
On Wed, 22 May 2013 23:28:21 -0400, Jonathan M Davis wrote:

 On Wednesday, May 22, 2013 21:31:53 Steven Schveighoffer wrote:
 On Wed, 22 May 2013 21:16:44 -0400, Jonathan Crapuchettes
 
 jcrapuchet...@gmail.com wrote:
  Can anyone tell me why this doesn't compile? Dmd 2.062 says that it
  cannot deduce the template function from arguments types.
  
  import std.stdio;
  
  void main()
  {
  
  test!(dchar, int)('b', 6, 'a', 54);
  
  }
  
  template test(Types...)
  {
  
  void test(T...)(const Types v, const T values...)
 
 Do you need that last elipsis? I thought you didn't, but not sure.
 
 You don't, and I'm surprised that it compiles, since I don't think that
 the elipsis is actually legal there. AFAIK, the only time that an
 elipsis is legal in the function arguments is with array variadics; e.g.
 
 auto foo(int[] bar...) {...}
 
 - Jonathan M Davis

The last ellipsis was a remnant of testing. Thank you for pointing that 
out. Removing it still doesn't help the compiler deduce the argument 
types. It appears that the issue has to do with the usage of the Types 
TypeTuple. If the

const Types v

is swapped out for

const dchar v1, const int v2

the code compiles just fine. This makes me wonder if dmd is not 
interpreting the Types TypeTuple correctly in the inner-function.

Jonathan


Re: Templated Function can't deduce function arguments

2013-05-23 Thread Jonathan Crapuchettes
Thank you for the help. Bug report at http://d.puremagic.com/issues/
show_bug.cgi?id=10156


Templated Function can't deduce function arguments

2013-05-22 Thread Jonathan Crapuchettes
Can anyone tell me why this doesn't compile? Dmd 2.062 says that it 
cannot deduce the template function from arguments types.

import std.stdio;

void main()
{
test!(dchar, int)('b', 6, 'a', 54);
}

template test(Types...)
{
void test(T...)(const Types v, const T values...)
{
writefln(%s,%s, v);
foreach (s; values)
writefln(%s,%s, s);
}
}

Thank you,
Jonathan


Re: Named Tuple Names

2013-03-26 Thread Jonathan Crapuchettes
On Tue, 26 Mar 2013 10:13:03 +0100, Jacob Carlborg wrote:

 On 2013-03-25 23:58, Jonathan Crapuchettes wrote:
 Is there a way to get the names of the fields in a named tuple? It
 looks like the names are actually aliases.
 
 Perhaps fieldSpecs.

Well, that gets you a FieldSpec template which would work except that it 
is private. I can get around that using the .stringof and then parsing, 
but I wish there was a cleaner way to do it. If the FieldSpec template 
was moved into the public section, it would allow for the name member to 
be accessed.


Named Tuple Names

2013-03-25 Thread Jonathan Crapuchettes
Is there a way to get the names of the fields in a named tuple? It looks 
like the names are actually aliases.

Thanks,
JC


Re: Function constraint vs const parameter?

2012-06-07 Thread Jonathan Crapuchettes
That worked great! I wish there was a simpler solution, but thank you very much 
for your help.

JC

Ali Çehreli wrote:

On 06/06/2012 10:02 AM, Jonathan Crapuchettes wrote:
  I'm running into a problem with the following function definition when
  passing in a const(string[]).
 
  public T condenseCountyList(T)(const T inputList) if (is(Unqual!T :
  string) || is(Unqual!T : string[]))
 
  I'm getting the Error: template common.condenseCountyList does not
  match any function template declaration when calling the function as
 
  condenseCountyList(countyList);
 
  , but if I changed the calling code to
 
  condenseCountyList(cast(string[])countyList)
 
  dmd is happy.
 
  How do I need to change the function constraint to make this work?
 
  Thank you,
  Jonathan Crapuchttes

Sorry for being terse but this works:

import std.traits;
import std.array;

template isSomeStringArray(T)
{
enum isSomeStringArray = is (typeof(
{
T variable;
static assert (isSomeString!(typeof(variable.front)));
}()));
}

T condenseCountyList(T)(const T inputList)
if (isSomeString!T || isSomeStringArray!T)
{
return T.init;
}

void main()
{
auto countyList = [ Santa Clara ];
condenseCountyList(countyList);
}

isSomeString is defined in std.traits. isSomeStringArray above uses a number of
D features:

* anonymous delegate

* executing that delegate by () (which is actually never executed)

* typeof, which produces invalid type if the expression illegal

* is, which produces true if the type that it receives is not invalid

* eponymous templates

Ali



Function constraint vs const parameter?

2012-06-06 Thread Jonathan Crapuchettes
I'm running into a problem with the following function definition when passing 
in a const(string[]).


public T condenseCountyList(T)(const T inputList) if (is(Unqual!T : string) || 
is(Unqual!T : string[]))


I'm getting the Error: template common.condenseCountyList does not match any 
function template declaration when calling the function as


condenseCountyList(countyList);

, but if I changed the calling code to

condenseCountyList(cast(string[])countyList)

dmd is happy.

How do I need to change the function constraint to make this work?

Thank you,
Jonathan Crapuchttes


Re: Global runtime strings help

2011-09-26 Thread Jonathan Crapuchettes
Thank you for the thought, but the problem here is that the file containing the 
strings is only known at runtime from a command line argument. I also have some 
global strings that need to be set from the database.


Thank you again,
JC

Jonathan M Davis wrote:

On Friday, September 23, 2011 13:29:08 Jonathan Crapuchettes wrote:

I'm working on an application that requires a large number of strings that
only need to be loaded once at runtime and need to be accessible to all
threads throughout the execution of the program. Some of these strings are
variables like database host and username that need to be read from a file.

Can anyone help me with an example how they might do this task?
Thank you,
JC


immutable string1;
immutable string2;
immtuable string3;

static shared this()
{
 string1 = the string;
 string2 = the other string;
 string3 = funcThatGrabsStringFromFile();
}

immutable variables are implicitly shared. The shared module constructor will
then initialize them before main runs, and all threads will have access to
them.

- Jonathan M Davis


Global runtime strings help

2011-09-23 Thread Jonathan Crapuchettes
I'm working on an application that requires a large number of strings that only 
need to be loaded once at runtime and need to be accessible to all threads 
throughout the execution of the program. Some of these strings are variables 
like database host and username that need to be read from a file.


Can anyone help me with an example how they might do this task?
Thank you,
JC


Re: Synchronized Linked List Traversal

2010-01-21 Thread Jonathan Crapuchettes
Thank you for the help. Since I am using D1, I think I will use the pthread 
mutexes through the C API.


Steven Schveighoffer wrote:

On Thu, 21 Jan 2010 08:35:50 -0500, Steven Schveighoffer
schvei...@yahoo.com wrote:


To do this, you need a manual mutex, look in tango.core.sync.Mutex I
think.


or core.sync.mutex for D2...

-Steve


Re: Synchronized Linked List Traversal

2010-01-21 Thread Jonathan Crapuchettes
Yeah, but I have a lot of code in this project that uses phobos and it would be 
a major pain (not to mention the time) to switch over.


Thank you for the thought,
Jonathan

Steven Schveighoffer wrote:

On Thu, 21 Jan 2010 13:29:37 -0500, Jonathan Crapuchettes
jcrapuchet...@gmail.com wrote:


Thank you for the help. Since I am using D1, I think I will use the
pthread mutexes through the C API.



Tango provides mutex objects for D1.

-Steve


Synchronized Linked List Traversal

2010-01-20 Thread Jonathan Crapuchettes
I'm working on a project that requires multi-threading and I am trying to grasp 
the best way to work with setting up a linked-list where multiple threads might 
try to be setting the next element in the list at the same time.


What I really need to know is if this will work:

Node!(T) child = parent.firstChild;
synchronized (child)
{
while (child.sibling !is null)
child = child.sibling;

child.sibling = node;
}

What I am worried about is that the mutex will not follow the traversal. Any 
help would be great.

Thanks you,
Jonathan


UNIX IPC Sockets

2009-03-02 Thread Jonathan Crapuchettes
I am looking into doing some IPC between two processes using phobos, but ran 
into a problem when trying to bind a Socket to a unix location. There is an 
InternetAddress class, but there doesn't appear to be an Address class for the 
UNIX address family. Can anyone give me an example of how to setup a Socket with 
an address family type AddressFamily.UNIX?


Thank you,
JC