Re: std.net.curl.HTTP: can't download more than 3.6K

2023-03-19 Thread Jeremy via Digitalmars-d-learn
Nevermind, it was my fault. I fixed my problem by appending the 
received buffer to my data buffer instead of overwriting my data 
buffer each time.


Re: Difference between using `import` with/without the colon

2023-03-19 Thread Jeremy via Digitalmars-d-learn

On Sunday, 19 March 2023 at 08:47:32 UTC, Basile B. wrote:

On Sunday, 19 March 2023 at 07:20:17 UTC, Jeremy wrote:

[...]


The colon-form, aka "selective import" has for effect

1. to create a local alias so this can indeed speedup symbol 
lookups in the sense that search will succeed before looking in 
the scope of the imports.
2. to make non-selected symbols, i.e not listed in the colon 
right hand side, in the import not available.


Note that using both makes no sense, but I guess you did that 
to express more clearly what you meant.


Ah, that makes sense. Thank you!


Re: alias Error: need 'this'

2023-03-19 Thread Ali Çehreli via Digitalmars-d-learn

On 3/19/23 06:49, bomat wrote:

> I can live with the `static`
> solution, I guess.

If you could, you would define it 'static' anyway. :) Because you highly 
likely needed a distinct 'variableWithALongName' member for each 
MyStruct object, that wouldn't work.


> Shouldn't it be the exact same underlying mechanism?

I don't know the answer. Luckily, what you describe is available in 
another form in the language:


ref alias2() {
return myStruct.memberWithALongName;
}

Ali

Unrelated: I find 'myObject' a more correct name for a variable because 
if 'struct' is the definition of a type, 'myStruct' attempts to convey a 
misleading meaning.




Re: alias Error: need 'this'

2023-03-19 Thread Basile B. via Digitalmars-d-learn

On Sunday, 19 March 2023 at 13:49:36 UTC, bomat wrote:
Thanks for the suggested workaround, I can live with the 
`static` solution, I guess.


I still don't understand why it's necessary, though.
Since a `struct` is a value type and, as I understand it, stack 
allocated, what difference does it make to the compiler whether 
I alias `variableWithALongName` or 
`myStruct.memberWithALongName`?

Shouldn't it be the exact same underlying mechanism?

Thanks and regards
bomat


D aliases are for symbols, but what you try to alias is an 
expression.


You might feel that what  you request may work, but that's only a 
very particular case. Generally expressions cannot be aliased 
because without context they become polymorphic (or erather 
polysemous).


Re: Traverse a DList and insert / remove in place?

2023-03-19 Thread Armando via Digitalmars-d-learn
ok there is the issue of not knowing how to traverse the list 
after removing the current element (=> the one that came after of 
course). But how about even just keeping a pointer (range) to 
'element' that can be used after the list has been traversed in 
full to use in remove / insertAfter?


I think I can achieve this with associative arrays (below) but it 
seems overkill to use an associative effectively as a DList?


```d
struct myType{
int id;
int prev, next; // id of prev and next
// [...] other stuff

this(int id_, int prev_, int next_)
{
//[...]
}
}

myType[int] list; // key = id

int[] to_remove;
myType[] to_add;

foreach(elem; list)
{
// [...] do something long and complicated with elem, throw a 
random number rand


if(rand < 0.5)
to_remove ~= elem.id; // flag for removal
else
to_add ~= myType(some_new_id, elem.id, elem.next); // add 
new elem after current

}

foreach(elem; to_add)
list[elem.id] = elem;

foreach(id; to_remove)
{
// reconnect: prev's next to next, and next's prev to prev
list[list[id].prev].next = list[id].next;
list[list[id].next].prev = list[id].prev;
list.remove(id);
}

```



Re: alias Error: need 'this'

2023-03-19 Thread bomat via Digitalmars-d-learn

On Sunday, 19 March 2023 at 12:29:19 UTC, Salih Dincer wrote:
It is possible to achieve the convenience you want to achieve 
in 2 ways. One of them is to use a static member but if not, to 
use an alias inside the container.


Thanks for the suggested workaround, I can live with the `static` 
solution, I guess.


I still don't understand why it's necessary, though.
Since a `struct` is a value type and, as I understand it, stack 
allocated, what difference does it make to the compiler whether I 
alias `variableWithALongName` or `myStruct.memberWithALongName`?

Shouldn't it be the exact same underlying mechanism?

Thanks and regards
bomat


Traverse a DList and insert / remove in place?

2023-03-19 Thread Armando via Digitalmars-d-learn
Say I have a DList. I am looking for a vanilla way to 
insert/remove elements in place while traversing that list.


```d
import std.container : DList;

struct myType{
   int id;
   // [...] other stuff
}

auto list = DList!myType();

// [...] populate list with elements
```

To remove the element with id=3 I can do:
```d
list.linearRemove(list[].find!(a => a.id==3).take(1));
```
but according to [this 
post](https://forum.dlang.org/post/mailman.3110.1356864469.5162.digitalmar...@puremagic.com) linearRemove might be less efficient than remove and remove can't be used. Also in the example above, finding the needle is easy but my case would involve more complicated scenarios that I am relunctant to bury into find's predicate.


I would like to do something like traversing a DList, operating 
on the current element, and potentially removing that element or 
inserting a new one before/after it - an easy operation if you 
code a DList yourself. Maybe I missed something?


```d
// Traverse and operate on current element
foreach(element; list)
{
// [...] do something long and complicated with m, throw a 
random number rand


if(rand < 0.5)
list.removeInPlace(); // remove 'element'
else
list.insertInPlace(myType(...)); // insert a new element 
"here" (before/after 'element')

}
```

Any way to achieve this simply with D? E.g. is there a way to 
extract the 'reading head' of a list being traversed and to use 
that head as a range into the remove, insertAfter, insertBefore 
functions?


Re: alias Error: need 'this'

2023-03-19 Thread Salih Dincer via Digitalmars-d-learn

On Sunday, 19 March 2023 at 11:52:50 UTC, bomat wrote:
It works fine with the `int` variable, but with the struct 
member I get a compilation error:

```
Error: need `this` for `memberWithALongName` of type `int`
```

What is that supposed to mean?


It is possible to achieve the convenience you want to achieve in 
2 ways. One of them is to use a static member but if not, to use 
an alias inside the container. For example:


```d
struct MyStruct
{
  int memberWithALongName;
  alias ln = memberWithALongName;

  static string str;
}

void main()
{
  auto myStruct = MyStruct(1);
   myStruct.ln = 2;
  alias alias2 =  MyStruct.str;
  alias2 = "2";

  import std.conv : text;
  assert(myStruct.ln.text == alias2);
}
```
SDB@79


alias Error: need 'this'

2023-03-19 Thread bomat via Digitalmars-d-learn

Hi,

I read about aliases today and thought it would be handy for 
shortening repeated access to struct members.

However, I'm clearly missing something.
Here's some example code:

```
int variableWithALongName = 42;
alias alias1 = variableWithALongName;
alias1 = 43;
assert(variableWithALongName == 43);

struct MyStruct
{
int memberWithALongName;
}
MyStruct myStruct;
myStruct.memberWithALongName = 42;
alias alias2 = myStruct.memberWithALongName;
alias2 = 43; // does not compile, see below
assert(myStruct.memberWithALongName == 43);
```

It works fine with the `int` variable, but with the struct member 
I get a compilation error:

```
Error: need `this` for `memberWithALongName` of type `int`
```

What is that supposed to mean?

Thanks
bomat


Re: Difference between using `import` with/without the colon

2023-03-19 Thread Basile B. via Digitalmars-d-learn

On Sunday, 19 March 2023 at 07:20:17 UTC, Jeremy wrote:
Hello, is there any difference at all between the following 
lines, as an example:


```d
import std.regex;
import std.regex : matchFirst;
```

What technical differences does it make (except for having the 
identifier available), using the colon?
Does it make any speed/optimization changes or am I better off 
just importing the whole module?


The colon-form, aka "selective import" has for effect

1. to create a local alias so this can indeed speedup symbol 
lookups in the sense that search will succeed before looking in 
the scope of the imports.
2. to make non-selected symbols, i.e not listed in the colon 
right hand side, in the import not available.


Note that using both makes no sense, but I guess you did that to 
express more clearly what you meant.


Difference between using `import` with/without the colon

2023-03-19 Thread Jeremy via Digitalmars-d-learn
Hello, is there any difference at all between the following 
lines, as an example:


```d
import std.regex;
import std.regex : matchFirst;
```

What technical differences does it make (except for having the 
identifier available), using the colon?
Does it make any speed/optimization changes or am I better off 
just importing the whole module?


std.net.curl.HTTP: can't download more than 3.6K

2023-03-19 Thread Jeremy via Digitalmars-d-learn
Hello, I'm new to D and the `std.net.curl` library. I'm using the 
`HTTP` struct because I need to set headers for an API, and most 
of the time, I can only download 3.6 kilobytes at a time. Would 
this be a problem with the library not following redirects or 
something else? I'm not really sure what it could be, so could 
someone help me with this?