Why typeof(template) is void?

2016-07-19 Thread mogu via Digitalmars-d-learn

```
struct S(T) {}

static assert(is (typeof(S) == void));
```

Why S's type isn't something like `S: (T) -> S`?


Re: Why typeof(template) is void?

2016-07-19 Thread mogu via Digitalmars-d-learn

On Wednesday, 20 July 2016 at 01:50:37 UTC, Adam D. Ruppe wrote:

On Wednesday, 20 July 2016 at 01:14:05 UTC, mogu wrote:

Why S's type isn't something like `S: (T) -> S`?


Because S isn't a type... think of a template as being like a 
function that returns a type.


int foo(int) { return 0; }

There, you wouldn't expect typeof(foo) to be int, no, 
typeof(foo) is a function that returns an int.


The template is the same thing - it isn't a type, it is a 
template that returns a type.


So it's a higher kinded type aka type class in Haskell. But D's 
reflection cannot get enough information about template's kind. 
Only a `void` given. It may be inconvenient in distinction 
between an alias of template and void. The only solution AFAIK is 
by string of the type property .stringof.


Re: Why typeof(template) is void?

2016-07-20 Thread mogu via Digitalmars-d-learn
On Wednesday, 20 July 2016 at 08:01:01 UTC, Lodovico Giaretta 
wrote:

Note that void is a type, while S is not. So you can do:

assert(is(void)) // is(type) returns true
assert(!is(S))   // is(template) returns false;


Thanks very much. I should have noticed this before. T.T


How to specify --arch option in dub.json

2017-02-26 Thread mogu via Digitalmars-d-learn
I use dub 1.2.1 to build my project. As convenience, I choose 
`dflags-windows-x86: ["-m32mscoff"]` option in dub.json of my 
library binding to specify default architecture in win32 
platform. But in the newest dub release, this cause a warning 
which told me to use DLFAGS environment or specify arch in 
command line. Is there any other ways to fix this for I don't 
want users of this library always build with a long command line 
args?


Re: About spinlock implementation

2016-09-01 Thread mogu via Digitalmars-d-learn

On Thursday, 1 September 2016 at 07:46:04 UTC, qznc wrote:


I'm not sure I understand rel [0], but raw is too weak. Raw 
means no sequencing barrier, so


  local_var = protected_value;
  spinlock.unlock();

could be transformed (by compiler or CPU) to

  spinlock.unlock();
  local_var = protected_value;

This effectively makes the access to the protected value 
unprotected and nullifies the effect of the spinlock.


I find the documentation on MemoryOrder lacking about the 
semantics of rel. :(


[0] https://dlang.org/library/core/atomic/memory_order.html


Thanks very much. I finally got it. :)


About spinlock implementation

2016-09-01 Thread mogu via Digitalmars-d-learn

I found an implementation of spinlock in concurrency.d.
```
static shared struct SpinLock
{
void lock() { while (!cas(, false, true)) { 
Thread.yield(); } }
void unlock() { atomicStore!(MemoryOrder.rel)(locked, false); 
}

bool locked;
}
```
Why atomicStore use MemoryOrder.rel instead of MemoryOrder.raw?


Re: About spinlock implementation

2016-09-01 Thread mogu via Digitalmars-d-learn

On Thursday, 1 September 2016 at 07:46:04 UTC, qznc wrote:


This effectively makes the access to the protected value 
unprotected and nullifies the effect of the spinlock.




So the cas operation implicit an MemoryOrder.acq? Does it make 
any other MemoryOrder guarantee?


Fiber cross threads terminated

2016-09-01 Thread mogu via Digitalmars-d-learn

Here's my code in main function:
```
auto fiber = new Fiber({
while (true) {
Thread.sleep(1.seconds);
Fiber.yield;
}
});

void foo() {
while (true) {
fiber.call;
//Thread.sleep(1.seconds);
//"---".writeln;
}
}

new Thread({
foo();
}).start;

new Thread({
Thread.sleep(500.msecs);
foo();
}).start;

```
If I comment the `fiber.call;`, all works.
system: ubuntu 16.04LTS
dmd version: 2.071.1


Re: Fiber cross threads terminated

2016-09-01 Thread mogu via Digitalmars-d-learn
On Friday, 2 September 2016 at 01:53:58 UTC, Steven Schveighoffer 
wrote:

On 9/1/16 9:27 PM, mogu wrote:

Here's my code in main function:
```
auto fiber = new Fiber({
while (true) {
Thread.sleep(1.seconds);
Fiber.yield;
}
});

void foo() {
while (true) {
fiber.call;
//Thread.sleep(1.seconds);
//"---".writeln;
}
}

new Thread({
foo();
}).start;

new Thread({
Thread.sleep(500.msecs);
foo();
}).start;

```
If I comment the `fiber.call;`, all works.
system: ubuntu 16.04LTS
dmd version: 2.071.1


Fibers in D are not meant to be run in multiple threads.

-Steve


Thanks. I got it.


Re: Prevent copy of range in foreach

2016-08-31 Thread mogu via Digitalmars-d-learn

On Tuesday, 30 August 2016 at 19:06:46 UTC, Yuxuan Shui wrote:
Is there a way to use a range defined with disabled post-blit 
in foreach? In other words, is there a way to prevent foreach 
from copying the range?


Should I use move()?

国人?望加群:531010036 谢谢


compile error while use `extern(C++, class)`

2016-08-17 Thread mogu via Digitalmars-d-learn
From spec (Interfacing to C++) 
https://dlang.org/spec/cpp_interface.html:


```
When mapping a D class onto a C++ struct, use extern(C++, struct) 
to avoid linking problems with C++ compilers (notably MSVC) that 
distinguish between C++'s class and struct when mangling. 
Conversely, use extern(C++, class) to map a D struct onto a C++ 
class.

```

But this compiles error. Please help, thanks.


Re: compile error while use `extern(C++, class)`

2016-08-18 Thread mogu via Digitalmars-d-learn
On Thursday, 18 August 2016 at 10:45:14 UTC, Lodovico Giaretta 
wrote:


Which kind of error? An error message by the compiler? One by 
the linker? The compiler crashes?




Compiler Error exactly. The minimal code is(dmd or ldc2 in ubuntu 
16.04 lts):

```
extern (C++, struct)
class A {}
```

Error: identifier expected for C++ namespace found 'struct' when 
expecting ')' declaration expected, not ')'