Re: Use C struct declaration without knowing definition

2019-07-24 Thread Ben Ogles via Digitalmars-d-learn

On Thursday, 25 July 2019 at 01:08:48 UTC, Ali Çehreli wrote:
And that type is traded as pointer to some_type on the C API, 
right? Usually there is a factory method that returns a pointer 
to a dynamically allocated object.


Correct, there is a function prototype that returns a dynamically 
allocated instance of the struct.


Indeed. The only option is to use some_type* on the D API as 
well:


extern(C) {
  struct S;
  S* C_API_allocate();
  void C_API(const(S) *);
}

// D API
void foo(S* s)  {
  C_API(s);
}

void main() {
  auto s = C_API_allocate();
  foo(s);
}


This makes sense. I can only deal with pointers to dynamically 
allocated instances of the struct since there is no way to know 
the size of the struct at compile time. Now that I think about 
it, this would be the case whether or not I was using D at all.


Thanks for your help!





Re: Use C struct declaration without knowing definition

2019-07-24 Thread Ali Çehreli via Digitalmars-d-learn

On 07/24/2019 05:35 PM, Ben Ogles wrote:
> Is there a way to use a C header that contains a struct declaration but
> no definition?
>
> For example, the C header may contain:
>
> typedef struct some_type_s some_type;
>
> Which is only fully defined in the implementing C file:
>
> struct some_type_s { ... };

And that type is traded as pointer to some_type on the C API, right? 
Usually there is a factory method that returns a pointer to a 
dynamically allocated object.


> I am trying to wrap the header in D:
>
> extern(C)
> {
>struct some_type_s;
>alias some_type = some_type_s;
> }
>
> And then use it in another D file:
>
> some_type s;
>
> But dmd does not know the size of the struct so it can't compile. Is
> there a work around to this other than moving the struct definition into
> the C header file?

Indeed. The only option is to use some_type* on the D API as well:

extern(C) {
  struct S;
  S* C_API_allocate();
  void C_API(const(S) *);
}

// D API
void foo(S* s)  {
  C_API(s);
}

void main() {
  auto s = C_API_allocate();
  foo(s);
}

Note: That program fails to link because C API functions are missing 
definitions.


You can wrap the pointer i a D struct and dispatch member function calls 
to the C API.


Ali



Use C struct declaration without knowing definition

2019-07-24 Thread Ben Ogles via Digitalmars-d-learn
Is there a way to use a C header that contains a struct 
declaration but no definition?


For example, the C header may contain:

typedef struct some_type_s some_type;

Which is only fully defined in the implementing C file:

struct some_type_s { ... };

I am trying to wrap the header in D:

extern(C)
{
  struct some_type_s;
  alias some_type = some_type_s;
}

And then use it in another D file:

some_type s;

But dmd does not know the size of the struct so it can't compile. 
Is there a work around to this other than moving the struct 
definition into the C header file?




Re: Return the complete number

2019-07-24 Thread Simen Kjærås via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 16:16:15 UTC, Greatsam4sure wrote:

On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:

On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:
On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure 
wrote:

int main(){
 double mum = 0;
 Write("enter a number: ")
 readf(" %s\n",);
 Writeln(num);

}

How do I return the complete number the user enter since I 
don't since the user can enter numbers of various length 
with dmd approximating it.


I will appreciate any help


  readf!" %s\n"( num );
or
  num.readf!" %s\n";
or
  num = readln.strip.to!double;


  writeln(num)

The result is always to six significant figure. I want the 
whole number to be diaplay


So, there are several possible issues here. First, floating-point 
numbers have limited precision - a double can not correctly 
represent all integers above 2^52, for instance. That's 4.5 
quadrillion though, so rarely an issue. In your case, as others 
have commented, writefln with a correctly chosen format string[1] 
will do the trick.


However, what is the correctly chosen format string? This is a 
very hard question to answer, as it depends on what number was 
typed, and what you mean by 'the complete number the user enter'. 
While drug's suggestion of "%.16f" will return an accurate 
representation of the floating-point number, it will include a 
lot of trailing zeroes that were presumably not present in the 
input string. It will also expose floating-point's limitations 
when presented with huge numbers, like 1e23 will print 
1603000.. The best option might 
be "%.16g", which will choose the shortest of the scientific 
notation and the regular notation, as well as strip trailing 
zeroes. Depending on the criteria though, this might still not be 
the optimal solution.


--
  Simen


1: https://dlang.org/phobos/std_format.html#format-string


Re: How to contact people on the forum

2019-07-24 Thread bachmeier via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 16:37:33 UTC, Greatsam4sure wrote:

On Wednesday, 24 July 2019 at 15:56:43 UTC, drug wrote:

24.07.2019 18:51, Greatsam4sure пишет:
Good day everyone. I am thinking,  if there is a  way to 
contact any person on dlang forums through mail or any other 
means. How do I get their email from their forum post?


I use thunderbird to read the forum and every post contains 
email of its author, so I would use it to communicate.


I don't use thunderbird any other options


Email addresses are shown on the mailing list archive (and that's 
why I use a fake email address):


http://lists.puremagic.com/pipermail/digitalmars-d/


Re: How to contact people on the forum

2019-07-24 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 16:37:33 UTC, Greatsam4sure wrote:

On Wednesday, 24 July 2019 at 15:56:43 UTC, drug wrote:

24.07.2019 18:51, Greatsam4sure пишет:
Good day everyone. I am thinking,  if there is a  way to 
contact any person on dlang forums through mail or any other 
means. How do I get their email from their forum post?


I use thunderbird to read the forum and every post contains 
email of its author, so I would use it to communicate.


I don't use thunderbird any other options


On the web forum there”s a link labeled “Source” on every post. 
Click it to see the raw message, including the header which 
contains the email address. Just be aware that not everyone uses 
a valid email address.


Re: How to contact people on the forum

2019-07-24 Thread Greatsam4sure via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 15:56:43 UTC, drug wrote:

24.07.2019 18:51, Greatsam4sure пишет:
Good day everyone. I am thinking,  if there is a  way to 
contact any person on dlang forums through mail or any other 
means. How do I get their email from their forum post?


I use thunderbird to read the forum and every post contains 
email of its author, so I would use it to communicate.


I don't use thunderbird any other options




Re: dip1000, perhaps annotate with return, and vibe-d

2019-07-24 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 12:54:51 UTC, aliak wrote:

Trying to get dip1000 flag in use. I have this error:

Error: returning Optional(null, false).this(value) escapes a 
reference to parameter value, perhaps annotate with return


in this function:

public auto some(T)(auto ref T value) {
return Optional!T(value); // <-- error on this line
}

And it's instantiated from here:

static Optional!T fromRepresentation(Json value) {
  if (value == Json.undefined) {
return no!T;
  }
  return some(deserializeJson!T(value)); // <-- instantiated 
from here

}

I put the qualifier "return" on the parameter to some, but did 
nothing. And I put it on Optional.this() as well, but I'm not 
sure where I'm supposed to put it.


It should go on the constructor's parameter; i.e.,

this(auto return ref T value) { /* ... */ }

Under the hood, a constructor actually returns the constructed 
value by reference, so the actual signature of the above 
constructor seen by the lifetime checker is:


ref Optional!T __ctor(auto return ref T value)

You can see this for yourself with something like `pragma(msg, 
typeof(Optional!T.__ctor))`.


Re: Return the complete number

2019-07-24 Thread a11e99z via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 16:16:15 UTC, Greatsam4sure wrote:

On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:

On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:

On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure


The result is always to six significant figure. I want the 
whole number to be display




see another answers at thread
https://forum.dlang.org/post/qh9v10$19v1$1...@digitalmars.com
https://forum.dlang.org/post/afzqnnorzwosimfqn...@forum.dlang.org


Re: Return the complete number

2019-07-24 Thread Greatsam4sure via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 15:57:06 UTC, a11e99z wrote:

On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:
On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure 
wrote:

int main(){
 double mum = 0;
 Write("enter a number: ")
 readf(" %s\n",);
 Writeln(num);

}

How do I return the complete number the user enter since I 
don't since the user can enter numbers of various length with 
dmd approximating it.


I will appreciate any help


  readf!" %s\n"( num );
or
  num.readf!" %s\n";
or
  num = readln.strip.to!double;


  writeln(num)

The result is always to six significant figure. I want the whole 
number to be diaplay


oops! i misunderstood





Re: Return the complete number

2019-07-24 Thread a11e99z via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure wrote:

int main(){
 double mum = 0;
 Write("enter a number: ")
 readf(" %s\n",);
 Writeln(num);

}

How do I return the complete number the user enter since I 
don't since the user can enter numbers of various length with 
dmd approximating it.


I will appreciate any help


as drug said: writefln with formatting
in case no need prints just string representation use
  s/format https://dlang.org/phobos/std_format.html#format
auto str = num.format!"%.16s"; // using UFCS
or
auto str = "%.16s".format( num); // UFCS too for 2nd version of 
format

or
string str = format( "%.16s", num);

simple help can be asked on IRC channel too 
irc://irc.freenode.net/d

(HexChat, mIRC clients and others)


Re: Return the complete number

2019-07-24 Thread a11e99z via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 15:56:13 UTC, a11e99z wrote:

On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure wrote:

int main(){
 double mum = 0;
 Write("enter a number: ")
 readf(" %s\n",);
 Writeln(num);

}

How do I return the complete number the user enter since I 
don't since the user can enter numbers of various length with 
dmd approximating it.


I will appreciate any help


  readf!" %s\n"( num );
or
  num.readf!" %s\n";
or
  num = readln.strip.to!double;


oops! i misunderstood


Re: Return the complete number

2019-07-24 Thread a11e99z via Digitalmars-d-learn

On Wednesday, 24 July 2019 at 15:45:08 UTC, Greatsam4sure wrote:

int main(){
 double mum = 0;
 Write("enter a number: ")
 readf(" %s\n",);
 Writeln(num);

}

How do I return the complete number the user enter since I 
don't since the user can enter numbers of various length with 
dmd approximating it.


I will appreciate any help


  readf!" %s\n"( num );
or
  num.readf!" %s\n";
or
  num = readln.strip.to!double;


Re: How to contact people on the forum

2019-07-24 Thread drug via Digitalmars-d-learn

24.07.2019 18:51, Greatsam4sure пишет:
Good day everyone. I am thinking,  if there is a  way to contact any 
person on dlang forums through mail or any other means. How do I get 
their email from their forum post?


I use thunderbird to read the forum and every post contains email of its 
author, so I would use it to communicate.


Re: Return the complete number

2019-07-24 Thread drug via Digitalmars-d-learn

24.07.2019 18:45, Greatsam4sure пишет:

int main(){
  double mum = 0;
  Write("enter a number: ")
  readf(" %s\n",);
  Writeln(num);

}

How do I return the complete number the user enter since I don't since 
the user can enter numbers of various length with dmd approximating it.


I will appreciate any help


does it help?

writefln("%.8f", num); // for float

writefln("%.16f", num); // for double



How to contact people on the forum

2019-07-24 Thread Greatsam4sure via Digitalmars-d-learn
Good day everyone. I am thinking,  if there is a  way to contact 
any person on dlang forums through mail or any other means. How 
do I get their email from their forum post?


Return the complete number

2019-07-24 Thread Greatsam4sure via Digitalmars-d-learn

int main(){
 double mum = 0;
 Write("enter a number: ")
 readf(" %s\n",);
 Writeln(num);

}

How do I return the complete number the user enter since I don't 
since the user can enter numbers of various length with dmd 
approximating it.


I will appreciate any help


dip1000, perhaps annotate with return, and vibe-d

2019-07-24 Thread aliak via Digitalmars-d-learn

Trying to get dip1000 flag in use. I have this error:

Error: returning Optional(null, false).this(value) escapes a 
reference to parameter value, perhaps annotate with return


in this function:

public auto some(T)(auto ref T value) {
return Optional!T(value); // <-- error on this line
}

And it's instantiated from here:

static Optional!T fromRepresentation(Json value) {
  if (value == Json.undefined) {
return no!T;
  }
  return some(deserializeJson!T(value)); // <-- instantiated from 
here

}

I put the qualifier "return" on the parameter to some, but did 
nothing. And I put it on Optional.this() as well, but I'm not 
sure where I'm supposed to put it.


The constructor for Optional takes an auto ref T ... I'm not sure 
that makes sense. Does it make sense to take a constructor 
parameter that is copied in to the struct by auto ref if you're 
not calling .move on it explicitly?


And as for vibe-d, I get this when compiling with dip1000:

Also, vibe-d gives me this: 
../../../.dub/packages/vibe-core-1.6.2/vibe-core/source/vibe/internal/traits.d(391,2): Error: static assert:  "FileStream does not implement method "read" of type @safe ulong(scope ubyte[] dst, IOMode mode)"


Has anyone seen that or used vibed with dip1000?