Re: Function Parameters without Names?

2022-02-19 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 19 February 2022 at 23:37:01 UTC, Vijay Nayar wrote:


What is the main motivator to allow parameters with no names? 
Do they get an automatic implied name like `_` or something?


The problem was about 
[here](https://dlang.org/spec/class.html#constructors), it should 
not be type inference: 
https://dlang.org/spec/function.html#function-attribute-inference


Do you think so? But it works when you open inactive codes and 
convert this(int) to this(T)! The program below throws an error 
about ```_param_0``` when compiling because the inferred type is 
not int.


```d
import std.conv;

class Foo
{
  int i;

  //   v=== type of inferred
  this(int)//(T i)
  {
this.i = i;//.to!int;
  }

  string test()
  {
return i.to!string;
  }
}

void main()
{
  long bar = 42;
  auto foo = new Foo(bar);
  assert(foo.test == "42"); /* Error:

  constructor `source.Foo.this(int _param_0)` is not callable 
using argument types `(long)`
  cannot pass argument `bar` of type `long` to parameter `int 
_param_0`


  */
}
```
The following doesn't work in a function outside the class:
```d
TYPE foo(TYPE) { return 42; }
```
The compiler gives the following error:
```undefined identifier TYPE```

SDB@79


Re: Function Parameters without Names?

2022-02-19 Thread Ali Çehreli via Digitalmars-d-learn

On 2/19/22 15:37, Vijay Nayar wrote:

> The problem is that the parameter of the constructor actually has no
> name at all. Thus, the statement `this.val = val` simply sets the member
> variable to its own value

Wow! I've never come accross that one before. :)

> What is the main motivator to allow parameters with no names?

This is common in C++ as well. You have to have a parameter because an 
interface requires it but you don't really have any use for that 
parameter. If you name it, then there may be "unused variable" warning. 
So, it is common to comment out the parameter name so that it is 
instructive to people who look at the code:


void foo(int /* length */) {
  // ...
}

> Do they get an automatic implied name like `_` or something?

The function is compiled to take the specified type of a parameter but 
it is simply not used. (Names are only for source code so it doesn't 
matter.)


Ali



Function Parameters without Names?

2022-02-19 Thread Vijay Nayar via Digitalmars-d-learn
I encountered an odd bug in a project of mine which is 
illustrated in the example below:


```d
class Thing {
  int val;
  this(int) {
this.val = val;
  }
}

void main()
{
  auto t = new Thing(3);
  assert(t.val != 3);
}
```

The problem is that the parameter of the constructor actually has 
no name at all. Thus, the statement `this.val = val` simply sets 
the member variable to its own value, thus it stays the same as 
`int.init`.


According to the 
[specification](https://dlang.org/spec/function.html), this is 
permissible via the following grammar rules: `FuncDeclaration` => 
`FuncDeclarator` => `FuncDeclaratorSuffix` => `Parameters` => 
`ParameterList` => `Parameter` => `Declarator` => 
`ParameterAttributes_opt Type`.


What is the main motivator to allow parameters with no names? Do 
they get an automatic implied name like `_` or something?


Tips on TCP socket to postgresql middleware

2022-02-19 Thread Chris Piker via Digitalmars-d-learn

Hi D

I'm about to start a small program to whose job is:

1. Connect to a server over a TCP socket
2. Read a packetized real-time data stream
3. Update/insert to a postgresql database as data arrive.

In general it should buffer data in RAM to avoid exerting back 
pressure on the input socket and to allow for dropped connections 
to the PG database.  Since the data stream is at most 1.5 
megabits/sec (not bytes) I can buffer for quite some time before 
running out of space.


So far, I've never written a multi-threaded D program where one 
thread writes a FIFO and the other reads it so I'm reading the 
last few chapters of Ali Cehreli's book as background.  On top of 
that preparation, I'm looking for:

  * general tips on which libraries to examine
  * gotchas you've run into in your multi-threaded (or just 
concurrent) programs,

  * other resources to consult
etc.

Thanks for any advice you want to share.

Best,



curl error msg

2022-02-19 Thread MichaelBi via Digitalmars-d-learn
when running the example in the std.net.curl on my windows, got 
following msg:


std.net.curl.CurlException@std\net\curl.d(4239): Failed to load 
curl, tried "libcurl.dll", "curl.dll"


does it mean i don't have those dll files? thx.