Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Anders S via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote:

On 11/29/2016 07:30 AM, Anders S wrote:

> INTargv[1];/* list of arguments */

In addition to what Nemanja Boric wrote, the recommended array 
syntax in D is the following:


INT[1] argv;

>   char sbuf[1024];
>   io = (IOREQ *)buf;  // Not accepted in dlang

You must use the cast keyword, the pointer to first element of 
an array is .ptr, and I think you meant sbuf:


io = cast(IOREQ *)sbuf.ptr;

>   st = write(fd, sbuf, 1024);  //

You can use the .length property of arrays:

st = write(fd, sbuf, sbuf.length);

Ali



Thanks you all guys, and the cast (IOREQ *) ... did the trick!!
I'll have a look at your other comments aswell on struct a.s.o.
/anders




Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote:

On 11/29/2016 07:30 AM, Anders S wrote:

> INTargv[1];/* list of arguments */



Speculation, but given that you say "list of args" is it possible 
OP means to use int[0] for an inline array.


In addition to what Nemanja Boric wrote, the recommended array 
syntax in D is the following:


INT[1] argv;

>   char sbuf[1024];
>   io = (IOREQ *)buf;  // Not accepted in dlang

You must use the cast keyword, the pointer to first element of 
an array is .ptr, and I think you meant sbuf:


io = cast(IOREQ *)sbuf.ptr;

>   st = write(fd, sbuf, 1024);  //

You can use the .length property of arrays:

st = write(fd, sbuf, sbuf.length);

Ali




Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Ali Çehreli via Digitalmars-d-learn

On 11/29/2016 07:30 AM, Anders S wrote:

> INTargv[1];/* list of arguments */

In addition to what Nemanja Boric wrote, the recommended array syntax in 
D is the following:


INT[1] argv;

>   char sbuf[1024];
>   io = (IOREQ *)buf;  // Not accepted in dlang

You must use the cast keyword, the pointer to first element of an array 
is .ptr, and I think you meant sbuf:


io = cast(IOREQ *)sbuf.ptr;

>   st = write(fd, sbuf, 1024);  //

You can use the .length property of arrays:

st = write(fd, sbuf, sbuf.length);

Ali



Re: Use class template as a type

2016-11-29 Thread Jerry via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote:

abstract class MyClass {}
abstract class MyClassImpl(T)



Oops, forgot MyClassImpl should extend from MyClass.

abstract class MyClassImpl(T) : MyClass {
...
}



Re: Use class template as a type

2016-11-29 Thread Jerry via Digitalmars-d-learn

On Monday, 28 November 2016 at 11:26:41 UTC, dm wrote:

```
abstract class MyClass(T)
{
  public:
   @property const(T) value(){return _value;}
   @property void value(T val){_value = val;}
...
  private:
   T _value;
...
}


To avoid having to use the Object class directly you can make an 
base class of the class template.

Like:

```
abstract class MyClass {}
abstract class MyClassImpl(T)
{
public:
@property const(T) value(){return _value;}
@property void value(T val){_value = val;}
 ...
   private:
T _value;
 ...
}

MyClassInt and float inherits from MyClassImpl
```

And use it like:

```
void main() {
   MyClass[] objs;
   objs ~= new MyClassFloat();
   objs ~= new MyClassInt();
}
```



Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Nemanja Boric via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 15:30:33 UTC, Anders S wrote:

Hi guys,

I want to write into a fifo pipe using write( ...)
Now a gather my data into my own struct IOREQ so in order to 
write I have to cast into an char buffer.


My problem in dlang is that it doesn't accept the casting 
(IOREQ *) I get:
Error: Cannot implicitly convert expression () of type 
char [1024] to IOREQ*


define FC_GETSTATUS 501;
typedef struct {
SHORT   fc; /* function code */
SHORT   rs; /* return code */
INT size;  /* size of this request, 
including header */
SHORT   src;/* source */

INT argv[1];/* list of arguments */
} IOREQ;

int WritePipe(int fd, int mess, int argc)
{
  struct IOREQ * io; // my array of data
  char sbuf[1024];
  io = (IOREQ *)buf;  // Not accepted in dlang
  io.fc = FC_GETSTATUS;
  io.src = getpid();// works

. // add more data

  st = write(fd, sbuf, 1024);  //
  return st;
}


First, instead of:

 typedef struct {
SHORT   fc; /* function code */
SHORT   rs; /* return code */
INT size;   /* size of this request, including */
SHORT   src;/* source */

INT argv[1];/* list of arguments */
 } IOREQ;

just

 struct IOREQ {
SHORT   fc; /* function code */
SHORT   rs; /* return code */
INT size;   /* size of this request, including */
SHORT   src;/* source */

INT argv[1];/* list of arguments */
 } IOREQ;

Then, `write` has the following definition:

https://github.com/dlang/druntime/blob/master/src/core/sys/posix/unistd.d#L100

`ssize_t write(int, in void*, size_t);`

so, it accepts pointer to anything, no need to mess with `char[]`:

IOREQ mystruct;
mystruct.src = getpid();
// etc...

// write it out
write(fd, , mystruct.sizeof); // todo: Don't forget 
to check for
   // the return value, 
etc.




Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Nemanja Boric via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 15:55:57 UTC, Nemanja Boric wrote:

just

 struct IOREQ {
SHORT   fc; /* function code */
SHORT   rs; /* return code */
INT size;   /* size of this request, including */
SHORT   src;/* source */

INT argv[1];/* list of arguments */
 } IOREQ;



Sorry, there's an extra IOREQ before and semicolon.


  struct IOREQ {
SHORT   fc; /* function code */
SHORT   rs; /* return code */
INT size;   /* size of this request, including */
SHORT   src;/* source */

INT argv[1];/* list of arguments */
  }


Cannot implicitly convert expression () of type char [1024] to IOREQ *

2016-11-29 Thread Anders S via Digitalmars-d-learn

Hi guys,

I want to write into a fifo pipe using write( ...)
Now a gather my data into my own struct IOREQ so in order to 
write I have to cast into an char buffer.


My problem in dlang is that it doesn't accept the casting (IOREQ 
*) I get:
Error: Cannot implicitly convert expression () of type char 
[1024] to IOREQ*


define FC_GETSTATUS 501;
typedef struct {
SHORT   fc; /* function code */
SHORT   rs; /* return code */
INT size;  /* size of this request, 
including header */
SHORT   src;/* source */

INT argv[1];/* list of arguments */
} IOREQ;

int WritePipe(int fd, int mess, int argc)
{
  struct IOREQ * io; // my array of data
  char sbuf[1024];
  io = (IOREQ *)buf;  // Not accepted in dlang
  io.fc = FC_GETSTATUS;
  io.src = getpid();// works

. // add more data

  st = write(fd, sbuf, 1024);  //
  return st;
}



Re: How can I concatenate a string, a char array and an int

2016-11-29 Thread Anders S via Digitalmars-d-learn

Thanks guys for a really quick answer !!
OK, a little bit awkward to use but getting there
posting a new question about char * to struct ;)

Thanks
/anders





Re: Converting all enum members to a string fails with version 2.0.72.0

2016-11-29 Thread Stefan via Digitalmars-d-learn

On Monday, 21 November 2016 at 18:53:59 UTC, Stefan wrote:
On Monday, 21 November 2016 at 17:26:37 UTC, Jonathan M Davis 
wrote:

[...]


Thanks Jonathan for the explanation. The cast works fine but 
feels "unsafe".


I will wait for the next version.

Stefan


Version D 2.072.1 Beta fixed my problem.


Re: How can I concatenate a string, a char array and an int

2016-11-29 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 10:21:24 UTC, Anders S wrote:

Hi guys,

just started to get into Dlang, comming from C and C++ I like 
to use methods like there if possible.


Now I want to catenate something like this, but don't get it to 
work

in standard C i code:
   char str[80];
   sprintf(str, "This is a number = %f", 3.14356);

Now in Dlang and import core.stdc.string and
code:
char [80] str;
sprintf(str, "This is a number = %d", 314356);
writefln("%s", str);



sprintf(str.ptr, "This is a number = %d".toStringz,314356);


but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*) 
format, ...) is not callable using argument types (char[80], 
string, int)




because in D arrays do not decay to pointers. To get a null 
terminated string use toStringz



Nor does this work
   char [50] temp = "This is a number";
   string greeting5= temp~" "~314356;
   writefln("%s",greeting5);



add a to!string

314356.to!string


result in error:
Error: incompatible types for ((cast(const(char)[])temp ~ " ") 
~ (314356)): 'char[]' and 'int'



Any ideas or hints?
/anders




Re: how to copy const struct with indirections to mutable one (of the same type)

2016-11-29 Thread drug via Digitalmars-d-learn

29.11.2016 13:49, Mathias Lang пишет:

On Tuesday, 29 November 2016 at 10:46:04 UTC, drug wrote:

I had the following code:
```
import std.algorithm: equal;

[...]


You are not calling the (identity) opAssign here, but postblit.

To call identity opAssign, you need an already constructed instance:

```
Data mutable_data;
mutable_data = const_data;
```


Thanks for clarification!


Re: how to copy const struct with indirections to mutable one (of the same type)

2016-11-29 Thread Mathias Lang via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 10:46:04 UTC, drug wrote:

I had the following code:
```
import std.algorithm: equal;

[...]


You are not calling the (identity) opAssign here, but postblit.

To call identity opAssign, you need an already constructed 
instance:


```
Data mutable_data;
mutable_data = const_data;
```


how to copy const struct with indirections to mutable one (of the same type)

2016-11-29 Thread drug via Digitalmars-d-learn

I had the following code:
```
import std.algorithm: equal;

struct Data
{
int[3] arr;
}

int main()
{
auto const_data = const(Data)([1, 2, 3]);
assert(const_data.arr[].equal([1, 2, 3]));

Data mutable_data = const_data;
assert(mutable_data.arr[].equal([1, 2, 3]));

return 0;
}
```
It workred fine. Now I replace static array by std.container.Array:
```
import std.algorithm: copy, equal;
import std.container: Array;

struct Data
{
Array!int arr;

this(R)(R r)
{
r.copy(arr[]);
}

ref Data opAssign(const(Data) other)
{
pragma(msg, __FUNCTION__);

import std.range: lockstep;

this.arr.length = other.arr.length;
foreach(ref s, ref d; lockstep(other.arr[], this.arr[]))
d = s;

return this;
}
}

int main()
{
auto const_data = const(Data)([1, 2, 3]);
assert(const_data.arr[].equal([1, 2, 3]));

Data mutable_data = const_data;
assert(mutable_data.arr[].equal([1, 2, 3]));

return 0;
}
```

and it fails now because:
``
Error: conversion error from const(Data) to Data
```
I can solve my problem in other ways but I'd like to know why opAssign 
overload doesn't work in this case?


Thank in advance


Re: How can I concatenate a string, a char array and an int

2016-11-29 Thread rumbu via Digitalmars-d-learn

On Tuesday, 29 November 2016 at 10:21:24 UTC, Anders S wrote:

Hi guys,

just started to get into Dlang, comming from C and C++ I like 
to use methods like there if possible.


Now I want to catenate something like this, but don't get it to 
work

in standard C i code:
   char str[80];
   sprintf(str, "This is a number = %f", 3.14356);


import std.format;
string str = format("This is a number = %f", 3.14356);



Now in Dlang and import core.stdc.string and
code:
char [80] str;
sprintf(str, "This is a number = %d", 314356);
writefln("%s", str);




but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*) 
format, ...) is not callable using argument types (char[80], 
string, int)


Nor does this work
   char [50] temp = "This is a number";
   string greeting5= temp~" "~314356;
   writefln("%s",greeting5);


import std.conv;
string temp = "This is a number";
string greeting5 = temp ~ " " ~ to!string(314356);




result in error:
Error: incompatible types for ((cast(const(char)[])temp ~ " ") 
~ (314356)): 'char[]' and 'int'



Any ideas or hints?
/anders







How can I concatenate a string, a char array and an int

2016-11-29 Thread Anders S via Digitalmars-d-learn

Hi guys,

just started to get into Dlang, comming from C and C++ I like to 
use methods like there if possible.


Now I want to catenate something like this, but don't get it to 
work

in standard C i code:
   char str[80];
   sprintf(str, "This is a number = %f", 3.14356);

Now in Dlang and import core.stdc.string and
code:
char [80] str;
sprintf(str, "This is a number = %d", 314356);
writefln("%s", str);

but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*) 
format, ...) is not callable using argument types (char[80], 
string, int)


Nor does this work
   char [50] temp = "This is a number";
   string greeting5= temp~" "~314356;
   writefln("%s",greeting5);

result in error:
Error: incompatible types for ((cast(const(char)[])temp ~ " ") ~ 
(314356)): 'char[]' and 'int'



Any ideas or hints?
/anders





Re: Use class template as a type

2016-11-29 Thread ag0aep6g via Digitalmars-d-learn

On 11/29/2016 02:21 AM, Basile B. wrote:

The cast from a class type to a sub class in itself does absolutely
nothing.


That can't be right. A bad downcast gives you null, so it has to check 
the dynamic type information. Compare with upcasts which are statically 
known to be correct, so they don't need to check anything at runtime.