Passing a single tuple or multiple values

2016-07-18 Thread jmh530 via Digitalmars-d-learn
In the code below, is it possible to combine both bar functions 
into one function (I just put some random foo function to get it 
to work)?


When I try to do it, I get errors that
no property 'expand' for type '(Tuple!(int, int))'
I think it has something to do with T... being considered a 
tuple. I also can't seem to do something using T.length because 
it is two regardless.


import std.typecons : isTuple, tuple;
import std.stdio : writeln;

auto foo(T...)(T x)
{
T[0] y;
foreach (i, e; x)
{
y += e;
}
return y;
}

auto bar(T)(T x)
{
static if (isTuple!T)
{
return foo(x.expand);
}
}

auto bar(T...)(T x)
{
return foo(x);
}

void main()
{
auto x = tuple(1, 2);
auto y = bar(x);
auto z = bar(x.expand);
writeln(y);
writeln(z);
}


Re: Variadic Tuple of Structs with Mixed Types

2016-07-18 Thread jmh530 via Digitalmars-d-learn

On Friday, 15 July 2016 at 19:24:12 UTC, jmh530 wrote:


Have you considered recursive solutions?



Will try that next. Thanks.


I think this worked for me.

A few tricks were that I had to have the fillAliasSeq template as 
global and also I couldn't disable the default constructor for 
what I was filling with it.


Thanks for the help.


Re: Bug? somearrayofclassinstances.filter(...).array fails because of .init() method in class

2016-07-18 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/15/16 7:44 AM, ag0aep6g wrote:

On 07/15/2016 12:54 PM, dom wrote:

i just had a scenario like the one below. when calling .array on the
filterresult dmd goes nuts because of the init() function in Players.
Renaming to initialize() solved the problem.

Solution: As .init is used for struct initialization and such
(https://dlang.org/spec/property.html#init) i think it should be a
restricted keyword for class members and methods


Known issue.

https://issues.dlang.org/show_bug.cgi?id=14237
https://issues.dlang.org/show_bug.cgi?id=7066

Of course, being known doesn't mean that anyone is actively working on a
fix. Feel free to make (a little) noise on Bugzilla, or in the General
group.


This is being worked on.

The issue is that TypeInfo.init is used by druntime to mean the init 
data to store for a type. So until that has been deprecated for a long 
enough time (I don't know the current status, but we are in the process 
of naming it initializer instead), then we can't fix this issue.


-Steve


Re: Iterate all visible symbols, even from imported modules

2016-07-18 Thread Lodovico Giaretta via Digitalmars-d-learn

On Monday, 18 July 2016 at 21:12:38 UTC, Meta wrote:
On Monday, 18 July 2016 at 13:00:16 UTC, Lodovico Giaretta 
wrote:
As per title, is it possible to iterate all visible symbols of 
the current module and of all imported modules and packages? 
My aim is to find everything in scope that has a specific UDA.


module foo;

import std.stdio, std.array, std.algorithm;

void bar(){}

struct S{}

void main()
{
// prints ["object", "std", "bar", "S", "main"]
// how do I discover that "std" is a package?
writeln([__traits(allMembers, foo)]);


// prints ["object", "core", "std", "KeepTerminator", 
"GCC_IO", ... ]
// strange thing: it looks the same even if I remove all 
imports other than std.stdio

writeln([__traits(allMembers, foo.std)]);
}

Thank you in advance.


This answer to a similar question on StackOverflow may be 
useful:


http://stackoverflow.com/questions/2329/d-finding-all-functions-with-certain-attribute/25560800#25560800


Wow!
Looks exactly what I was looking for. I'll give this a try as 
soon as possible.

Thank you.


Re: Iterate all visible symbols, even from imported modules

2016-07-18 Thread Lodovico Giaretta via Digitalmars-d-learn

On Monday, 18 July 2016 at 18:21:41 UTC, ketmar wrote:

short answer: no.

there is still no way to write a reliable enumerator like this: 
too much things to hack around.


as for module symbols, it is easy: they has no type. literally: 
`!is(typeof(...))`.


`is(typeof(...))` is a necessary safeguard anyway if you are 
enumerating symbols in module, as you can't do much with module 
names anyway, and you *have* to filter 'em out with top-level 
static if.


Thank you.
It looks like the check `is(typeof(T)) || is(T)` is passed by 
every symbol `T` that is not a module nor a package, so I think 
I'll use its complementary as a filter.


Re: Iterate all visible symbols, even from imported modules

2016-07-18 Thread Meta via Digitalmars-d-learn

On Monday, 18 July 2016 at 13:00:16 UTC, Lodovico Giaretta wrote:
As per title, is it possible to iterate all visible symbols of 
the current module and of all imported modules and packages? My 
aim is to find everything in scope that has a specific UDA.


module foo;

import std.stdio, std.array, std.algorithm;

void bar(){}

struct S{}

void main()
{
// prints ["object", "std", "bar", "S", "main"]
// how do I discover that "std" is a package?
writeln([__traits(allMembers, foo)]);


// prints ["object", "core", "std", "KeepTerminator", 
"GCC_IO", ... ]
// strange thing: it looks the same even if I remove all 
imports other than std.stdio

writeln([__traits(allMembers, foo.std)]);
}

Thank you in advance.


This answer to a similar question on StackOverflow may be useful:

http://stackoverflow.com/questions/2329/d-finding-all-functions-with-certain-attribute/25560800#25560800


Re: Iterate all visible symbols, even from imported modules

2016-07-18 Thread BLM768 via Digitalmars-d-learn

On Monday, 18 July 2016 at 13:00:16 UTC, Lodovico Giaretta wrote:

// how do I discover that "std" is a package?


I've got a DMD pull request that adds __traits(isPackage, 
someSymbol), but it's stuck waiting for approval. If and when it 
gets merged, it could be useful for that.


https://github.com/dlang/dmd/pull/5290


Re: Convert delegate or function type to other.

2016-07-18 Thread John via Digitalmars-d-learn

On Monday, 18 July 2016 at 18:49:22 UTC, Rufus Smith wrote:

Suppose I have the following: alias func = void function(int);

Is there a way to convert it automatically to something the 
same type except of delegate: alias del = toDel(func) = void 
delegate(int);?


import std.traits;
alias del = ReturnType!func delegate(Parameters!func);


Re: Convert delegate or function type to other.

2016-07-18 Thread Rufus Smith via Digitalmars-d-learn

On Monday, 18 July 2016 at 18:51:29 UTC, Jacob Carlborg wrote:

On 2016-07-18 20:49, Rufus Smith wrote:

Suppose I have the following: alias func = void function(int);

Is there a way to convert it automatically to something the 
same type
except of delegate: alias del = toDel(func) = void 
delegate(int);?


https://dlang.org/phobos/std_functional.html#toDelegate


No, that converts an actual function. I need to create a new 
alias from the old one.


I'd also like to be able to create a delegate with a different 
context pointer.


Convert delegate or function type to other.

2016-07-18 Thread Rufus Smith via Digitalmars-d-learn

Suppose I have the following: alias func = void function(int);

Is there a way to convert it automatically to something the same 
type except of delegate: alias del = toDel(func) = void 
delegate(int);?





Re: Convert delegate or function type to other.

2016-07-18 Thread Jacob Carlborg via Digitalmars-d-learn

On 2016-07-18 20:49, Rufus Smith wrote:

Suppose I have the following: alias func = void function(int);

Is there a way to convert it automatically to something the same type
except of delegate: alias del = toDel(func) = void delegate(int);?


https://dlang.org/phobos/std_functional.html#toDelegate

--
/Jacob Carlborg


Re: Iterate all visible symbols, even from imported modules

2016-07-18 Thread ketmar via Digitalmars-d-learn

short answer: no.

there is still no way to write a reliable enumerator like this: 
too much things to hack around.


as for module symbols, it is easy: they has no type. literally: 
`!is(typeof(...))`.


`is(typeof(...))` is a necessary safeguard anyway if you are 
enumerating symbols in module, as you can't do much with module 
names anyway, and you *have* to filter 'em out with top-level 
static if.


Re: Is there a way to "see" source code generated by templates after a compile?

2016-07-18 Thread Stefan Koch via Digitalmars-d-learn

On Monday, 18 July 2016 at 17:35:29 UTC, Stefan Koch wrote:


I did it for function templates just now.
It will the instantiated bodys to std-out.


Okay here is the frist really hacky draft patch

In dtemplate.d line 6691 right at the end of semantic3 insert the 
following code :


if (auto a = toAlias2) {
scope hgs = HdrGenState();
scope ob = OutBuffer();
scope ppv = new PrettyPrintVisitor(, );

if (auto fd = a.isFuncDeclaration()) {
printf("FunctionTemplate: %s\n{", toChars);
if (auto fb = fd.fbody)
fb.accept(ppv);
}

printf("%.*s\n}\n", ob.size, cast(char*)ob.data);

}
}

Without warranty of any kind of course :)




Re: Is there a way to "see" source code generated by templates after a compile?

2016-07-18 Thread Stefan Koch via Digitalmars-d-learn

On Sunday, 17 July 2016 at 16:30:38 UTC, Stefan Koch wrote:

On Sunday, 17 July 2016 at 14:54:34 UTC, zabruk70 wrote:

On Sunday, 17 July 2016 at 11:14:39 UTC, Stefan Koch wrote:
If you want to see template expansions you have to wait a 
little longer.


Wow! Is this really possible?! So long time several peoples 
asked this...


I am reasonably sure that I can do it :)



I did it for function templates just now.
It will the instantiated bodys to std-out.



Re: Some asm help for the 'thiscall' calling convention?

2016-07-18 Thread Kagamin via Digitalmars-d-learn

On Saturday, 16 July 2016 at 21:16:29 UTC, Andrew Godfrey wrote:
COM is a model; in practice people pick the parts they need, 
and often still call it "COM".


No need to rename what has a name: 
https://en.wikipedia.org/wiki/Interface-based_programming


Re: Some asm help for the 'thiscall' calling convention?

2016-07-18 Thread Kagamin via Digitalmars-d-learn

On Saturday, 16 July 2016 at 20:31:25 UTC, Adam Sansier wrote:
Yet you are wrong. Regardless what the asio standard does right 
or wrong, it uses COM, correct? Is asio not well established? 
Yes it is. Hence it proves that not all COM is as you think it 
is.


The component must comply with the COM standard to provide a COM 
interface, just using COM somehow somewhere is not enough for it. 
The absolute fact is the COM specification, not my words. If you 
don't like what the COM specification says, argue with it, not 
with me. I suppose ASIO is whatever compiled on the developer's 
machine, he didn't bother about COM.


Re: Is there a way to "see" source code generated by templates after a compile?

2016-07-18 Thread WhatMeWorry via Digitalmars-d-learn

On Monday, 18 July 2016 at 07:37:27 UTC, lobo wrote:

On Sunday, 17 July 2016 at 05:57:52 UTC, WhatMeWorry wrote:



[snip]



Now that I think about, was there even a way to look at c/c++ 
code after the pre-processor step?


Does this do what you want?

cl.exe /E
gcc -E
clang -E
clang-cl /E


i appreciate the reply but you're about 20 years too late :)  
Thankfully, my c/c++ days are long gone.  Long live D.


Re: to auto or not to auto ( in foreach )

2016-07-18 Thread Johan Engelen via Digitalmars-d-learn

On Saturday, 16 July 2016 at 22:05:49 UTC, ketmar wrote:


`foreach (v; rng)` looks like `foreach` is *reusing* *existing* 
*variable*.


+1


Iterate all visible symbols, even from imported modules

2016-07-18 Thread Lodovico Giaretta via Digitalmars-d-learn
As per title, is it possible to iterate all visible symbols of 
the current module and of all imported modules and packages? My 
aim is to find everything in scope that has a specific UDA.


module foo;

import std.stdio, std.array, std.algorithm;

void bar(){}

struct S{}

void main()
{
// prints ["object", "std", "bar", "S", "main"]
// how do I discover that "std" is a package?
writeln([__traits(allMembers, foo)]);


// prints ["object", "core", "std", "KeepTerminator", 
"GCC_IO", ... ]
// strange thing: it looks the same even if I remove all 
imports other than std.stdio

writeln([__traits(allMembers, foo.std)]);
}

Thank you in advance.


Re: How to group similar member functions from different classes?

2016-07-18 Thread Marc Schütz via Digitalmars-d-learn

On Friday, 15 July 2016 at 17:25:23 UTC, cy wrote:

On Monday, 20 June 2016 at 16:39:54 UTC, Marc Schütz wrote:

Untested:


Seems to only work if A and B are both defined in the same file 
as Foos (defeating the purpose). Putting A and B in a.d and b.d 
respectively gives me these errors:


a.d(2): Error: undefined identifier 'Foos'
a.d(2): Error: mixin a.A.Foos!() is not defined
b.d(2): Error: undefined identifier 'Foos'
b.d(2): Error: mixin b.B.Foos!() is not defined

I also tried switching it around, like
// b.d
import foos Foos;
class B {
  mixin Foos;
}

but that of course gives the error:
foos.d(4): Error: undefined identifier 'A'
b.d(3): Error: mixin b.B.Foos!() error instantiating

since you can't do a static if(typeof(this) == A) without 
importing A from a somehow. (you can do else static 
if(typeof(this) == B) without importing B though, since it does 
the branch for A first)


I think a mixin here is just required, because you can't use an 
identifier before it's defined, even at compile time. Honestly, 
I have yet to find a use for mixin templates.


It works if you add forward declarations for the classes:

import a, b;
class A;
class B;
mixin template Foos() {
static if(is(typeof(this) == A))
void foo() { /* implementation for A */ }
static if(is(typeof(this) == B))
void foo() { /* implementation for B */ }
}



Re: Is there a way to "see" source code generated by templates after a compile?

2016-07-18 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 17 July 2016 at 16:30:38 UTC, Stefan Koch wrote:

On Sunday, 17 July 2016 at 14:54:34 UTC, zabruk70 wrote:

On Sunday, 17 July 2016 at 11:14:39 UTC, Stefan Koch wrote:
If you want to see template expansions you have to wait a 
little longer.


Wow! Is this really possible?! So long time several peoples 
asked this...


I am reasonably sure that I can do it :)

However don't expect anything pretty.
The indentation will be long gone ...


Just pipe it through dformat should do a good enough job.


Re: Is there a way to "see" source code generated by templates after a compile?

2016-07-18 Thread lobo via Digitalmars-d-learn

On Sunday, 17 July 2016 at 05:57:52 UTC, WhatMeWorry wrote:



[snip]



Now that I think about, was there even a way to look at c/c++ 
code after the pre-processor step?


Does this do what you want?

cl.exe /E
gcc -E
clang -E
clang-cl /E






Re: build a subpackage in dub?

2016-07-18 Thread drug via Digitalmars-d-learn

16.07.2016 20:26, cy пишет:

Say I have a package called "main" and a sub-package in a
"complicatedexample" directory, and my dub.json in "main" looks sort of
like:

"subPackages": [
  "./complicatedexample/"
],

Let's say I do *not* have ":complicatedexample" in my dependencies for
"main", but "complicatedexample" itself imports from /other/
sub-packages in main, like I dunno, "support" "coolstuff" "thingies" or
whatever.

I can't chdir to complicatedexample and dub -v build, because it says
"Unknown dependency: complicatedexample:support" since it can't know
that it's in a subpackage. But I also can't build ":complicatedexample"
from in the main directory, because "dub build -v :complicatedexample"
gives me "Failed to find a package named main:complicatedexample".

I swear I figured out how to do this before, but I honestly cannot
remember. The example imports a huge C library and compiles stubs and
does all sorts of stupid stuff that the rest of the program doesn't have
anything to do with at all, just to demonstrate the algorithm. But I
don't know how to isolate it. Any ideas?

Just... create a totally separate package, and remember to copy and
paste all the other sub-packages to it, and add all the other
subpackages to every dub.json manually?
If you could list example of your package dub.json it would be better. I 
use subpackages intensively, subpackages depends on other subpackages 
and it works, but I'm not sure I understand your case.


Re: Fast MSB to LSB

2016-07-18 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 18 July 2016 at 04:54:07 UTC, Rufus Smith wrote:
Is there any MSB to LSB and vice versa in phobos? Or some 
tricks with templates that make it fast as possible?


you mean endianness conversions? then yes. see 
std.bitmanip:swapendian and friends.