Re: Relocatable objects and internal pointers

2018-01-20 Thread timotheecour via Digitalmars-d-learn

On Saturday, 30 January 2016 at 03:13:59 UTC, Matt Elkins wrote:

std.typecons.Unique seems to require heap allocation, which 
makes it a far cry from std::unique_ptr.


isn't unique_ptr typically for heap allocation?
eg: 
https://stackoverflow.com/questions/42910711/unique-ptr-heap-and-stack-allocation


NOTE: calypso (ldc fork) should allow internal pointers now, see 
https://github.com/Syniurge/Calypso/issues/70 (cv::Mat.step.p is 
an internal pointer)


Re: Struct initialization syntax

2018-01-20 Thread Azi Hassan via Digitalmars-d-learn

On Thursday, 18 January 2018 at 03:50:15 UTC, arturg wrote:

On Wednesday, 17 January 2018 at 17:37:07 UTC, H. S. Teoh wrote:
On Wed, Jan 17, 2018 at 05:31:03PM +, Azi Hassan via 
Digitalmars-d-learn wrote:
The D tour for structs uses a syntax similar to that of C++ 
in order to initialize a Person struct : Person p(30, 180). 
Is this syntax supported in D ? Running that part of the code 
neither works on the playground nor on my machine (dmd 
v2.076.0).


You're probably looking for this syntax:

auto p = Person(30, 180);


T


looks like a bug in the 3rd example.


That's what I was wondering about, thanks.


How to manage function's parameter storage class?

2018-01-20 Thread Sobaya via Digitalmars-d-learn

I'm using opDispatch for wrapping a function like below.

```
import std.stdio;

void func(ref int x, int y) {
x++;
}

struct B {
// wraps 'func'. I want to implement this function.
template opDispatch(string fn) {
void opDispatch(Args...)(Args args) {
mixin(fn~"(args);"); // func(args);
}
}
}

void main() {
{
// This is good behavior.
int x = 3;
func(x, 1);
writeln(x);   // prints '4'
}
{
// This is bad behavior.
B b;
int x = 3;
b.func(x, 1);
writeln(x);   // prints '3' because x is copied when 
passed to opDispatch.

}

}
```

How can I wrap function whose arguments contain both ref and 
normal like 'func' ?
With normal 'Args', x is not increased because x is copied when 
passed to opDispatch.
If I write 'ref Args' in opDispatch's argument, it fails because 
second parameter is not rvalue.


Re: How to manage function's parameter storage class?

2018-01-20 Thread Simen Kjærås via Digitalmars-d-learn

On Saturday, 20 January 2018 at 14:31:59 UTC, Sobaya wrote:
How can I wrap function whose arguments contain both ref and 
normal like 'func' ?
With normal 'Args', x is not increased because x is copied when 
passed to opDispatch.
If I write 'ref Args' in opDispatch's argument, it fails 
because second parameter is not rvalue.


https://dlang.org/spec/function.html#auto-ref-functions

Simply put, instead of 'ref' use 'auto ref'.

--
  Simen


Re: Using Postgres connection functions

2018-01-20 Thread Joe via Digitalmars-d-learn

On Saturday, 20 January 2018 at 04:54:47 UTC, Adam D. Ruppe wrote:

Same as above. The general pattern is:

C_Type[] name = new C_Type[](requested_size);
// pass as `name.ptr`. This becomes a C_Type*


Thanks, Adam. Perhaps something like this ought to make its way 
into the "D for C Programmers" page.


Re: Cannot initialize associative array

2018-01-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, January 19, 2018 23:39:08 rumbu via Digitalmars-d-learn wrote:
> Thank you Adam, just figured out myself the same solution, but I
> didn't expect to have a static constructor in main.d. I thought
> static constructors are meant to be used in imported modules.
> Thanks again.

There really isn't anything special about whatever module you have main in.
It's just like any other module except that it has main in it. Technically,
you could put main somewhere deep in your module hierarchy. It's just
probably not the best idea from an organizational standpoint.

- Jonathan M Davis



Re: How to manage function's parameter storage class?

2018-01-20 Thread Sobaya via Digitalmars-d-learn

On Saturday, 20 January 2018 at 17:05:40 UTC, Simen Kjærås wrote:

On Saturday, 20 January 2018 at 14:31:59 UTC, Sobaya wrote:
How can I wrap function whose arguments contain both ref and 
normal like 'func' ?
With normal 'Args', x is not increased because x is copied 
when passed to opDispatch.
If I write 'ref Args' in opDispatch's argument, it fails 
because second parameter is not rvalue.


https://dlang.org/spec/function.html#auto-ref-functions

Simply put, instead of 'ref' use 'auto ref'.

--
  Simen


Oh... I missed it...
Thanks! !


Filter a Range Based on a Passed In Variable

2018-01-20 Thread jsako via Digitalmars-d-learn
I want to be able to filter a range based on a variable known at 
runtime. Something like this:


[code]
int id = getFilterID();

auto filteredRange = filter!(a => a.id == id)(rangeToBeFiltered);

[/code]

This doesn't seem to be possible, however as .filter only takes 
unary predicates. I tried:


[code]

filterString = "a.id == " ~ to!string(id);
filter!filterString(rangeToBeFiltered);

[/code]

But that also doesn't work. Is there another range algorithm that 
should be used in this case? Do I roll my own?


Re: Filter a Range Based on a Passed In Variable

2018-01-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 20 January 2018 at 19:04:06 UTC, jsako wrote:
I want to be able to filter a range based on a variable known 
at runtime. Something like this:


[code]
int id = getFilterID();

auto filteredRange = filter!(a => a.id == 
id)(rangeToBeFiltered);


[/code]

This doesn't seem to be possible, however as .filter only takes 
unary predicates. I tried:


That should actually work. What exactly happened when you ran 
that literal code above?


Re: Filter a Range Based on a Passed In Variable

2018-01-20 Thread jsako via Digitalmars-d-learn

On Saturday, 20 January 2018 at 19:09:28 UTC, Adam D. Ruppe wrote:

On Saturday, 20 January 2018 at 19:04:06 UTC, jsako wrote:
I want to be able to filter a range based on a variable known 
at runtime. Something like this:


[code]
int id = getFilterID();

auto filteredRange = filter!(a => a.id == 
id)(rangeToBeFiltered);


[/code]

This doesn't seem to be possible, however as .filter only 
takes unary predicates. I tried:


That should actually work. What exactly happened when you ran 
that literal code above?


... Huh. The code I posted was a simplified case of what I 
actually have and like a fool I didn't test it first. You're 
absolutely right, the above code does work. Color me embarrassed.


In the actual code, I kept getting DMD saying "...does not match 
template declaration filter(alias predicate) if 
(is(typeof(unaryFun!predicate)))".


I think it may be a scoping issue? I'll have to look closer at it.

Thanks for the help! Sorry I wasted your time. At least this 
pointed me in the right direction to find out what is really 
going on.