Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Godnyx via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote:

On 12/23/20 3:23 PM, Godnyx wrote:


Any ideas?


Just fix your typos:

```D
import std : printf, toStringz;

void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
static if (is(typeof(args[i]) == string))
printf("%s\n", args[i].toStringz);
static if (is(typeof(args[i]) == int))
printf("%i\n", args[i]);
static if (is(typeof(args[i]) == bool)) {
if (args[i])
printf("true");
else
printf("false");
}
}
}

void main() {
put("Prompt:", "Hello, my age is: ", 19, true);
}
```

P.S. replace `arg` by `args[i]`


Damn I'm fucking BLIND! Anyway I changed it to foreach (x; args) 
to get the args itself. Thanks for anything man! The community is 
the best thing about D!!! ;)


Re: C++ or D?

2020-12-23 Thread evilrat via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 18:03:56 UTC, frame wrote:


It's not the problem mentioned but I had to struggle with DLLs 
and D's Variant-type. The problem is that Variant uses TypeInfo 
which does not pass DLL boundaries correctly so that int != int 
in runtime even it's in fact a simple int.


If you need to exchange unknown data between a DLL and your 
application you need to get a workaround and cannot use that 
elsewhere settled nice feature. But it's a Windows specific 
issue - it works as expected on other systems.


Which is basically same as in C++, despite the fact it does have 
real working SO/DLL runtime's many large projects have their own 
RTTI implementation. LLVM has its own RTTI because standard type 
info is "inefficient", Unreal Engine has its own, IIRC Qt too has 
its own, etc...


Same thing with D Variant, some people say it is "inefficient"... 
so we ended up having multiple libraries.


Not saying anything about how good or bad all this, just the 
facts.


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Godnyx via Digitalmars-d-learn
On Wednesday, 23 December 2020 at 13:55:20 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote:

static foreach (ulong i; 0..args.length) {
static if (is(typeof(args[i]) == string))
printf("%s\n", args[i].toStringz);
static if (is(typeof(args[i]) == int))


Putting some `else` in there would help too to ensure your 
thing only ever matches one branch.


doesn't matter here but will later if you add generic array 
support.


and then there's const etc but that's yet another thing so wait 
on that till you have the basics down


I will probably but I probably won't add array, class support. 
Probably I'll use this only for debug purposes with std.write. 
Thanks a lot for everything!!!


Re: C++ or D?

2020-12-23 Thread frame via Digitalmars-d-learn

On Saturday, 19 December 2020 at 09:06:33 UTC, Godnyx wrote:
Hi! Can you be more specific about the problems someone is 
gonna face with D that can't be fixed? This is very important 
for me because I'm planning to use D for development in the 
near (I wish near) future and I want to know what's going on. 
So yeah some examples will be appreciated!


It's not the problem mentioned but I had to struggle with DLLs 
and D's Variant-type. The problem is that Variant uses TypeInfo 
which does not pass DLL boundaries correctly so that int != int 
in runtime even it's in fact a simple int.


If you need to exchange unknown data between a DLL and your 
application you need to get a workaround and cannot use that 
elsewhere settled nice feature. But it's a Windows specific issue 
- it works as expected on other systems.




Re: Slice allocation after appending

2020-12-23 Thread Ali Çehreli via Digitalmars-d-learn

On 12/23/20 8:14 AM, frame wrote:

> That implementation
> can become very handy for some situations but for this simple case
>
> foreach (arr; [a, b]) { .. }
>
> would also work.

Absolutely.

> The difference is that the foreach loop is happen at
> runtime and will not compiled as multiple lines.

AliasSeq also allowed me to print the names of the local array variables 
'a' and 'b'.


Ali



Re: Getting started with graphqld

2020-12-23 Thread aberba via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 08:33:21 UTC, Trustee wrote:

On Tuesday, 22 December 2020 at 23:49:12 UTC, aberba wrote:

On Friday, 18 December 2020 at 03:36:05 UTC, Trustee wrote:

[...]


Heres's a demo I put together 
https://github.com/aberba/graphqld-demo


A minimal example with only the essential APIs


Thank you for this. Between this and the test code I should 
have enough to get going with.


I did also begin working through the test code and it was 
beginning to make sense. Being pushed for time though, I did 
spin up a Prisma 2 based gateway server. This time with the 
intention to build out a D version in parallel as I learn the 
package.


This should speed things along.

I will keep you posted on any developments and I'll be sure to 
get in touch if I have any questions.


Thanks again.


I keep hearing about Prisma too... gotta learn myself.


Re: Slice allocation after appending

2020-12-23 Thread frame via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 11:19:38 UTC, Rekel wrote:

I'm not sure what your aliasSeq does, sadly I don't find the 
documentation's explanation satisfactory.


Try to write

static foreach (arr; [a, b]) { .. }

- it will not work because that loop is generated at compile time 
(static). It will generate the inside code for every usage later 
in code and for each element (a and b) so the 2 lines inside will 
be compiled to 4 lines really when you call info().


But the compiler cannot access the values of a and b at compile 
time (which it thinks it should by using "static" in this case) 
so we need to say the compiler:


- do not use value or address of [a, b] directly
- instead, use [a, b] as symbol - just prepare the code for later 
usage


This is what AliasSeq! does in this example. You can put all 
static stuff inside an AliasSequence:


41  = literal/value
int = type
int[]   = type
int[] a = symbol/alias


Here it contains not values but the symbols to it. That 
implementation can become very handy for some situations but for 
this simple case


foreach (arr; [a, b]) { .. }

would also work. The difference is that the foreach loop is 
happen at runtime and will not compiled as multiple lines.





Re: Slice allocation after appending

2020-12-23 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/22/20 5:12 PM, Rekel wrote:
According to the D slice article 
(https://dlang.org/articles/d-array-article.html), slices do not care 
where they start, only where they end, when checking whether expanding 
in place is permitable, or at least that is what I understood regarding it.


Now I'm unsure how to check this, I tried to a bit using the online 
editor and a bit of pointer usage which seemed to confirm my suspicion, 
but does this mean that taking a (small) slice at the end of a 
(possibly) very large dynamic array can lead to problematic behavior?


Problematic in what way? It will not stomp on data that is live. So I'd 
say it's the opposite of problematic.


You can check whether an append will reallocate or not by checking the 
capacity. If arr.capacity <= arr.length, then an append will reallocate.



For example;


int[] a = new int[10]; // Imagine this is very large
int[] b = a[$-1..$];   // Small slice at the end
b ~= 2;    // b extends, possibly in place
a ~= -1;   // a no longer can, so the entire array needs 
reallocating

(instead of be reallocating & a growing in place)


For sure a will need reallocating. The runtime cannot know that you will 
append with a, so it uses the space for b.


If you'd rather b reallocate, you can use the concatenation operator on 
it instead:


b = b ~ 2;

Or you can manage the array allocations yourself without using the 
runtime (see std.array.Appender).




Again I'm not very certain I fully understood how slices are 
implemented, but is this example, and the problem I imagine it leading 
to, valid?


Is the description you have valid? yes. Is it a problem with the 
implementation? I'd say no. If you change your expectations, you can 
avoid this situation easily.


-Steve


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 13:06:37 UTC, drug wrote:

static foreach (ulong i; 0..args.length) {
static if (is(typeof(args[i]) == string))
printf("%s\n", args[i].toStringz);
static if (is(typeof(args[i]) == int))


Putting some `else` in there would help too to ensure your thing 
only ever matches one branch.


doesn't matter here but will later if you add generic array 
support.


and then there's const etc but that's yet another thing so wait 
on that till you have the basics down


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread drug via Digitalmars-d-learn

On 12/23/20 3:23 PM, Godnyx wrote:


Any ideas?


Just fix your typos:

```D
import std : printf, toStringz;

void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
static if (is(typeof(args[i]) == string))
printf("%s\n", args[i].toStringz);
static if (is(typeof(args[i]) == int))
printf("%i\n", args[i]);
static if (is(typeof(args[i]) == bool)) {
if (args[i])
printf("true");
else
printf("false");
}
}
}

void main() {
put("Prompt:", "Hello, my age is: ", 19, true);
}
```

P.S. replace `arg` by `args[i]`



Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Godnyx via Digitalmars-d-learn
On Wednesday, 23 December 2020 at 09:42:42 UTC, Ferhat Kurtulmuş 
wrote:
On Wednesday, 23 December 2020 at 09:40:27 UTC, Ferhat 
Kurtulmuş wrote:

On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote:

[...]


I didn't dive into your use case, but you should use static 
foreach in this case:


void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
if (typeof(args[i]).stringof == "string")
printf("%s\n", args[i].toStringz);
}
}


and better:
void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
if (is(typeof(args[i]) == string))
printf("%s\n", args[i].toStringz);
}
}


This method works but I won't let me use arguments other than 
string. Ali gave a solution that will solve this limitation! See 
me reply below!


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Godnyx via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 09:50:03 UTC, Ali Çehreli wrote:

On 12/23/20 1:06 AM, Godnyx wrote:

>  for (ulong i = 0; i < args.length; i++) {
>  if (typeof(args[i]).stringof == "string")
>  printf("%s\n", args[i].toStringz);
>  }

I replaced for with foreach and it worked (and I passed 
"prompt"). static foreach would work as well.


import core.stdc.stdio : printf;
import std.string : toStringz;

void put(A...)(string prompt, A args) {
foreach (arg; args) {
if (typeof(arg).stringof == "string")
printf("%s\n", arg.toStringz);
}
}

void main() {
string h = "World!";
string w = "World!";
put("prompt", h, w);
}

But it can get better: you don't want to compare typeof with 
"string" at run time. Instead, there shouldn't be any code 
generated for the non-string cases. Enter 'static if' and the 
'is' expression to feel better. :)


import core.stdc.stdio : printf;
import std.string : toStringz;

void put(A...)(string prompt, A args) {
  static foreach (arg; args) {
static if (is (typeof(arg) == string)) {
  printf("%s\n", arg.toStringz);
}
  }
}

void main() {
  string h = "World!";
  string w = "World!";
  put("prompt", h, w);
}

Ali


Probably the best solution! It also let's me use types other than 
string! Example: put("Prompt:", "Hello, my age is: ", 19, true); 
tho still I can't print anything. This is the code:


void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
static if (is(typeof(arg) == string))
printf("%s\n", args[i].toStringz);
static if (is(typeof(arg) == int))
printf("%i\n", args[i]);
static if (is(typeof(arg) == bool)) {
if (arg)
printf("true");
else
printf("false");
}
}
}

void main() {
put("Prompt:", "Hello, my age is: ", 19, true);
}

Any ideas?


Re: Slice allocation after appending

2020-12-23 Thread Rekel via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 04:03:37 UTC, Ali Çehreli wrote:

It is valid. One can always copy the small array before 
appending to it and the large array would be preserved.


Try the -profile command line switch when compiling your 
program and it will show where memory allocations occur. Very 
helpful in exposing such problem spots...


Ali


I'm not sure what your aliasSeq does, sadly I don't find the 
documentation's explanation satisfactory. Though thank's a lot, 
now I know what to take into account, & I didn't know about the 
-profile switch yet ^^


Re: Why is (int[int] s = int[int].init) not allowed

2020-12-23 Thread Andre Pany via Digitalmars-d-learn
On Wednesday, 23 December 2020 at 07:08:31 UTC, Daniel Kozak 
wrote:
Dne st 23. 12. 2020 1:00 uživatel Steven Schveighoffer via 
Digitalmars-d-learn  napsal:



On 12/22/20 5:44 PM, Daniel Kozak wrote:
> [...]

Yeah:

void sample_valid(int[int] s = null)

-Steve



Yes AA.init is null per doc.

https://dlang.org/spec/hash-map.html#construction_and_ref_semantic


Thanks for clarification, I was not aware that AA.init is 
technically the same as null.


Kind regards
Andre


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Ali Çehreli via Digitalmars-d-learn

On 12/23/20 1:06 AM, Godnyx wrote:

>  for (ulong i = 0; i < args.length; i++) {
>  if (typeof(args[i]).stringof == "string")
>  printf("%s\n", args[i].toStringz);
>  }

I replaced for with foreach and it worked (and I passed "prompt"). 
static foreach would work as well.


import core.stdc.stdio : printf;
import std.string : toStringz;

void put(A...)(string prompt, A args) {
foreach (arg; args) {
if (typeof(arg).stringof == "string")
printf("%s\n", arg.toStringz);
}
}

void main() {
string h = "World!";
string w = "World!";
put("prompt", h, w);
}

But it can get better: you don't want to compare typeof with "string" at 
run time. Instead, there shouldn't be any code generated for the 
non-string cases. Enter 'static if' and the 'is' expression to feel 
better. :)


import core.stdc.stdio : printf;
import std.string : toStringz;

void put(A...)(string prompt, A args) {
  static foreach (arg; args) {
static if (is (typeof(arg) == string)) {
  printf("%s\n", arg.toStringz);
}
  }
}

void main() {
  string h = "World!";
  string w = "World!";
  put("prompt", h, w);
}

Ali



Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Wednesday, 23 December 2020 at 09:40:27 UTC, Ferhat Kurtulmuş 
wrote:

On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote:

[...]


I didn't dive into your use case, but you should use static 
foreach in this case:


void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
if (typeof(args[i]).stringof == "string")
printf("%s\n", args[i].toStringz);
}
}


and better:
void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
if (is(typeof(args[i]) == string))
printf("%s\n", args[i].toStringz);
}
}


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 09:06:02 UTC, Godnyx wrote:
On Wednesday, 23 December 2020 at 08:50:50 UTC, Mike Parker 
wrote:

On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote:

Yep and I find it out! It won't work with templates and/or 
variadic function parameters. It says that the variable can't 
be read at compile time (so I can't cast it) or it will work 
but it will give me a segmentation fault (lol hello C). Any 
idea why this is happening in those cases?


Please show the code that's causing the error. Without it, all 
anyone can do is keep making suggestions that *might* be the 
problem. With the code, someone can point to it exactly.


Yep that's the best thing I can do! Code:

import core.stdc.stdio : printf;
import std.string : toStringz;

void put(A...)(string prompt, A args) {
for (ulong i = 0; i < args.length; i++) {
if (typeof(args[i]).stringof == "string")
printf("%s\n", args[i].toStringz);
}
}

void main() {
string h = "World!";
string w = "World!";
put(h, w);
}

I'm getting two errors. First that i can't be read at compile 
time and second that I don't initialize the function right. So 
I know I'm doing something wrong but I don't know why... Any 
ideas?


I didn't dive into your use case, but you should use static 
foreach in this case:


void put(A...)(string prompt, A args) {
static foreach (ulong i; 0..args.length) {
if (typeof(args[i]).stringof == "string")
printf("%s\n", args[i].toStringz);
}
}


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Godnyx via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 08:50:50 UTC, Mike Parker wrote:

On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote:

Yep and I find it out! It won't work with templates and/or 
variadic function parameters. It says that the variable can't 
be read at compile time (so I can't cast it) or it will work 
but it will give me a segmentation fault (lol hello C). Any 
idea why this is happening in those cases?


Please show the code that's causing the error. Without it, all 
anyone can do is keep making suggestions that *might* be the 
problem. With the code, someone can point to it exactly.


Yep that's the best thing I can do! Code:

import core.stdc.stdio : printf;
import std.string : toStringz;

void put(A...)(string prompt, A args) {
for (ulong i = 0; i < args.length; i++) {
if (typeof(args[i]).stringof == "string")
printf("%s\n", args[i].toStringz);
}
}

void main() {
string h = "World!";
string w = "World!";
put(h, w);
}

I'm getting two errors. First that i can't be read at compile 
time and second that I don't initialize the function right. So I 
know I'm doing something wrong but I don't know why... Any ideas?


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 08:45:15 UTC, Godnyx wrote:

Yep and I find it out! It won't work with templates and/or 
variadic function parameters. It says that the variable can't 
be read at compile time (so I can't cast it) or it will work 
but it will give me a segmentation fault (lol hello C). Any 
idea why this is happening in those cases?


Please show the code that's causing the error. Without it, all 
anyone can do is keep making suggestions that *might* be the 
problem. With the code, someone can point to it exactly.


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Godnyx via Digitalmars-d-learn

On Wednesday, 23 December 2020 at 04:02:54 UTC, Paul Backus wrote:

On Tuesday, 22 December 2020 at 21:26:37 UTC, Godnyx wrote:

On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:

Is there a way? If not then how std.stdio does it?


I should mention that I want to use it in a variable that 
can't be read at compile time so .toStringz is not working for 
me.


toStringz works just fine on variables that can't be read at 
compile time. You must be doing something else to trigger that 
error.


Yep and I find it out! It won't work with templates and/or 
variadic function parameters. It says that the variable can't be 
read at compile time (so I can't cast it) or it will work but it 
will give me a segmentation fault (lol hello C). Any idea why 
this is happening in those cases?


Re: Can I output strings using core.stdc.stdio?

2020-12-23 Thread Godnyx via Digitalmars-d-learn

On Tuesday, 22 December 2020 at 21:40:15 UTC, Dave P. wrote:

On Tuesday, 22 December 2020 at 21:37:23 UTC, Godnyx wrote:

On Tuesday, 22 December 2020 at 21:28:10 UTC, Dave P. wrote:

On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:
[...]


Lol. Actually I just don't want to use Phobos and trying to 
stay on core. Unfortunately, my variable can't be read at 
compile time so I doesn't work. Any other ideas?


What I wrote should still work in non-betterC as long as you 
are linking to libc.


You can also just write to stdout directly if all you need is 
to write the string without any extra formatting:


fwrite(somestring.ptr, 1, somestring.length, stdout);


They is another problem. See the reply I did to Paul Backus


Re: Getting started with graphqld

2020-12-23 Thread Trustee via Digitalmars-d-learn

On Tuesday, 22 December 2020 at 23:49:12 UTC, aberba wrote:

On Friday, 18 December 2020 at 03:36:05 UTC, Trustee wrote:

On Thursday, 17 December 2020 at 14:49:42 UTC, evilrat wrote:

On Tuesday, 15 December 2020 at 16:25:29 UTC, Trustee wrote:


connect a basic vibe-d app to a graphql backend.



umm, what?
Did you mean write graphql backend using vibe.d?


Vibe-d web app -> Vibe-d/GraphQL gateway server (a la Prisma 
1) -> Vibe-d/GraphQL API server -> Data.


That's why I'm more interested in the workings of the package 
than a "How-To get a basic vibe-d/graphql server. I want to 
know which pieces are available OOTB to be put together to 
create the above, and which pieces need to be created.


Heres's a demo I put together 
https://github.com/aberba/graphqld-demo


A minimal example with only the essential APIs


Thank you for this. Between this and the test code I should have 
enough to get going with.


I did also begin working through the test code and it was 
beginning to make sense. Being pushed for time though, I did spin 
up a Prisma 2 based gateway server. This time with the intention 
to build out a D version in parallel as I learn the package.


This should speed things along.

I will keep you posted on any developments and I'll be sure to 
get in touch if I have any questions.


Thanks again.