Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Michelle Long via Digitalmars-d-learn

On Wednesday, 12 December 2018 at 16:33:02 UTC, H. S. Teoh wrote:
On Wed, Dec 12, 2018 at 01:35:00PM +, AlCaponeJr via 
Digitalmars-d-learn wrote:

On Tuesday, 11 December 2018 at 21:17:46 UTC, H. S. Teoh wrote:
> Whoa.  That looks like a compiler bug. File a bug here:
> ...

Genuinely asking if this is a case of lacking of unit test for 
the Compiler or this is a case that where is hard to test or 
prevent?

[...]

I think it's just a case of too many possible combinations of 
features to test that some inevitably get overlooked.  
Combinatorial explosion. But once bug reports like these are 
filed and fixed, more unittests will be added, which increase 
the surface area of tested feature combinations and (hopefully) 
reduce the likelihood of similar bugs.





Proper factorization prevents this. It may be a difficult problem 
but all it requires is to think before one leaps. People have 
already developed randomized program testing(essentially 
generates random but working programs(or non-working for error 
codes)).  Someone could setup a machine that continuously 
generates these programs and tests the compiler. Any 
discrepancies are reported and investigated.




Re: Can you move a disabled this struct in to a container type if it's an rvalue?

2018-12-12 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 12 December 2018 at 20:05:18 UTC, aliak wrote:

Ie:

struct S {
@disable this();
this(int i) {}
}

struct Container(T) {
T value;
this(auto ref T value) {
this.value = value;
}
}

void main() {
auto a = Container!S(S(3)); // can't do this.
}


The only error I get when I compile this has to do with incorrect 
use of `auto ref`. If I change the constructor's signature to 
`this()(auto ref T value)`, it works fine.


Can you move a disabled this struct in to a container type if it's an rvalue?

2018-12-12 Thread aliak via Digitalmars-d-learn

Ie:

struct S {
@disable this();
this(int i) {}
}

struct Container(T) {
T value;
this(auto ref T value) {
this.value = value;
}
}

void main() {
auto a = Container!S(S(3)); // can't do this.
}

I can build a custom constructor for Container that makes this 
work:


static auto construct(Args...)(auto ref Args args) {
import std.algorithm: move;
auto value = T(args);
auto opt = Container!T.init;
opt.value = move(value);
return move(opt);
}

But is there a way to do it without adding a custom constructor 
type?


Cheers,
- Ali



Re: how to initialize nested struct elegantly?

2018-12-12 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/12/18 4:49 AM, elvisxzhou wrote:
dlang version has two unnecessary symbols aa and bb, which is ugly 
compare to c99 one.




Well, this works:

BB bb = {
f : 0.2f,
a : [
{i:1010, f:0.1f}, // note the nested field name syntax
{i:1020, f:0.2f}
]
};

writeln(bb);

But you can't use that syntax for expressions, only for initialization.

So, no you can't get rid of the bb if you want to use that syntax.

Or you can just create a factory function to do what you want.

-Steve


Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/12/18 10:22 AM, Kagamin wrote:

On Tuesday, 11 December 2018 at 21:19:59 UTC, Steven Schveighoffer wrote:
If I add an int using a regular declaration or a straight mixin("int 
i0 = 5;"); then the variable shows up.


mixin template genInts()
{
     enum arr = [0];
     static foreach (t; arr)
     {
     mixin("int i0 = 5;");
     }
}

Still prints garbage.


I meant this:

mixin template genInts()
{
   mixin("int i0 = 5;");
}

It's still a static foreach + mixin + mixin template bug, all those are 
needed to make it fail.


What's interesting is that dmd thinks there is an i0 (there is no error 
on a missing symbol), but the ast printed doesn't show a declaration for it.


-Steve


Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Dec 12, 2018 at 01:35:00PM +, AlCaponeJr via Digitalmars-d-learn 
wrote:
> On Tuesday, 11 December 2018 at 21:17:46 UTC, H. S. Teoh wrote:
> > Whoa.  That looks like a compiler bug. File a bug here:
> > ...
> 
> Genuinely asking if this is a case of lacking of unit test for the
> Compiler or this is a case that where is hard to test or prevent?
[...]

I think it's just a case of too many possible combinations of features
to test that some inevitably get overlooked.  Combinatorial explosion.
But once bug reports like these are filed and fixed, more unittests will
be added, which increase the surface area of tested feature combinations
and (hopefully) reduce the likelihood of similar bugs.


T

-- 
Talk is cheap. Whining is actually free. -- Lars Wirzenius


Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 11 December 2018 at 21:19:59 UTC, Steven 
Schveighoffer wrote:
If I add an int using a regular declaration or a straight 
mixin("int i0 = 5;"); then the variable shows up.


mixin template genInts()
{
enum arr = [0];
static foreach (t; arr)
{
mixin("int i0 = 5;");
}
}

Still prints garbage.


Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread AlCaponeJr via Digitalmars-d-learn

On Tuesday, 11 December 2018 at 21:17:46 UTC, H. S. Teoh wrote:

Whoa.  That looks like a compiler bug. File a bug here:
...


Genuinely asking if this is a case of lacking of unit test for 
the Compiler or this is a case that where is hard to test or 
prevent?


Al.


Re: how to initialize nested struct elegantly?

2018-12-12 Thread elvisxzhou via Digitalmars-d-learn

On Wednesday, 12 December 2018 at 09:49:58 UTC, elvisxzhou wrote:

import std.stdio;

struct AA
{
int i;
char c;
double d;
string s;
float f;
}

struct BB
{
float f;
AA[2] a;
}

void print(BB* bb)
{
writefln("bb.a[0].i=%d bb.f=%f", bb.a[0].i, bb.f);
}

void main()
{
//dlang
AA aa = {
i:1010,
f:0.1f
};

BB bb = {
f : 0.2f,
a : [
AA(1010, 0,0,null,0.1f),
AA(1020,0,0,null,0.2f)
]
};

print(&bb);

//
/*

//C99
print( &(BB){
.f = 0.2f,
.a = {
[0] = { .i = 1010, .f = 0.1f }
[1] = { .i = 1020, .f = 0.2f }
}
});

*/
}


dlang version has two unnecessary symbols aa and bb, which is 
ugly compare to c99 one.


aa is already eliminated in the example, but you know where the 
ugly is.


how to initialize nested struct elegantly?

2018-12-12 Thread elvisxzhou via Digitalmars-d-learn

import std.stdio;

struct AA
{
int i;
char c;
double d;
string s;
float f;
}

struct BB
{
float f;
AA[2] a;
}

void print(BB* bb)
{
writefln("bb.a[0].i=%d bb.f=%f", bb.a[0].i, bb.f);
}

void main()
{
//dlang
AA aa = {
i:1010,
f:0.1f
};

BB bb = {
f : 0.2f,
a : [
AA(1010, 0,0,null,0.1f),
AA(1020,0,0,null,0.2f)
]
};

print(&bb);

//
/*

//C99
print( &(BB){
.f = 0.2f,
.a = {
[0] = { .i = 1010, .f = 0.1f }
[1] = { .i = 1020, .f = 0.2f }
}
});

*/
}


dlang version has two unnecessary symbols aa and bb, which is 
ugly compare to c99 one.




Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Johannes Loher via Digitalmars-d-learn
On Tuesday, 11 December 2018 at 21:09:55 UTC, Johannes Riecken 
wrote:

Code:

import std.conv;
import std.stdio;

mixin template genInts()
{
  enum arr = [0,1];
  static foreach (t; arr) {
mixin("int i" ~ to!string(t) ~ " = 5;");
  }
}

void main() {
  mixin genInts!();
  writeln(i0);
  writeln(i1);
}


Expected output:
5
5

Actual output is two garbage integer values.


Definitely a compiler bug.

According to 
https://run.dlang.io/gist/1e6e7574a8762d7fed5a3aa22390493d?compiler=dreg

Garbage values are printed since 2.078.1.

compiling with ldc results in a segmentation fault when running 
the program: 
https://run.dlang.io/gist/02780bfe33a2bfd000aa5718a3fe6505?compiler=ldc


Re: How to get the name of member function through an alias of that function

2018-12-12 Thread Peter Particle via Digitalmars-d-learn
On Wednesday, 12 December 2018 at 08:36:00 UTC, Simen Kjærås 
wrote:
On Wednesday, 12 December 2018 at 08:16:03 UTC, Peter Particle 
wrote:
I prefer that all the members marked with myUDA are process in 
the order thy appear in Foo. This happens automatically when I 
iterate with through 'getMemberByUDA'.


Just replace fullyQualifiedName!member with 
__traits(identifier, member), and things should work.


--
  Simen


How could I have missed that one! THX!


Re: How to get the name of member function through an alias of that function

2018-12-12 Thread Simen Kjærås via Digitalmars-d-learn
On Wednesday, 12 December 2018 at 08:16:03 UTC, Peter Particle 
wrote:
I prefer that all the members marked with myUDA are process in 
the order thy appear in Foo. This happens automatically when I 
iterate with through 'getMemberByUDA'.


Just replace fullyQualifiedName!member with __traits(identifier, 
member), and things should work.


--
  Simen


How to get the name of member function through an alias of that function

2018-12-12 Thread Peter Particle via Digitalmars-d-learn
I prefer that all the members marked with myUDA are process in 
the order thy appear in Foo. This happens automatically when I 
iterate with through 'getMemberByUDA'.


// Reduced example code
enum myUDA;
struct Foo {
public:
  @myUDA void  bar(float f) { baz = 1 / f; }
  @myUDA float bar() { return 1 / baz; }
  float baz;
}

import std.traits;
string getMemberFuncName(T)(ref T foo) {
  static foreach(member; getSymbolsByUDA!(T, myUDA)) {
static if(isSomeFunction!member) {

  // Error: function `Test.Foo.bar(float f)` is not callable 
using argument types `()`

  // pragma(msg, member.stringof);

  // Don't want the fully qualified name, just the func name
  pragma(msg, fullyQualifiedName!member);

  // return the name of the first found member function
  //return name;
}
  }
  return "";
}

void main() {
  Foo foo;
  string name = getMemberFuncName(foo);
}