Re: Type sniffing at runtime

2020-05-16 Thread Alex via Digitalmars-d-learn

On Saturday, 16 May 2020 at 05:22:49 UTC, n0den1te wrote:

[...]


For example, like this:

´´´
import std;

alias types = AliasSeq!(
bool, byte, ubyte, short, ushort, int, uint, long, ulong, 
float, double, real, char, wchar, dchar

);

void main()
{
static foreach(type; types)
{
sniffType!type;
}
}


void sniffType(T)() {
writeln("Type   : ", T.stringof);
writeln("Length in bytes: ", T.sizeof);
static if(__traits(compiles, T.min))
writeln("Minimum value  : ", T.min);
writeln("Maximum value  : ", T.max);
writeln("Initial value  : ", T.init);
}

´´´

However, for the char, wchar and dchar types the results of min, 
max and init are somewhat cumbersome. And T.min does not compile 
for the floating point types.





Re: Option and Result [was Integration tests]

2020-04-21 Thread Alex via Digitalmars-d-learn

On Tuesday, 21 April 2020 at 16:30:15 UTC, Russel Winder wrote:
On Mon, 2020-04-20 at 20:19 +, aliak via 
Digitalmars-d-learn wrote:


[…]



[0]: https://github.com/aliak00/optional


Rust has Option and Result, and most languages are rapidly 
introducing at least Option if not Result – and yes it is 
almost certain all this comes from Haskell.


Is Option intended for adding to Phobos?


There is Nullable in Phobos. This is not really an Option, I 
know. But just in case.


But back to integration tests, there are indeed only few 
possibilities to mock something. However, the basics are there.

I found a section of code generation a while ago in std.typecons.
https://dlang.org/library/std/typecons.html

There are Black and White holes there, for example, which could 
help to implement a basic mocking framework.


Re: need help to get member function const address

2020-03-19 Thread Alex via Digitalmars-d-learn

On Thursday, 19 March 2020 at 04:30:32 UTC, Calvin P wrote:

I use this code to get member function address on runtime:

=
struct A {
   this(){};
}
auto ctor = (&__traits(getMember, A.init,"__ctor")).funcptr;
=


my question is, how to get it in compile time like static 
function address:


=
struct A {
 void d(){};
 static void fn(){};
}

enum FN =   // static method address is ok
enum A0 = &(A.d).funcptr; // Error: need this for d of type 
void()
enum A1 = (&__traits(getMember, A,"d")).funcptr; // Error: no 
property funcptr for type void function()
enum A2 = (&__traits(getMember, A.init,"d")).funcptr; //  
Error: (().d).funcptr cannot be evaluated at compile time

=


A non-static member method can use the context of the struct 
where it is defined in. E.g. it could alter a member variable.
This context has to be constructed at run time (and there could 
be many instances of the context) and does not exist in compile 
time. Therefore the difference to the static method.


Re: What type does byGrapheme() return?

2020-01-06 Thread Alex via Digitalmars-d-learn

On Monday, 6 January 2020 at 08:39:19 UTC, Robert M. Münch wrote:

On 2020-01-05 04:18:34 +, H. S. Teoh said:

At a minimum, I think we should file a bug report to 
investigate whether
Grapheme.opSlice can be implemented differently, such that we 
avoid this
obscure referential behaviour that makes it hard to work with 
in complex
code. I'm not sure if this is possible, but IMO we should at 
least

investigate the possibilities.


Done... my first bug report :-) I copied togehter all the 
findings from this thread.


For the sake of completeness
https://issues.dlang.org/show_bug.cgi?id=20483


Re: Multi-threaded sorting of text file

2020-01-04 Thread Alex via Digitalmars-d-learn

On Saturday, 4 January 2020 at 07:51:49 UTC, MGW wrote:

Need help:
There' s a large text file (hundreds of thousands of lines).
The structure is as follows:
2345|wedwededwedwedwe ..
872625|rfrferwewweww .
23|rergrferfefer 


It is necessary to sort this file by the first field having 
received:

23|rergrferfefer...
2345|wedwededwedwedwe...
872625|rfrferwewweww...

There are also N CPU (from 4 to 8) and 16 Gb of Memory. 
Necessary
come up with an algorithm in D for fast sorting using 
multithreading.


As far as I know, there isn't any native in D. Maybe I overlooked 
some at code.dlang.org. But there are plenty out there in the 
wild. Found this on the first shoot:

https://stackoverflow.com/questions/23531625/multithreaded-sorting-application/23532317


Re: array of functions/delegates

2019-12-24 Thread Alex via Digitalmars-d-learn

On Tuesday, 24 December 2019 at 07:37:02 UTC, Rumbu wrote:

I am trying to create an array of functions inside a struct.

struct S {
  void f1() {}
  void f2() {}

  alias Func = void function();

 immutable Func[2] = [, ]

}

What I got: Error: non-constant expression ''

Tried also with delegates (since I am in a struct context but I 
got: no `this` to create delegate `f1`.


So, is there any way to have an array of functions without 
adding them at runtime?


If you don't need runtime, probably like this:

´´´
import std;

void f1(S s) {assert(1);}
void f2(S s) {assert(1);}
alias field = AliasSeq!(f1, f2);

struct S{}

void main()
{
S s;
field[0](s);
}
´´´

Don't know why UCFS doesn't work in this case though.

Maybe, this is also helpful:
https://forum.dlang.org/post/mailman.2415.1354291433.5162.digitalmars-d-le...@puremagic.com


Re: Intersection of two sets

2019-12-03 Thread Alex via Digitalmars-d-learn

On Tuesday, 3 December 2019 at 13:43:26 UTC, Jan Hönig wrote:

It seems i don't google the right keywords.

What i want to do: I have two sets. (I didn't find how to do 
sets, so i have two associative boolean arrays 
`bool[]`). And i want to join them, via an 
intersection.


I know how to code this. Loop over one AA, if the key is also 
in the other one, we add that to the third array which is going 
to be returned.


pseudocode:
alias set = bool[]
set foo = ...
set bar = ...
set result;

foreach(key; foo)
{
  if (key in bar)
  {
result[key] = true;
  }
}
return result;


1) Is there a better way for creating a set, other then `alias 
set = bool[keyClass]`?


This depends on the available accesses on your sets. In terms of 
ranges:

Are your sets InputRanges, ForwardRange, ... ?



2) Are there some build-in function for handling such sets?


This is maybe what you are looking for: 
https://dlang.org/phobos/std_algorithm_setops.html




Re: Simple casting?

2019-11-25 Thread Alex via Digitalmars-d-learn
On Tuesday, 26 November 2019 at 05:17:54 UTC, Taylor R Hillegeist 
wrote:
On Tuesday, 26 November 2019 at 05:05:48 UTC, Taylor R 
Hillegeist wrote:

I'm attempting to do a segment group.

details:
alias ProbePoint[3]=triple;
triple[] irqSortedSet = UniqueTriples.keys
.sort!("a[1].irqid < 
b[1].irqid",SwapStrategy.stable)
.array; 
83:triple[][] irqSortedSets = irqSortedSet.chunkBy!((a,b) => 
a[1].irqid == b[1].irqid);	



GetAllTriplesExtractFileIrqSplit.d(83): Error: cannot 
implicitly convert expression `chunkBy(irqSortedSet)` of type 
`ChunkByImpl!(__lambda4, ProbePoint[3][])` to 
`ProbePoint[3][][]`


I have something that looks like a triple[][] but I can't seem 
to get that type out.
when I add .array it converts to a Group which doesn't make 
sense to me because I'm not using a unary comparison. Any 
thought?


a simpler example:

import std.algorithm.comparison : equal;
import std.array;
// Grouping by particular attribute of each element:
uint[3][] data = [
[1, 1,0],
[1, 2,0],
[2, 2,0],
[2, 3,0]
];

uint[3][][] r1 = data.chunkBy!((a,b) => a[0] == b[0]);

fails in the same way.


What exactly is the problem, as this works for me if I understood 
your goal correctly:


´´´
void main()
{
import std.algorithm.comparison : equal;
import std.array;
import std;
// Grouping by particular attribute of each element:
uint[3][] data = [
[1, 1,0],
[1, 2,0],
[2, 2,0],
[2, 3,0]
];

auto r1 = data.chunkBy!((a,b) => a[0] == b[0]);
}
´´´

If it is the type of the return value --> the return value of 
chunkBy has a different one compared to the input. Instead, you 
get an abstracted range whereas the input data serves as a source.


Re: Splitting a stream of data on based on data change.

2019-11-21 Thread Alex via Digitalmars-d-learn
On Thursday, 21 November 2019 at 21:36:08 UTC, Taylor R 
Hillegeist wrote:
I was looking through the standard library for a good way to 
split a range into several ranges based on value changes in the 
stream:


AAABB
would be split on the AB transition into:
AAA BB

I just couldn't figure out an elegant way to do it? Any ideas?


Like this?

´´´
void main()
{
import std;
"AAABB"
.chunkBy!((a,b) => a == b)
.writeln;
}
´´´

https://dlang.org/library/std/algorithm/iteration/group.html
https://dlang.org/library/std/algorithm/iteration/chunk_by.html


Re: Unable to pass a D function member to a C callback

2019-11-02 Thread Alex via Digitalmars-d-learn

On Saturday, 2 November 2019 at 17:49:09 UTC, Luh wrote:

Hello,

When trying to pass a D function to the C callback, the 
compiler says:


'Error: cannot implicitly convert expression  
of type extern (C) bool delegate(const(short*) a, ulong b, 
void* c) to extern (C) bool function(const(short*), ulong, 
void*'


because my function is member of a class (compiles when the 
function is out of the class).


Is there any way to say to solve this ?
The wiki isn't very clear about the C callbacks:
https://dlang.org/spec/interfaceToC.html#callbacks

C code:

typedef bool (*onProcessCallback)(const short*, size_t, void*);



D Code:
-
class Game
{
onProcessCallback m_onProcessCb;

this()
{
m_onProcessCb =  // Error here
}

void onProcess()
{
// ...
}

extern(C) bool onProcessCb(const short* a, size_t b, void* c)
{
onProcess();
return true;
}
}

private extern(C)
{
// Should call onProcess() when executed by the C lib
	alias onProcessCallback = bool function(const short*, size_t, 
void*);

}
-


This is because onProcessCb is a member of an object. Therefore, 
it carries also the information about the context, which includes 
e.g. all members of the class.


Due to this onProcessCb is a delegate, which is something 
different from a function, cf.

https://dlang.org/spec/function.html#closures

So, there is a type mismatch.



Re: Dynamic Arrays as Stack and/or Queue

2019-10-07 Thread Alex via Digitalmars-d-learn

On Monday, 7 October 2019 at 19:38:50 UTC, mipri wrote:

On Monday, 7 October 2019 at 19:16:31 UTC, IGotD- wrote:
On Monday, 7 October 2019 at 17:36:09 UTC, Ferhat Kurtulmuş 
wrote:


I'm not talking about memory deletion. I'm talking about 
push, pop, enqueue, and dequeue behavior. I'd assume in a 
garbage collected language letting the reference float off 
should be picked up by the GC.
I'm sorry. Writing on my mobile phone. Maybe this is what you 
are looking for

https://dlang.org/phobos/std_container_dlist.html


I think what he is looking for are the general pop_front, 
push_front, pop_back and push_back that you would find in 
virtually any C++ STL container algorithms like list, vector 
or map.


I think this is a good question as I don't really see any good 
example in the documentation of the dynamic arrays about this. 
This is very common use case for arrays. Is there any D 
equivalent?


With the performance that you'd expect, I believe:

#! /usr/bin/env rdmd
[...]


I assume this is a little bit more complicated:
while popFront and popBack are ubiquitous in ranges in D, the 
push functions can't be unified in terms of memory management. 
For example, you cannot guarantee any performance, while using 
GC. This opposed to complexity, which is, of course, known.


Maybe, this is the reason, why such containers are omitted in 
Phobos (?)


But:
There is a priority queue, named binaryheap:
https://dlang.org/phobos/std_container_binaryheap.html

and there are hints, that for a stack you have to rely e.g., on 
an array or a list implementation

http://www.cplusplus.com/reference/stack/stack/
both exist in phobos:
https://dlang.org/phobos/std_container_array.html
https://dlang.org/phobos/std_container_dlist.html

This gives rise to user implementations like
https://code.dlang.org/packages/queue
https://code.dlang.org/packages/mergearray
etc.

Maybe, there will be more soon? ;)



Re: how to determine if a function exists in a class?

2019-10-02 Thread Alex via Digitalmars-d-learn

On Wednesday, 2 October 2019 at 06:06:20 UTC, TodNaz wrote:

Hello everyone!
I have a question: how to determine if a function exists in a 
class? Is this possible with @pointer tagging?


Do you mean, like in examples of

https://dlang.org/library/std/traits/has_member.html

?


Re: Looking for a Simple Doubly Linked List Implementation

2019-09-21 Thread Alex via Digitalmars-d-learn

On Friday, 20 September 2019 at 20:26:03 UTC, Ron Tarrant wrote:

Hi guys,

I've been banging my head on the screen with this one for the 
last week or so. For whatever reason, I'm having major problems 
understanding how to implement a doubly-linked list in D. I 
don't know if it's because I'm losing my ability to sort these 
things or if it's just that different from C.


If someone could please post a minimal example (if there's 
extra stuff in there, I'll get confused; I'm getting that old, 
dammit) I'd be ever so grateful.


rosetta code is quite good for such problems

http://rosettacode.org/wiki/Doubly-linked_list/Definition#D


Re: need this for name of type string

2019-09-10 Thread Alex via Digitalmars-d-learn

On Tuesday, 10 September 2019 at 10:32:29 UTC, Andre Pany wrote:

Hi,

following coding is throwing compiler error:
  need this for name of type string

The error disappears if I delete method0.
My gut feeling is, this is a compiler bug?

---
class C
{
static this()
{
getT!(typeof(this))();
}

@Foo void method0(){}

@Foo("abc") void method1(){}
}

struct Foo
{
string name;
}

void getT(T)()
{
import std.traits: hasUDA, getUDAs;

static foreach(fieldName; __traits(allMembers, T))
{
static if (hasUDA!(__traits(getMember, T, fieldName), 
Foo))

{
pragma(msg, getUDAs!(__traits(getMember, T, 
fieldName), Foo)[0].name);

}
}
}

void main(){}
---

Kind regards
André


Don't think so. In case of @Foo, you don't instantiate an object. 
Therefore, name cannot exist. So... in this case, the UDA is a 
type, not an object you can query for a name.
It's more the like the example with SimpleAttr on the help page 
[1], I think.


[1] https://dlang.org/library/std/traits/get_ud_as.html


Re: What the abstrac final class mean?

2019-08-12 Thread Alex via Digitalmars-d-learn

On Monday, 12 August 2019 at 08:54:56 UTC, lili wrote:

Hi:
Why need defined an abstract final class?
see  
https://github.com/Rikarin/Trinix/blob/master/Kernel/arch/amd64/gdt.d


From what I saw, all members are static. So, this is a kind of 
utility class, which is not supposed to be instantiated, nor to 
be derived from. Maybe, it serves like a namespace, for 
convenience...


Re: Abstract classes vs interfaces, casting from void*

2019-08-11 Thread Alex via Digitalmars-d-learn

On Sunday, 11 August 2019 at 20:32:14 UTC, John Colvin wrote:

As I see this, everything you wrote is correct. :)

But you compared abstractness with interface usage, initially. 
So... I would say, interfaces are more like the abstract 
method case without any function body. But then, you will have 
to use "override" all across the inherited classes.


Ok. So that means the difference is pretty subtle, give or take 
a few extra keywords.


Which leaves multiple inheritance as the only significant 
difference?


From my perspective it looks like there are two massively 
overlapping features with some quite arbitrary feeling 
restrictions and differences. E.g. why can I not inherit from 
multiple 100% abstract empty classes? Wouldn't that be the same 
as inheriting from multiple interfaces?


The overlap is there, but it is not so massive, I would say. If 
you inherit from abstract classes, then you do not plan to keep 
them empty.
So, the overlap you are speaking about is exactly as large as the 
amount of "100% abstract empty classes". And for these, the 
approach to keep the interface as a separate interface seems more 
convenient, as Adam said.
In the end, by forcing an explicit override, some semantic is 
also implied.


Re: Abstract classes vs interfaces, casting from void*

2019-08-11 Thread Alex via Digitalmars-d-learn

On Sunday, 11 August 2019 at 16:05:20 UTC, John Colvin wrote:
I'm trying to narrow down exactly what patterns work with each 
and how they overlap.


What I was trying to get at with the abstract method thing is 
that


abstract class C
{
void foo();
}

is an abstract class with a non-abstract method, whose 
implementation is going to come from somewhere else (not a 
common pattern in D).


class C
{
abstract void foo();
}

is an abstract class with an abstract method foo, which means 
you have to override it in a inheriting class to get a 
non-abstract class.


As I see this, everything you wrote is correct. :)

But you compared abstractness with interface usage, initially. 
So... I would say, interfaces are more like the abstract method 
case without any function body. But then, you will have to use 
"override" all across the inherited classes.


Re: Abstract classes vs interfaces, casting from void*

2019-08-11 Thread Alex via Digitalmars-d-learn

On Sunday, 11 August 2019 at 13:09:43 UTC, John Colvin wrote:
Ok. What would go wrong (in D) if I just replaced every 
interface with an abstract class?


I think there's some confusion here, because B.foo is not 
abstract. abstract on a class is not inherited by its methods. 
https://dlang.org/spec/attribute.html#abstract


Now, I'm confused, as you asked about abstract classes. So, yes, 
you can define the abstractness of classes differently. And what 
is your point?


Re: Passing nested template function

2019-08-10 Thread Alex via Digitalmars-d-learn

On Saturday, 10 August 2019 at 17:45:43 UTC, Prateek Nayak wrote:
A nested function can be passed to another function evident 
from this example: https://run.dlang.io/is/6waRkB


However if the nested function is a template function, it 
raises an error

https://run.dlang.io/is/PQhkwl
The error being: cannot get frame pointer to the nested function

Is there a way to pass a nested template function to a function 
declared outside the outer function scope?


The part I'm not sure about:
This is not possible: Before instantiation, the object does not 
exist, you cannot, therefore, pass its pointer, not to speak 
about the pointer of its frame.


However, how about this:
https://run.dlang.io/is/8drZRf


Re: Abstract classes vs interfaces, casting from void*

2019-08-10 Thread Alex via Digitalmars-d-learn

On Saturday, 10 August 2019 at 14:29:03 UTC, John Colvin wrote:

On Saturday, 10 August 2019 at 10:11:15 UTC, Alex wrote:

On Saturday, 10 August 2019 at 08:20:46 UTC, John Colvin wrote:

On Friday, 9 August 2019 at 13:39:53 UTC, Simen Kjærås wrote:




Thanks for the extra detail.

Is there a solid reason to ever use an interface over an 
abstract class? (Other than multiple inheritance).


I'm such a noob at anything related to OO.


The general question is tricky, as different languages differ 
in details what is forced and what is allowed for abstract 
classes and interfaces.


But roughly speaking, my opinion is: if you can/want to 
provide some default behavior than you are about to write an 
abstract class.
If you are about to provide information/restriction of 
behavior, then this is more like an interface.


Ok. What would go wrong (in D) if I just replaced every 
interface with an abstract class?


´´´
void main(){}
interface A { void fun(); }
abstract class B{ void fun(); }
class C : A{ void fun(){} }
class D : B{ /*override*/ void fun(){} }
´´´

case 1:
interface A and class C implementing interface A:
You don't need to "override" anything. You are forced to provide 
an implementation of the function inside the class.


case 2:
abstract class B and class D inheriting from it:
You can but not have to provide an implementation of a function 
inside the abstract class.
If I don't and do not provide any implementation inside D I get a 
linker error. Don't how this case behaves on your system.
If you provide an implementation inside the abstract class, you 
don't have to provide any in the derived one.
In any case, if you want to provide an implementation inside the 
derived class you have to literally "override", as in D implicit 
overrides are not allowed.


Re: Abstract classes vs interfaces, casting from void*

2019-08-10 Thread Alex via Digitalmars-d-learn

On Saturday, 10 August 2019 at 08:20:46 UTC, John Colvin wrote:

On Friday, 9 August 2019 at 13:39:53 UTC, Simen Kjærås wrote:




Thanks for the extra detail.

Is there a solid reason to ever use an interface over an 
abstract class? (Other than multiple inheritance).


I'm such a noob at anything related to OO.


The general question is tricky, as different languages differ in 
details what is forced and what is allowed for abstract classes 
and interfaces.


But roughly speaking, my opinion is: if you can/want to provide 
some default behavior than you are about to write an abstract 
class.
If you are about to provide information/restriction of behavior, 
then this is more like an interface.


Re: Is it possible to execute a function inside .d script that has no main function?

2019-07-11 Thread Alex via Digitalmars-d-learn

On Thursday, 11 July 2019 at 09:43:55 UTC, BoQsc wrote:

Here I have a file named: module.d


import std.stdio   : writeln;

void interestingFunction(){
writeln("Testing");
}


There is no main() function since, I want to import this 
module, into another .d file.


( If I try to import and module.d does have main() function I 
get this error: )
otherFile.d(13): Error: only one main, WinMain, or DllMain 
allowed. Previously found main at module.d(3)






I would like to launch function "interestingFunction()" 
directly using rdmd.

Is it possible to achieve that by any way?





I tried to launch the whole file, but it gave me some weird 
output:

C:\Users\User\Desktop\Environment variables>rdmd module.d
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Warning 134: No Start Address


If you are trying to test a function separately from other 
functions, e.g. for testing, then you are approaching unit 
testing.
You could write a unit test block just under the function to test 
like

unittest
{
  interestingFunction;
}

and launch it via rdmd... don't know the syntax right now. But 
here is something:

https://stackoverflow.com/questions/10694994/rdmd-is-not-running-unit-tests

So, I assume something like
rdmd -unittest --main module.d


Re: For loop with separator

2019-07-04 Thread Alex via Digitalmars-d-learn

On Thursday, 4 July 2019 at 17:00:33 UTC, Q. Schroll wrote:

Probably you've come over this problem once in a while, too.
You have a repeating solution, so you use a for(each) loop.
Sometimes, there is an action to be performed between the end 
of one iteration and the beginning of the next, if there is 
one. The prime example is printing the comma when printing a 
list: There is one between any two elements, but neither is one 
at front or behind the last one.


Typical solutions I employed were:
1 Handling the first element separately
2 Condition in the loop, that is false exactly for the first 
iteration.


1 can be done with ranges easily:

if (!range.empty)
{
action(range.front);
range.popFront;
foreach (element; range)
{
betweenAction();
action(element);
}
}

This approach is clearly quite verbose for the problem, but 
there's nothing done unnecessarily.


2 can be done easily, too:

foreach (i, element; range)
{
if (i > 0) betweenAction();
action(element);
}

While 2 is less code, it's prone to be checked every iteration.
Note that 2 is rather D specific in its length. It can be done 
in other languages, but is more verbose.


Is there a cleaner solution that I missed?


As far as I can interpret it, joiner

https://dlang.org/library/std/algorithm/iteration/joiner.html

uses roughly the first approach.


Re: create and initialise array

2019-06-20 Thread Alex via Digitalmars-d-learn

Thanks Matheus, thats what i needed.
I added a PR to mention this function in the language 
documentation about arrays.




create and initialise array

2019-06-19 Thread Alex via Digitalmars-d-learn

Is there a way of creating and initialising a dynamic array ?

for example I am doing this:

auto arr = new float[];
arr[] = 0.0f;

Profiling indicates that the compiler (gdc) is spending 
significant time memsetting the whole array to something (nan ?) 
before I immediately memset it to 0.0f.


It would be good if there was a way of either void initialing it 
so that the first memset is avoided or a way of replacing the 
init value with a different one.


Thanks,
Alex


Re: Delegate / Error: cannot implicitly convert expression...

2019-06-15 Thread Alex via Digitalmars-d-learn

On Saturday, 15 June 2019 at 16:34:22 UTC, Robert M. Münch wrote:

On 2019-06-15 16:19:23 +, Anonymouse said:

By design, I think: "delegate and function objects cannot be 
mixed. But the standard function std.functional.toDelegate 
converts a function to a delegate."


Your example compiles if the assignment is changed to dg = 
toDelegate(); (given appropriate imports).


https://tour.dlang.org/tour/en/basics/delegates

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


Hmm... but this here compiles:

void main()
{
   import std.stdio: write, writeln, writef, writefln;
   void foo(int a) {return; }

   void test()
   {
void delegate(int) dg;

   dg = 
   }
}

See: https://run.dlang.io/is/U7uhAX

Is it because inside main() there is a stack frame? And with a 
global function there is none? I'm a bit confused...


Correct. Inside main, foo is a delegate.
https://dlang.org/spec/function.html#nested


Re: Elegant way to test if members of array A are present in array B?

2019-06-11 Thread Alex via Digitalmars-d-learn

On Tuesday, 11 June 2019 at 17:12:17 UTC, Robert M. Münch wrote:
Is there a simple and elegant way to do this? Or is just using 
a foreach(...) with canFind() the best way?


There is also find_among, but the performance is the same, I 
assume.

https://dlang.org/library/std/algorithm/searching/find_among.html


Re: How to create a template class using foreach delegate to filter objects in a member function call?

2019-06-01 Thread Alex via Digitalmars-d-learn

On Saturday, 1 June 2019 at 14:24:11 UTC, Robert M. Münch wrote:


The myFilter struct is the implementation which myClass.put() 
should use to iterate over all objects.


Which ones? The E-objects, or the objects contained in myClass, 
which you don't want to know about?



All things being only examples.


Sure. So, please, provide a minimal example, as I'm only guessing 
what use case you want to solve. Even if it is not compilable.


So, the idea is, that I can provide a delegate which 
foreach(...; myFilter(objects)) uses. Like a run-time plug-in.


Maybe you can. But "put" has to remain a function, not a template.



Do you have control about the contained classes? If so, it is 
a hint to implement the testing inside them. Like:


/* probably inside a templated mixin */
bool testfunction(inputs){...}

class myOtherClass(/*probably templated*/){... mixin 
testfunction ... & provide a put function}


I thought about a mixin too. But this requires an understanding 
of how myClass works. Which I what I want to avoid.


Over which objects your filter will be iterating then?

* So, you are not totally against the idea of modifying the 
foreign library, but you want to keep modifications small?


Yes, because myClass is some external lib, which can be changed 
to support this approach here. But not more.


Re: How to create a template class using foreach delegate to filter objects in a member function call?

2019-05-31 Thread Alex via Digitalmars-d-learn

On Friday, 31 May 2019 at 16:24:28 UTC, Robert M. Münch wrote:
The code is just to show the problem and not meant to compile. 
I couldn't get anything to compile...


That's ok, but could you provide an example anyway? Is it like 
this?


´´´
void main(){
auto target = new myClass!int();
target.objects.length = 4;
auto val = 42;
put(target, val, testfunction); // does the test function 
enters here?

put(target, val);
auto f = myFilter!int; // where do you want to use this 
entity?

}
´´´

Said this, I for myself had a similar problem. I solved this 
by reversing the hierarchy: I templated my objects I wanted to 
use the filter on with the filter function and removed the 
need of the template parameter inside the filter.


The thing is, myClass is not under my control. It's coming from 
a library I don't maintain and I don't want to mess around with 
the code or if, as minimalistic as possible. That's why I was 
thinking about providing a put(T)... function.


*



My first idea was to sub-class myClass, but objects is private, 
so no chance to get access to it.


You could still write a general filter function in this case, 
if you want. For example, you could use mixins for this...


Then myClass needs to somehow get the mixin in.

So, to summarize the problem: Given a class that manages an 
array of things as an OutputRange how can I provide a put() 
function with something like a filter-predicate? I only want to 
put() to some of the things, not all.


Do you have control about the contained classes? If so, it is a 
hint to implement the testing inside them. Like:


/* probably inside a templated mixin */
bool testfunction(inputs){...}

class myOtherClass(/*probably templated*/){... mixin testfunction 
... & provide a put function}


* So, you are not totally against the idea of modifying the 
foreign library, but you want to keep modifications small? With 
the approach now, you could, for example, handle compile time 
blocks inside the put function in the myClass and dynamical ones 
inside the myOtherClasses.


class myClass(E){/* inserted put function */ void put(...){static 
if put action is at all possible --> put. }}


Re: How to create a template class using foreach delegate to filter objects in a member function call?

2019-05-31 Thread Alex via Digitalmars-d-learn

On Thursday, 30 May 2019 at 18:34:31 UTC, Robert M. Münch wrote:
I have myClass and I want to add a way where I can provide a 
delegate to iterate over myClass.objects when a member function 
put(...) of myClass is called. The idea is that a user of 
myClass can provide something like an "iterator filter" so that 
the function is only called on a subset of myClass.objects.



myClass(E){
myOtherClass!E objects;

void put(E obj) {
.put(objects, obj);
}

void put(T)(E obj, T filter) {
foreach(o ; filter!E(objects)){
.put(o, obj);
}
}
}

struct myFilter {
myOtherClass!E object;

opApply(int delegate(E) foreach_body) const {
if(mytestfunc(object))
return(foreach_body(object));
}
}

But I'm struggeling how to write all this down with tempaltes, 
because E is not known in myFilter. But the filter code needs 
to be aware of the template type if myClass. I hope the idea 
want to do is understandable.


Not sure, if I understood your problem correctly. It is meant 
that the class myClass defines an array of myOtherClass objects? 
The code does not compile and it does not provide an example, how 
you would apply the pattern, even in a non-compileable way... 
However,


commonly, a filter is a higher order function, which expects a 
predicate acting on each element of a set. Even if it's higher 
order, it is still a function, not a delegate. Therefore, it is 
unexpected, that you want to store something inside the filter.


Said this, I for myself had a similar problem. I solved this by 
reversing the hierarchy:
I templated my objects I wanted to use the filter on with the 
filter function and removed the need of the template parameter 
inside the filter.
You could still write a general filter function in this case, if 
you want. For example, you could use mixins for this...

As I said... not sure if this is of any help for you...


Re: Performance of tables slower than built in?

2019-05-24 Thread Alex via Digitalmars-d-learn

On Friday, 24 May 2019 at 13:57:30 UTC, Ola Fosheim Grøstad wrote:

On Friday, 24 May 2019 at 12:24:02 UTC, Alex wrote:
If it truly is a 27x faster then then that is very relevant 
and knowing why is important.


Of course, a lot of that might simply be due to LDC and I 
wasn't able to determine this.


Just one more thing you really ought to consider:

It isn't obvious that a LUT using double will be more precise 
than computing sin using single precision float.


So when I use single precision float with ldc and "-O3 
-ffast-math" then I get roughly 300ms. So in that case the LUT 
is only 3 times faster.


Perhaps not worth it then. You might as well just use float 
instead of double.


The downside is that -ffast-math makes all your computations 
more inaccurate.


It is also possible that recent processors can do multiple 
sin/cos as simd.


Too many options… I know.


The thing is, the LUT can have as much precision as one wants. 
One could even spend several days calculating it then loading it 
from disk.


I'm not sure what the real precision of the build in functions 
are but it shouldn't be hard to max out a double using standard 
methods(even if slow, but irrelevant after the LUT has been 
created).


Right now I'm just using the built ins... Maybe later I'll get 
back around to all this and make some progress.




Re: Performance of tables slower than built in?

2019-05-24 Thread Alex via Digitalmars-d-learn

On Friday, 24 May 2019 at 11:45:46 UTC, Ola Fosheim Grøstad wrote:
On Friday, 24 May 2019 at 08:33:34 UTC, Ola Fosheim Grøstad 
wrote:

On Thursday, 23 May 2019 at 21:47:45 UTC, Alex wrote:
Either way, sin it's still twice as fast. Also, in the code 
the sinTab version is missing the writeln so it would have 
been faster.. so it is not being optimized out.


Well, when I run this modified version:

https://gist.github.com/run-dlang/9f29a83b7b6754da98993063029ef93c

on https://run.dlang.io/

then I get:

LUT:709
sin(x): 2761

So the LUT is 3-4 times faster even with your quarter-LUT 
overhead.


FWIW, as far as I can tell I managed to get the lookup version 
down to 104 by using bit manipulation tricks like these:


auto fastQuarterLookup(double x){
const ulong mantissa = cast(ulong)( (x - floor(x)) * 
(cast(double)(1UL<<63)*2.0) );
const double sign = 
cast(double)(-cast(uint)((mantissa>>63)&1));

… etc

So it seems like a quarter-wave LUT is 27 times faster than sin…



If so then that is great and what I'd expected to achieve 
originally.


I guess this is using LDC though? I wasn't able to compile with 
LDC since after updating I'm getting linker errors that I have to 
go figure out.


You just have to make sure that the generated instructions 
fills the entire CPU pipeline.


What exactly does this mean? I realize the pipeline in cpu's is 
how the cpu decodes and optimizes the instructions but when you 
say "You have to make sure" that pre-supposes there is a method 
or algorithm to know.


Are you saying that I did not have enough instructions that the 
pipeline could take advantage of?



In any case, I'll check your code out and try to figure out the 
details and see exactly what is going on.


If it truly is a 27x faster then then that is very relevant and 
knowing why is important.


Of course, a lot of that might simply be due to LDC and I wasn't 
able to determine this.


Can you do some stats for dmd and ldc?

You seem to be interested in this, are you up for a challenge?


The idea is to use tables to optimize these functions.

Half sin was done above but quarter sine can be used(there are 4 
quadrants but only one has to be tabularized because all the 
others differ by sign and reversion(1 - x), it's a matter of 
figuring out the sign).


Of course it requires extra computation so it would be 
interesting to see the difference in performance for the extra 
logic.


Then there is exp

exp(x) can be written as exp(floor(x) + {x}) = 
exp(floor(x))*exp({x})


and so one can optimize this by tabulating exp(x) for 0<= x < 1 
which is the fractional part of exp(x).


Then tabulating it for a wide range of integers(probably in 2's).


e.g.,

exp(3.5) = exp(3)*exp(.5)

both come from a lookup table.

or one could do

exp(3) = exp(1 + 1 + 1) = exp(1)*exp(1)*exp(1)

(this requires iteration if we do not tabulate exp(3).

Hence one would limit the iteration count by tabulating things 
like exp(10^k) and exp(k) for -10 < k < 10.


The idea is basically one can get really dense LUT's for a small 
range that then are used to build the function for arbitrary 
inputs.


With linear interpolation one can get very accurate(for all 
practical purposes) LUT table methods that, if your code is 
right, is at least an order of magnitude faster. The memory 
requirements will be quite small with linear interpolation(and 
ideally quadratic or cubic if the costs are not too high).


That was what I was starting to work on before I got thrown off 
by it being much slower.


It seems you already have the half-sin done.

One could do things like sin, cos(obviously easy), exp, exp()^2, 
erf(x), sinh, cosh, etc. Things like sec could also be done as it 
would save a division since it seems they take about 30 cycles. 
But it would depend on the memory used.


[I can't mess with this now because I've gotta work other things 
at the moment]


Thanks.




















Re: Performance of tables slower than built in?

2019-05-24 Thread Alex via Digitalmars-d-learn

On Friday, 24 May 2019 at 11:57:44 UTC, Alex wrote:

On Friday, 24 May 2019 at 08:13:00 UTC, Basilez B. wrote:

On Thursday, 23 May 2019 at 10:16:42 UTC, Alex wrote:

On Wednesday, 22 May 2019 at 08:25:58 UTC, Basile B. wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:
I am trying to create some fast sin, sinc, and exponential 
routines to speed up some code by using tables... but it 
seems it's slower than the function itself?!?


[...]


Hi, lookup tables ARE faster but the problem you have here, 
and I'm surprised that nobody noticed it so far, is that 
YOUR SWITCH LEADS TO A RUNTIME STRING COMPARISON AT RUNTIME. 
Just replace it with a static if (Method = "Linear") { 
/*...*/} else { /*...*/}


Also takes care to the type used. With DMD the implicit 
coercion of float and double can lead to extra conversions.


You'll directly see a 15% gain after refactoring the switch.


Surely not?!?! Surely the compiler can optimize that switch 
since the value passed is CT? I thought the whole point of 
not having static switch(analogous to static if) was because 
it would go ahead and optimize these cases for us... and it's 
just a switch, just a jmp table.


Try by yourself but to be clear note that I don't like your 
attitude, which I find disrespectful.


Are you an idiot or on medications? My attitude? What did you 
want me to do? Suck your cock? My attitude? Seriously? WHERE? 
WHERE? You quoted everything?


I don't like your attitude? You seem to be extremely 
oversensitive and take things out of context?


It sounds like you just don't like people questioning you in 
any way shape or form even if you are wrong.


"Surely not?!?! Surely the compiler can optimize that switch
since the value passed is CT? I thought the whole point of not
having static switch(analogous to static if) was because it
would go ahead and optimize these cases for us... and it's
just a switch, just a jmp table."

Where? I seriously do not like your attitude though! You attack 
me when I said nothing offensive to you. You have serious 
problems. Get back on or get off the lithium.


[Now, of course, you get to see what an attitude really looks 
like... maybe I've taught you a bit about perspective? I doubt 
it.]


What amazes me is that you Basilez come in and start stuff and 
yet my response to you starting stuff will get deleted and yours 
won't... that is typical.


You really have mental issues. No where in my post was I hostile 
to you and yet you decided to have a hissy fit.  You are a 
snowflake that obviously need a ass kicking. That will teach you 
not to be so sensitive and interpret things in your own little 
cushioned wall universe.


See, no one was attacking you or questioning your intelligence... 
that is something you perceived and made up all on your own. But 
you will be supported by all the other people who love cushy 
walls and koolaid rather than getting the help you need.





Re: Performance of tables slower than built in?

2019-05-24 Thread Alex via Digitalmars-d-learn

On Friday, 24 May 2019 at 11:45:46 UTC, Ola Fosheim Grøstad wrote:
On Friday, 24 May 2019 at 08:33:34 UTC, Ola Fosheim Grøstad 
wrote:

On Thursday, 23 May 2019 at 21:47:45 UTC, Alex wrote:
Either way, sin it's still twice as fast. Also, in the code 
the sinTab version is missing the writeln so it would have 
been faster.. so it is not being optimized out.


Well, when I run this modified version:

https://gist.github.com/run-dlang/9f29a83b7b6754da98993063029ef93c

on https://run.dlang.io/

then I get:

LUT:709
sin(x): 2761

So the LUT is 3-4 times faster even with your quarter-LUT 
overhead.


FWIW, as far as I can tell I managed to get the lookup version 
down to 104 by using bit manipulation tricks like these:


auto fastQuarterLookup(double x){
const ulong mantissa = cast(ulong)( (x - floor(x)) * 
(cast(double)(1UL<<63)*2.0) );
const double sign = 
cast(double)(-cast(uint)((mantissa>>63)&1));

… etc

So it seems like a quarter-wave LUT is 27 times faster than sin…

You just have to make sure that the generated instructions 
fills the entire CPU pipeline.



Well, the QuarterWave was suppose to generate just a quarter 
since that is all that is required for these functions due to 
symmetry and periodicity. I started with a half to get that 
working then figure out the sign flipping.


Essentially one just has to tabulate a quarter of sin, that is, 
from 0 to 90o and then get the sin right. This allows one to have 
4 times the resolution or 1/4 the size at the same cost.


Or, to put it another say, sin as 4 fold redundancy.

I'll check out your code, thanks for looking in to it.


Re: Performance of tables slower than built in?

2019-05-24 Thread Alex via Digitalmars-d-learn

On Friday, 24 May 2019 at 08:13:00 UTC, Basilez B. wrote:

On Thursday, 23 May 2019 at 10:16:42 UTC, Alex wrote:

On Wednesday, 22 May 2019 at 08:25:58 UTC, Basile B. wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:
I am trying to create some fast sin, sinc, and exponential 
routines to speed up some code by using tables... but it 
seems it's slower than the function itself?!?


[...]


Hi, lookup tables ARE faster but the problem you have here, 
and I'm surprised that nobody noticed it so far, is that YOUR 
SWITCH LEADS TO A RUNTIME STRING COMPARISON AT RUNTIME. Just 
replace it with a static if (Method = "Linear") { /*...*/} 
else { /*...*/}


Also takes care to the type used. With DMD the implicit 
coercion of float and double can lead to extra conversions.


You'll directly see a 15% gain after refactoring the switch.


Surely not?!?! Surely the compiler can optimize that switch 
since the value passed is CT? I thought the whole point of not 
having static switch(analogous to static if) was because it 
would go ahead and optimize these cases for us... and it's 
just a switch, just a jmp table.


Try by yourself but to be clear note that I don't like your 
attitude, which I find disrespectful.


Are you an idiot or on medications? My attitude? What did you 
want me to do? Suck your cock? My attitude? Seriously? WHERE? 
WHERE? You quoted everything?


I don't like your attitude? You seem to be extremely 
oversensitive and take things out of context?


It sounds like you just don't like people questioning you in any 
way shape or form even if you are wrong.


"Surely not?!?! Surely the compiler can optimize that switch
since the value passed is CT? I thought the whole point of not
having static switch(analogous to static if) was because it
would go ahead and optimize these cases for us... and it's
just a switch, just a jmp table."

Where? I seriously do not like your attitude though! You attack 
me when I said nothing offensive to you. You have serious 
problems. Get back on or get off the lithium.


[Now, of course, you get to see what an attitude really looks 
like... maybe I've taught you a bit about perspective? I doubt 
it.]




Re: Performance of tables slower than built in?

2019-05-23 Thread Alex via Digitalmars-d-learn
On Thursday, 23 May 2019 at 18:57:03 UTC, Ola Fosheim Grøstad 
wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:
I am trying to create some fast sin, sinc, and exponential 
routines to speed up some code by using tables... but it seems 
it's slower than the function itself?!?


Not when I tried it with one of the online compilers, LUT is 
3-4 times faster on your tight inner loop, but I guess it 
depends on the CPU.


LUT should be very fast in long-running tight inner loops like 
that once the cache is warm with such as small LUT that fits in 
working memory (cache).


I've used very small LUT's like a length of 5 and it didn't 
significantly change anything.


Unless it is being hit hard at the start and that is skewing the 
results, it doesn't seem to be the issue.


I haven't tested this well but was just thrown off by the results 
as it should easily have been inverted and I expected quite a 
significant speed up(several factors) and not the reverse.


Re: Performance of tables slower than built in?

2019-05-23 Thread Alex via Digitalmars-d-learn
On Thursday, 23 May 2019 at 19:17:40 UTC, Ola Fosheim Grøstad 
wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:

xxx = 0;
sw.reset();
sw.start();
for(double i = 0; i < 1000; i++) xxx += sin(PI*i);
t = sw.peek().msecs;
writeln(t);
sw.stop();
}


What you are doing wrong is that xxx is never used... So DMD 
removes it altogether?


If you add writeln(xxx) after the second loop as well, then 
maybe the measurements make more sense?


Ola.


maybe, I thought I put in the writeln(xxx) in there to do that... 
yeah, looking at my code it's there, so that is not the problem.


This is the code I had,

import std.datetime;
double xxx = 0;
StopWatch sw;
sw.start();
for(double i = 0; i < 1000; i++) xxx += sinTab(i);
auto t = sw.peek().msecs;
writeln(t);
sw.stop();
writeln(xxx);

xxx = 0;
sw.reset();
sw.start();
for(double i = 0; i < 1000; i++) xxx += sin(PI*i);
t = sw.peek().msecs;
writeln(t);
sw.stop();
writeln(xxx);

Either I modified it later or deleted one of those lines or the 
editor screwed up when I pasted it... I don't recall touching the 
code afterwards.


Either way, sin it's still twice as fast. Also, in the code the 
sinTab version is missing the writeln so it would have been 
faster.. so it is not being optimized out.




Re: Performance of tables slower than built in?

2019-05-23 Thread Alex via Digitalmars-d-learn

On Thursday, 23 May 2019 at 15:20:22 UTC, Timon Gehr wrote:

On 23.05.19 12:21, Alex wrote:

On Wednesday, 22 May 2019 at 00:55:37 UTC, Adam D. Ruppe wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:
I am trying to create some fast sin, sinc, and exponential 
routines to speed up some code by using tables... but it 
seems it's slower than the function itself?!?


There's intrinsic cpu instructions for some of those that can 
do the math faster than waiting on memory access.


It is quite likely calculating it is actually faster. Even 
carefully written and optimized tables tend to just have a 
very small win relative to the cpu nowadays.


Surely not? I'm not sure what method is used to calculate them 
and maybe a table method is used internally for the common 
functions(maybe the periodic ones) but memory access surely is 
faster than multiplying doubles?

...


Depends on what kind of memory access, and what kind of faster. 
If you hit L1 cache then a memory access might be (barely) 
faster than a single double multiplication. (But modern 
hardware usually can do multiple double multiplies in parallel, 
and presumably also multiple memory reads, using SIMD and/or 
instruction-level parallelism.)


I think a single in-register double multiplication will be 
roughly 25 times faster than an access to main memory. Each 
access to main memory will pull an entire cache line from main 
memory to the cache, so if you have good locality (you usually 
won't with a LUT), your memory accesses will be faster on 
average. There are a lot of other microarchitectural details 
that can matter quite a lot for performance.




I realize a lot of optimizations could be made but I still find 
it hard to believe that it could be done faster even with special 
hardware unless a LUT is used in the hardware already or some 
highly optimized algorithms.


The whole point of general purpose computing was to get away from 
specialized hardware because it was cheaper and one could spend 
the resources in improving raw cycle performance which would 
benefit the entire system.



But I guess because of complexity of hardware now days it's hard 
to really figure out what is going on.


http://ithare.com/infographics-operation-costs-in-cpu-clock-cycles/

Maybe when it is an "intrinsic" it can avoid some of the issues 
that might otherwise cost in a LUT(such as a context switch).





And most of the time these functions are computed by some 
series that requires many terms. I'd expect, say, to compute 
sin one would require at least 10 multiplies for any 
accuracy... and surely that is much slower than simply 
accessing a table(it's true that my code is more complex due 
to the modulos and maybe that is eating up the diff).


Do you have any proof of your claims? Like a paper that 
discusses such things so I can see what's really going on and 
how they achieve such performance(and how accurate)?


Not exactly what you asked, but this might help:
https://www.agner.org/optimize

Also, look up the CORDIC algorithm.


I don't see how that can necessarily be faster. A LUT can give 
full 64-bit precision with one operation. The CORDIC needs 
iteration, at least 10 to be of any use. LUT's are precision 
independent assuming the creation cost is not included.


I couldn't find anything in that link that specifically addressed 
speeding up typical math functions.


It would be nice to know exactly what is going on, which 
functions are optimized and the typical speed up over a lut.


I have some code that uses sin, exp and a few other primitive 
algebraic functions and in one case the code is extremely slow(it 
uses exp).  I haven't done much testing of all this but something 
just seems off somewhere. Either the sin is heavily optimized and 
exp is not(which it too have a CORDIC like algorithm since 
exp(a+b) = exp(a)*exp(b)) or something else is happening that I'm 
not aware of.. which is why I went to tabularizing these 
functions in the first place.


Also, I don't know the kinda accuracy they have, which doesn't 
help much but I could use a more accurate algorithm which is 
slower since it is pre-computed.


That site I linked does talk about some of the issues and 
stuff... I guess I just need to update my thinking on modern cpus 
a little better.



I guess there is no tool that can tell one exactly what is 
happening to a piece of code in the cpu... basically an 
instruction level profiler?


Re: Performance of tables slower than built in?

2019-05-23 Thread Alex via Digitalmars-d-learn

On Wednesday, 22 May 2019 at 00:55:37 UTC, Adam D. Ruppe wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:
I am trying to create some fast sin, sinc, and exponential 
routines to speed up some code by using tables... but it seems 
it's slower than the function itself?!?


There's intrinsic cpu instructions for some of those that can 
do the math faster than waiting on memory access.


It is quite likely calculating it is actually faster. Even 
carefully written and optimized tables tend to just have a very 
small win relative to the cpu nowadays.


Surely not? I'm not sure what method is used to calculate them 
and maybe a table method is used internally for the common 
functions(maybe the periodic ones) but memory access surely is 
faster than multiplying doubles?


And most of the time these functions are computed by some series 
that requires many terms. I'd expect, say, to compute sin one 
would require at least 10 multiplies for any accuracy... and 
surely that is much slower than simply accessing a table(it's 
true that my code is more complex due to the modulos and maybe 
that is eating up the diff).


Do you have any proof of your claims? Like a paper that discusses 
such things so I can see what's really going on and how they 
achieve such performance(and how accurate)?


Re: Performance of tables slower than built in?

2019-05-23 Thread Alex via Digitalmars-d-learn

On Wednesday, 22 May 2019 at 08:25:58 UTC, Basile B. wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:
I am trying to create some fast sin, sinc, and exponential 
routines to speed up some code by using tables... but it seems 
it's slower than the function itself?!?


[...]


Hi, lookup tables ARE faster but the problem you have here, and 
I'm surprised that nobody noticed it so far, is that YOUR 
SWITCH LEADS TO A RUNTIME STRING COMPARISON AT RUNTIME. Just 
replace it with a static if (Method = "Linear") { /*...*/} else 
{ /*...*/}


Also takes care to the type used. With DMD the implicit 
coercion of float and double can lead to extra conversions.


You'll directly see a 15% gain after refactoring the switch.


Surely not?!?! Surely the compiler can optimize that switch since 
the value passed is CT? I thought the whole point of not having 
static switch(analogous to static if) was because it would go 
ahead and optimize these cases for us... and it's just a switch, 
just a jmp table.





Re: Why function does not work with delegate

2019-05-23 Thread Alex via Digitalmars-d-learn

On Wednesday, 22 May 2019 at 23:54:47 UTC, Adam D. Ruppe wrote:

On Wednesday, 22 May 2019 at 22:33:52 UTC, Alex wrote:

auto x = (GdkEventButton* e, Widget w) ...
X.addOnButtonPress(x);

Why is x not a delegate?


Because you didn't ask for one and it didn't have to be.

Just add the delegate keyword to ask for one:

auto x = delegate(GdkEventButton* e, Widget w) {};
 


That is what I was looking for.

Thanks.





Why function does not work with delegate

2019-05-22 Thread Alex via Digitalmars-d-learn

In gtkD one can use a lambda directly:

X.addOnButtonPress((GdkEventButton* e, Widget w) ...

but if I try move the lambda in to a variable so I can use it 
with multiple handlers, I get an error:


// x is a function and does not work
auto x = (GdkEventButton* e, Widget w) ...
X.addOnButtonPress(x);

etc...

It's because the second case treats x as a function while the 
first it is a delegate and addOnButtonPress requires a delegate...


Why is x not a delegate?

Now, if I reference a variable outside the scope of the lambda it 
turns magically in to a delegate and works!


But that is an ugly hack!

// now x is a delegate and works
auto x = (GdkEventButton* e, Widget w) { auto q = X; ...


Now sure I can hack x and use some template to turn it in to a 
fake delegate but then that is dangerous.


I suppose one could have a template that only converts it if it 
is a function and then that will deal with both cases and might 
work...


But why?
Is there any way to force it to not reduce the delegate to a 
function which is obviously an optimization when nothing accesses 
the outside context.










Re: release build with debug information?

2019-05-21 Thread Alex via Digitalmars-d-learn

On Tuesday, 21 May 2019 at 09:48:34 UTC, Robert M. Münch wrote:
Is it possible to compile a release build with debug 
information?


There is a "release-debug" version in case you are using dub. Not 
sure, if there is enough debug info for you.


Impose structure on array

2019-05-20 Thread Alex via Digitalmars-d-learn
given some array, is there some way to easily impose structure on 
that array at runtime?


void* data;

auto x = cast(byte[A,B,C])data;

X is then an AxBxC matrix.

I'm having to compute the index myself and it just seems 
unnecessary. A and B are not known at compile time though.


Obviously it should be just as efficient as computing the offset 
manually.


I could probably do this with a struct and override opIndex but 
I'm wondering what is already out there. If it's slower or 
consumes more memory than manual it's not worth it(since my code 
already calcs the index).






Re: Basic Linear Algebra and D's Array Operation

2019-05-19 Thread Alex via Digitalmars-d-learn

On Sunday, 19 May 2019 at 16:17:17 UTC, Andrew Edwards wrote:

´´´
import std;

void main()
{
int[][] M = [[1,2,3],[1,2,3],[1,2,3]];
M.recursiveMultiplier(4);
writeln(M);
}

void recursiveMultiplier(T, V)(T arr, V val) @nogc
{
static if(isArray!(ElementType!T))
arr.each!(el => el.recursiveMultiplier(val));
else
arr[] = arr[] * val;
}
´´´


Can you do me a favor and extend it to cover the cases where V 
is either an array or multidimensional array? The text I'm 
using has exercise to implement a bunch of matrix operations so 
that would help out a lot.




Not really. For a general matrix-matrix multiplication special 
algorithms exist. ndslices use blas for calculation. For 
different numeric types, things get involved very fast, not to 
speak about special algorithms for special-formed matrices.
And I don't even know, which representation (row/column major) 
you expect ;)


The operation itself is, however, a simple one. To implement a 
basic version I would cite

http://rosettacode.org/wiki/Matrix_multiplication#D


Re: Basic Linear Algebra and D's Array Operation

2019-05-19 Thread Alex via Digitalmars-d-learn

On Sunday, 19 May 2019 at 06:34:18 UTC, Andrew Edwards wrote:
Sooo... I'm trying to learn this stuff so that I can fully 
grasp the content of Jens Mueller's 2019 DConf talk and its 
applications in financial sector (forex and options/futures 
trading). Unfortunately, I'm doing so using python but I'd like 
to accomplish the same in D. Here goes:


Array (Vector) Algebra

auto V = [1, 2, 3];
auto v = V[] * 4; // v == [4, 8, 12]

Output:
	Error: array operation V[] * 4 without destination memory not 
allowed


Huh? What? That makes no sense, but okay... whatever.

int[3] v = V[] * 4; // v == [4, 8, 12]
v[] = v[] + v[]; // v == [8, 16, 24]
v[] = v[] / 2; // v == [4, 8, 12]
v[] = v[] - v[]; // v = [0, 0, 0]

so far so good. On to the fun stuff.

Multidimensional Array (Matrix) Algebra

auto M = [[1, 2, 3], [1, 2, 3], [1,2,3]];
int[3][3] m = M[] * 4;

Output:
Error: incompatible types for (M[]) * (4): int[][] and int

Okay, I'm lost on that one. But let's press on.

int[3][3] m = M[] * [[4]];

Output:
	Error: cannot implicitly convert expression M[] * [[4]] of 
type int[][] to int[]


int[3][3] m = M[] * [[4,4,4], [4,4,4], [4,4,4]];

Output:
	Error: cannot implicitly convert expression M[] * [[4, 4, 4], 
[4, 4, 4], [4, 4, 4]] of type int[][] to int[]


I'm so confused. Maybe it's the way I'm accessing M?

int[3][3] m = M[][] * 4;

Output:
Error: incompatible types for (M[]) * (4): int[][] and int

And... it begins again!!!

So the question is, how do I pull this off in D using just 
builtin arrays and phobos?


Any assistance is appreciated.



Matrix operations are a pain in D. This is due to static arrays 
and dynamic arrays are different types and don't mix a priori:

https://dlang.org/spec/arrays.html

Slice operations exist, but they are defined mainly for arrays 
and not for arrays of arrays. Some basic operations (like 
setting) are possible, but as you see, even multiplication fails. 
You could reinvent something like


´´´
import std;

void main()
{
int[][] M = [[1,2,3],[1,2,3],[1,2,3]];
M.recursiveMultiplier(4);
writeln(M);
}

void recursiveMultiplier(T, V)(T arr, V val) @nogc
{
static if(isArray!(ElementType!T))
arr.each!(el => el.recursiveMultiplier(val));
else
arr[] = arr[] * val;
}
´´´

BUT:
As I saw, in the talk, NDarrays are referenced. The common 
(widely used and recommended) way to use this functionality in D 
is by using mir.ndslices.

http://docs.algorithm.dlang.io/latest/mir_ndslice.html


Thanks,
Andrew

P.S. Why do we still have two sets of documentations ([1],[2]) 
for the language? Which is the official one and when can we get 
rid of the other?


[1] https://dlang.org/library/std/array.html
[2] https://dlang.org/phobos/std_array.html


Re: Creating Anonymous structs

2019-05-18 Thread Alex via Digitalmars-d-learn

On Saturday, 18 May 2019 at 20:53:50 UTC, Alex wrote:

On Saturday, 18 May 2019 at 20:03:14 UTC, Adam D. Ruppe wrote:

On Saturday, 18 May 2019 at 15:59:05 UTC, Alex wrote:
Structs combine data, I have a use case where I do not want 
to litter the scope with variables and would like to put them 
in a struct but the variable will only be used once.


This is basically what Phobos's Tuple is.

auto x = tuple(value_for_x, value_for_y);

http://dpldocs.info/experimental-docs/std.typecons.tuple.html


I thought about using them but they are not the same. A tuple 
cannot have functionality. It's not as general as a struct or 
class and it doesn't look as nice if the struct is large... and 
if we were to use a class we don't get to inherit.


I suppose one could use delegates but it's still a pain because 
to setup and ends up being more terse than just going about it 
the standard way.


Re: Creating Anonymous structs

2019-05-18 Thread Alex via Digitalmars-d-learn

On Saturday, 18 May 2019 at 20:03:14 UTC, Adam D. Ruppe wrote:

On Saturday, 18 May 2019 at 15:59:05 UTC, Alex wrote:
Structs combine data, I have a use case where I do not want to 
litter the scope with variables and would like to put them in 
a struct but the variable will only be used once.


This is basically what Phobos's Tuple is.

auto x = tuple(value_for_x, value_for_y);

http://dpldocs.info/experimental-docs/std.typecons.tuple.html


I thought about using them but they are not the same. A tuple 
cannot have functionality. It's not as general as a struct or 
class and it doesn't look as nice if the struct is large... and 
if we were to use a class we don't get to inherit.




Re: Creating Anonymous structs

2019-05-18 Thread Alex via Digitalmars-d-learn
Also, I realize one could use Voldemort types, e.g., something 
like


auto x = (){ struct X { int x; } return X(); }

but this is so verbose as to not really be any better(although it 
does accomplish hiding the struct, I'm not so concerned with 
hiding the struct as I am code brevity. I do realize there is 
very little extra chars but that isn't the point. I'm asking if 
there is a way to accomplish what I'd like to do).


Creating Anonymous structs

2019-05-18 Thread Alex via Digitalmars-d-learn
Structs combine data, I have a use case where I do not want to 
litter the scope with variables and would like to put them in a 
struct but the variable will only be used once.


struct X
{
  int x;
  double y;
}

X x;

Seems redundant to have to do this, rather, it would be nice to do

auto x = struct {
  int x;
  double y;
};

And this would create the same effect but it would only exist in 
one place.


Is there any way to accomplish this without actually defeating 
the purpose?


Obviously there is not a huge difference between what is possible 
and what I want to achieve. I just feel it would be more sound to 
use an anonymous struct.









Re: 1 - 17 ms, 553 ╬╝s, and 1 hnsec

2019-05-18 Thread Alex via Digitalmars-d-learn

On Friday, 17 May 2019 at 18:02:04 UTC, kdevel wrote:
On Thursday, 16 May 2019 at 20:31:23 UTC, Vladimir Panteleev 
wrote:

On Thursday, 16 May 2019 at 20:17:37 UTC, Steven Schveighoffer


[...]

hnsecs is more confusing than nanoseconds. People know what a 
nanosecond is, a hecto-nano-second is not as familiar a term.


Agreed, which is why Duration.toString shouldn't be used to 
present durations to users.


Developers, however, are expected to know what a 
hectonanosecond is, same as with all the other technical terms.


"hectonanosecond" looks like an illegal combination of SI 
prefixes [1]. I recommend changing the meaning of hnsecs to 
"[one] hundred nanoseconds".


[1] "Prefixes may not be used in combination."
https://en.wikipedia.org/wiki/Metric_prefix


It through me off, it really  makes no sense. The purpose of a 
prefix is to define something better. hectonano seems 
contradictory... and is it any different than nanohecto?


There really is no point in it, the whole reason for the metric 
system is to provide a hierarchical resolution. Just use nano or 
pico


Re: Blog Post #0036 - File Dialog - Open a Single File

2019-05-17 Thread Alex via Digitalmars-d-learn

On Friday, 17 May 2019 at 09:24:59 UTC, Ron Tarrant wrote:
The second post this week continues the series on Dialogs. This 
one is about opening files and can be found here: 
http://gtkdcoding.com/2019/05/17/0036-file-open-dialogs.html


So, I'm using gtkD and eventually I'll have the need to use 
movable icons that can be interacted with using the icon by 
dragging them around in a DrawingArea.


So if you need ideas to for another tutorial that would be 
helpful to me. It's probably not too hard but I've not messed 
with those things yet and maybe you'll find an easier way to deal 
with it.


The idea would be to create DrawingArea and then display the 
"icons" and have mouse interaction. I'm not sure if using custom 
drawing or something built in is best. Custom drawing is probably 
what I'll do since I know more about that then gtk.


Re: disabling and enabling console output

2019-05-16 Thread Alex via Digitalmars-d-learn

On Thursday, 16 May 2019 at 17:49:24 UTC, Adam D. Ruppe wrote:

On Thursday, 16 May 2019 at 14:53:14 UTC, Alex wrote:
I have some code that disables the console because some other 
code puts junk on it that I don't want to see


s stupid question but can't you just edit the other code to 
not put junk on it?




No, it is in an external lib and I'd have to mod the code, 
maintain it, and compile it...


should be much simpler just to redirect stdout.

In fact, it works, I just can figure out how to re-enable it.






Re: disabling and enabling console output

2019-05-16 Thread Alex via Digitalmars-d-learn
On Thursday, 16 May 2019 at 17:19:13 UTC, Vladimir Panteleev 
wrote:

On Thursday, 16 May 2019 at 17:18:01 UTC, Alex wrote:

adding

int dup(int) @trusted;
int dup2(int, int) @trusted;
int close(int) @trusted;
int open(in char*, int, ...) @trusted;


Be sure to make them extern(C).

Sorry, I haven't tried it, I'm guessing that it should work 
based on:

https://github.com/digitalmars/dmc/blob/master/include/io.h#L142-L147


Ok, the issue is because I was adding

import core.stdc.stdio, core.stdc.stdio;
extern (C):
@system:
nothrow:
@nogc:
static int _dup(int);
static int dup2(int, int);

directly inside the PushFD struct... I marked them static and it 
still didn't help. I don't know it's adding the class for the 
lookup:


PushFD!(1).PushFD.dup2(int, int)" 
(_D3mM__T6PushFDVii1ZQm4dup2UNbNiiiZi)


Unfortunately the code is crashing because _open or _sopen is 
returning -1 for /dev/null


Works fine nul, NUL, and CONOUT$ but does not block the output.


I'm not sure if they are failing to block or if they are blocking 
what is being opened(and not the original console). That is, do I 
need to not open and simply close stdout?






Re: disabling and enabling console output

2019-05-16 Thread Alex via Digitalmars-d-learn
On Thursday, 16 May 2019 at 17:07:39 UTC, Vladimir Panteleev 
wrote:

On Thursday, 16 May 2019 at 17:05:01 UTC, Alex wrote:
One thing you could try is going one level lower, and using 
dup() to save the stream to another fd, close() to close the 
stdout one, and dup2() to restore the saved fd over the 
stdout one.


Unfortunately D doesn't seem to have dup, dup2.


They are in the C library. Looks like Druntime has D 
declarations only for Posix:


https://github.com/cybershadow/druntime/blob/issue19433/src/core/sys/posix/unistd.d#L51-L52

You can just copy those.


adding

int dup(int) @trusted;
int dup2(int, int) @trusted;
int close(int) @trusted;
int open(in char*, int, ...) @trusted;


results in an unresolved symbol.


Re: disabling and enabling console output

2019-05-16 Thread Alex via Digitalmars-d-learn
On Thursday, 16 May 2019 at 15:21:48 UTC, Vladimir Panteleev 
wrote:

On Thursday, 16 May 2019 at 14:53:14 UTC, Alex wrote:
I have some code that disables the console because some other 
code puts junk on it that I don't want to see... then I enable 
it.


One thing you could try is going one level lower, and using 
dup() to save the stream to another fd, close() to close the 
stdout one, and dup2() to restore the saved fd over the stdout 
one.


Unfortunately D doesn't seem to have dup, dup2.

On 05/10/14 22:24, MarisaLovesUsAll via Digitalmars-d-learn wrote:
I sometimes got a useless messages in stdout from SDL_Image 
library, and I want to temporary silence it. How do I do?


One way would be something like:

   import std.stdio;

   void writeOutput () {
  static c = 1;
  printf("%d\n", c++);
   }

   void main() {
  writeOutput();

  {
 auto ex = PushFD!1("/dev/null".ptr);
 writeOutput();
  }

  writeOutput();
   }

   struct PushFD(int fd) {
  import core.sys.posix.fcntl, core.sys.posix.unistd;
  int old;
  this(const char* fn) { //
 old = dup(fd);
 auto nfd = open(fn, O_RDWR);
 dup2(nfd, fd);
 close(nfd);
  }
  ~this() { dup2(old, fd); close(old); }
   }

// In real code you'll want to check for errors from 
dup/dup2/open/close.


artur

That code fails to compile on windows.


Re: 1 - 17 ms, 553 ╬╝s, and 1 hnsec

2019-05-16 Thread Alex via Digitalmars-d-learn
On Thursday, 16 May 2019 at 15:27:33 UTC, Vladimir Panteleev 
wrote:

On Thursday, 16 May 2019 at 15:19:03 UTC, Alex wrote:

1 - 17 ms, 553 ╬╝s, and 1 hnsec

WTH!! is there any way to just get a normal u rather than some 
fancy useless asci hieroglyphic? Why don't we have a fancy M? 
and an h?


It's outputting UTF-8, but, your console is not configured to 
display UTF-8.


On Windows, you can do so (before running your program), by 
running: chcp 65001


Or, within your program, by calling: 
SetConsoleOutputCP(CP_UTF8);


Note that this has some negative side effects, which is why D 
doesn't do it automatically. (Blame Windows.)



What's an hnsec anyways?


Hecto-nano-second, the smallest representable unit of time in 
SysTime and Duration.


Thanks...

Why not just use u? If that is too much trouble then detect the 
code page and use u rather than the extended ascii which looks 
very out of place?


1 - 17 ms, 553 ╬╝s, and 1 hnsec

2019-05-16 Thread Alex via Digitalmars-d-learn

1 - 17 ms, 553 ╬╝s, and 1 hnsec

WTH!! is there any way to just get a normal u rather than some 
fancy useless asci hieroglyphic? Why don't we have a fancy M? and 
an h?


What's an hnsec anyways?


disabling and enabling console output

2019-05-16 Thread Alex via Digitalmars-d-learn
I have some code that disables the console because some other 
code puts junk on it that I don't want to see... then I enable it.


stdout.close();
stderr.close();

...
stdout.open("CON", "w");
stderr.open("CON", "w");

It works but when the routine that uses this is called twice, it 
completely disables all output afterwards.


//1
stdout.close();
stderr.close();
//2
stdout.open("CON", "w");
stderr.open("CON", "w");
//3
stdout.close();
stderr.close();
//4
stdout.open("CON", "w");
stderr.open("CON", "w");
//5


So, one gets output at 1 and 3 but not 5.

Am I doing something wrong or is this a bug?


I've tried various things such as saving and restoring stdout, 
using CONOUT$, etc...


I think it might be outputting in 3 because of caching rather 
than it actually working once.


Can play around with it here:

https://run.dlang.io/is/q3kpSB


Re: GtkD slows down visual D keyboard

2019-05-15 Thread Alex via Digitalmars-d-learn

A hack:

On Tue, 14 May 2019 19:44:01 +0200, Mike Wey wrote:

On 14-05-2019 05:10, Alex X wrote:

Any news on this?

https://forum.dlang.org/thread/bznpylcjostbrrwzh...@forum.dlang.org

It's severely cramping my style ;/



Unfortunately no.



	// The following code bypasses GTK windows hooking that causes 
problems with visual studio debugging(slow keyboard, it is a hack 
and may not work in general)

debug {
import core.sys.windows.windows, core.stdc.string;
		alias HHOOK function(int, HOOKPROC, HINSTANCE, DWORD) 
SetWindowsHookExAProc;
		alias HHOOK function(int, HOOKPROC, HINSTANCE, DWORD) 
SetWindowsHookExWProc;
		static extern (Windows) HHOOK KBHook(int, HOOKPROC, HINSTANCE, 
DWORD)

{
asm
{
naked;
ret;
}
}

DWORD old;
auto err = GetLastError();

auto hModule = LoadLibrary("User32.dll");
		auto proc = cast(SetWindowsHookExAProc)GetProcAddress(hModule, 
"SetWindowsHookExA");

err = GetLastError();
VirtualProtect(proc, 40, PAGE_EXECUTE_READWRITE, );
err = GetLastError();

memcpy(proc, , 7);
// Cleanup  
//FreeLibrary(hModule);
}


The code above is based on the idea from

https://github.com/microsoft/Detours

I didn't feel like investing the time to integrate it in to my 
code.


The code I've included is a hack that simply bypasses all hooking 
routines and hence the screwed up keyboard hook.


It only works for x64 since ret works to return properly(x86 
messages with the stack and requires a bit more code).







Re: Compiler/Phobos/Types problem — panic level due to timing.

2019-05-08 Thread Alex via Digitalmars-d-learn

On Wednesday, 8 May 2019 at 11:53:34 UTC, Russel Winder wrote:
On Mon, 2019-05-06 at 15:53 +, John Colvin via 
Digitalmars-d-learn wrote:

[…]

pretty please show people it with UFCS:

recurrence!((a, n) => a[n-1] + a[n-2])(zero, one)
 .dropExactly(n)
 .front


Any particular reason for preferring this form over the 
original?


For example, It is more readable, as the order of execution is 
unwinded.


Re: alias this and struct allocation

2019-05-06 Thread Alex via Digitalmars-d-learn

On Monday, 6 May 2019 at 14:48:56 UTC, faissaloo wrote:
I've been having some memory issues (referenced objects turning 
to nulls for no apparent reason) and I was wondering if I've 
misunderstood how allocation works when instantiating a struct 
that uses alias this:


import std.stdio;

struct Parent {
int a;
}
struct Child {
Parent base;
alias base this;
int y;
}
auto myStructMaker() {
return new Child(Parent(10),20);
}

void main()
{
writeln(*myStructMaker());
}

In this example is the data in base guaranteed to exist?


Yes:
static assert(!__traits(compiles, Child.init.base is null));

Or is base definitely part of the allocation of Child on the 
heap?


I would say, yes. Why do you think it is a contradiction?


Re: Mixin can't access library symbols?

2019-05-04 Thread Alex via Digitalmars-d-learn

On Friday, 3 May 2019 at 17:55:19 UTC, faissaloo wrote:

On Friday, 3 May 2019 at 17:51:39 UTC, Adam D. Ruppe wrote:

On Friday, 3 May 2019 at 17:48:50 UTC, faissaloo wrote:
How can I get a mixin to implicitly include the symbols from 
its surrounding context? Is this possible?


What's your big picture goal? Do you have sample code you have 
tried so far?


Also, are you talking mixin("string") or mixin template? They 
work differently with symbols too.


My sample code is too big to be useful. The use of my mixin 
looks like:


mixin(defState!("XEvent value;"));

But it tells me it can't find the symbol XEvent despite me 
having used it elsewhere.



It's because XEvent is not visible in the scope, you need to 
import the module it is contained in. mixin templates are used in 
the call scope, not the defining scope.


You have some misunderstanding of D meta programming but you h 
ave not given enough info to diagnose the problem.


You look to be using a string that represents a code string that 
will be later mixed in(as a string mixin) but you are also using 
a template mixin.


But most likely you simply need to add an "import ..." somewhere 
to get XEvent in the scope. Sometimes this is a little tricky 
because you want to generalize it so you have to do some weird 
stuff and use mixin("import "~moduleName!T~";");








Re: What's best practice to use compound literals with structs and C-APIs?

2019-05-02 Thread Alex via Digitalmars-d-learn

On Thursday, 2 May 2019 at 07:11:37 UTC, Robert M. Münch wrote:

On 2019-05-01 19:13:54 +, Alex said:

Doesn't work because this seems to kick in some D releated 
run-time stuff which lead to unresolved externals during 
linking:


error LNK2001: Nicht aufgelöstes externes Symbol "...__initZ".
error LNK2001: Nicht aufgelöstes externes Symbol 
"...__xtoHashFNbNeKxSQBtQBoQBfZm".
error LNK2001: Nicht aufgelöstes externes Symbol 
"...__xopEqualsFKxSQBsQBnQBeKxQmZb".


Not sure, why this happens.


Found this bug: https://issues.dlang.org/show_bug.cgi?id=12374

It is closed now, because of inactivity, if you have a current 
case - a reopen is welcome.
However... Not sure, maybe someone else has more knowledge 
about this.


Thanks for the link, looks a bit related. I would need to see 
if I can create a small case which shows the problem...


So, you generate D structs from C, right? If so, and if you 
need the byRef at any cost - you could tweak the generator, so 
byRef is generated for all structs.


Well, the generator is DStep... and I'm not sure if a general 
"byRef" approach would make sense. And listing affected structs 
manually will complicate the workflow again.



yeah...


What I'm irriated about is, that this works:

A tmp = {x,y};
return myfunc();

But this doesn't:

return myfunc(new A(x,y));

Or I haven't found a valid syntax to do it inline.


For me, this works:

´´´
void main()
{
myfunc(new A(5));
}
struct A{
int i;
}

void myfunc(A* myA){}
´´´



Re: GtkD slows down visual D keyboard

2019-05-01 Thread Alex via Digitalmars-d-learn

On Friday, 26 April 2019 at 14:50:17 UTC, Mike Wey wrote:

On 26-04-2019 10:31, Amex wrote:
When debugging under visual D, the keyboard response is slowed 
down to the extreme. This is a Gtk issue I believe. It only 
has to do with the keyboard.


For example, if I hit F10 to step, it takes the ide about 10 
seconds to "respond" and move to the next line... yet the 
mouse can access stuff instantaneous.



I believe Gtk or GtkD is slowing down the keyboard input 
somehow and for some reason making debugging apps a nightmare 
since it literally takes about 100 times longer to debug than 
it should.


searching google reveals:

https://github.com/Microsoft/vcpkg/issues/4529

https://developercommunity.visualstudio.com/content/problem/42018/debugging-with-keyboard-very-slow.html


"You somehow break keyboard shortcuts during debugging in VS 
if the application you're debugging is registering a callback 
with "SetWindowsHookEx" from user32.dll with hook ID 
"WH_KEYBOARD_LL".


Don't call it in debug builds or add "if 
(!Debugger.IsAttached)" in front of the call to 
"SetWindowsHookEx" if the debugger is attached before the 
function is called.


This brings debugging with keyboard back to the same speed as 
with the UI buttons for our application."




This seems to be an issue with Gtk. I'm not sure if GtkD can 
do anything about it. Maybe somehow reroute the keyboard 
handler(first remove it from the hook then call it manually or 
reduce the number of calls to it).


I can confirm that gtk call  "SetWindowsHookEx" with the 
"WH_KEYBOARD_LL" ID upon initialization.


As far as i can tell it doesn't provide a way to skip this.


Any news about this? Do you think it can be fixed in some way?


Re: What's best practice to use compound literals with structs and C-APIs?

2019-05-01 Thread Alex via Digitalmars-d-learn

On Wednesday, 1 May 2019 at 14:59:48 UTC, Robert M. Münch wrote:

On 2019-05-01 14:23:37 +, Alex said:

However, to rebuild the same structure, auto ref parameters 
may be appropriate.

https://dlang.org/spec/template.html#auto-ref-parameters


That would need me to change myfunc which is not possible 
because the D binding is all generated from C code.



Ok, I see.


or you new A(...) in place


Doesn't work because this seems to kick in some D releated 
run-time stuff which lead to unresolved externals during 
linking:


error LNK2001: Nicht aufgelöstes externes Symbol "...__initZ".
error LNK2001: Nicht aufgelöstes externes Symbol 
"...__xtoHashFNbNeKxSQBtQBoQBfZm".
error LNK2001: Nicht aufgelöstes externes Symbol 
"...__xopEqualsFKxSQBsQBnQBeKxQmZb".


Not sure, why this happens.


Found this bug:
https://issues.dlang.org/show_bug.cgi?id=12374

It is closed now, because of inactivity, if you have a current 
case - a reopen is welcome.
However... Not sure, maybe someone else has more knowledge about 
this.





or you use the byRef-pattern as shown in d-idioms
https://p0nce.github.io/d-idioms/#Rvalue-references:-Understanding-auto-ref-and-then-not-using-it



This again would need my to change the C bindings, which are 
generated.


So, you generate D structs from C, right? If so, and if you need 
the byRef at any cost - you could tweak the generator, so byRef 
is generated for all structs.


Re: What's best practice to use compound literals with structs and C-APIs?

2019-05-01 Thread Alex via Digitalmars-d-learn

On Wednesday, 1 May 2019 at 12:47:22 UTC, Robert M. Münch wrote:
I use some C library that uses structs and many functions that 
use pointer to structs as arguments:


struct A {...};
myfunc(A *myA);

In C you can do this to get a lvalue:

myfunc(&(A){...});

In D this doesn't work and I get an "is not an lvalue and 
cannot be modified".


What's the correct D-ish way in such a case?


Look, even in C you have to workaround the case, where you want 
to pass a value instead of a pointer. So, the proper question 
would be, how to redesign, so that &(A){...} is not needed.


However, to rebuild the same structure, auto ref parameters may 
be appropriate.

https://dlang.org/spec/template.html#auto-ref-parameters

or you new A(...) in place

or you use the byRef-pattern as shown in d-idioms
https://p0nce.github.io/d-idioms/#Rvalue-references:-Understanding-auto-ref-and-then-not-using-it

Not sure, which part is under your control...


Determime actual modules used

2019-04-28 Thread Alex via Digitalmars-d-learn
I need to determine the actual modules that are used in the 
project, not just imported but ones that actually are referenced.


Any utility exists to do this?


Re: Call delegate from C++

2019-04-25 Thread Alex via Digitalmars-d-learn
On Wednesday, 24 April 2019 at 16:20:17 UTC, Nicholas Wilson 
wrote:

How do you pass a delegate to a c++ function to be called by it?

The function to pass the delegate to is:

extern (C++) int
fakeEntrypoint(
extern(C++) void function(void* /*delegate's context*/) 
func,

void* /*delegate's context*/ arg);


What I want is:

int entrypoint(scope void delegate() func)
{
return fakeEntrypoint(
// for some reason func.funcptr is void 
function()

cast(void function(void*))func.funcptr,
func.ptr);
}

but that fails with

cannot pass argument cast(void function(void*))func.funcptr of 
type
void function(void*) to parameter extern (C++) void 
function(void*) func


It's clear that your delegate is not extern C++


int entrypoint(scope extern(C++) void delegate() func)


Re: alias fails to compile

2019-04-22 Thread Alex via Digitalmars-d-learn
On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran 
wrote:

What am I doing wrong here?

struct A
{
union B
{
int bb;
}
B b;
alias aa = b.bb;
}

void main()
{
A a = A();
// a.b.bb = 4; // works
a.aa = 4; // fails
}


https://run.dlang.io/is/kXaVy2


aa is a "static" semantic.

your statement a.aa does not get translated in to a.b.bb like you 
think it does.


You are doing nothing any different than A.aa = 4.

which, when you realize this you'll understand the "need this" 
error.



It's as if you are doing


struct A
{
union B
{
int bb;
}
B b;
}

 alias aa = A.b.bb;

aa = 4;

which is like trying to say

A.b.bb = 4;


In D we can access "static" stuff using the object and so this 
can make things look like the mean something they are not.


If you could magically pass the this to it using something like 
UFCS then it could work.


import std.stdio;
struct A
{
union B
{
int bb;
}
B b;
alias aa = (ref typeof(this) a) { return  };
}

void main()
{
A a = A();
a.b.bb = 4;
a.aa(a).bb = 5;
writeln(a.aa(a).bb);
}

But here a.aa is not aa(a) and UFCS does not work so one must 
first access the alias and then pass the this.


Basically you are not going to get it to work. Just not how the 
semantics works.


It would be nice if a.aa(a) could reduce to a.aa as is UFCS was 
smart enough to realize it could pass it to the alias as this, 
but it doesn't.


else one might could do

alias aa = this.b.bb;

and

a.aa = 4;






Re: Inherit enum members

2019-04-21 Thread Alex via Digitalmars-d-learn

On Sunday, 21 April 2019 at 20:58:19 UTC, Andrey wrote:

Hello,
I have got 2 enums. How to inherit one enum from another?

enum Key : string
{
K1 = "qwerty",
K2 = "asdfgh"
}

enum ExtendedKey : Key
{
E1 = "q1",
E2 = "w2",
E3 = "e3"
}


Result:
onlineapp.d(27): Error: cannot implicitly convert expression 
"q1" of type string to Key
onlineapp.d(28): Error: cannot implicitly convert expression 
"w2" of type string to Key
onlineapp.d(29): Error: cannot implicitly convert expression 
"e3" of type string to Key


How to understand this?


I don't know why you want to do this, just use string directly.


enum ExtendedKey : typeof(EnumMembers!Key[0])
{
q = EnumMembers!Key[0]
}


would work..

Alternatively look at this thread:

https://forum.dlang.org/thread/irvtrixunermburvv...@forum.dlang.org?page=2

Where you can use the code and classes to subtype and emulate 
enums.








Re: How to debug long-lived D program memory usage?

2019-04-19 Thread Alex via Digitalmars-d-learn

On Friday, 19 April 2019 at 03:27:04 UTC, Adam D. Ruppe wrote:

On Friday, 19 April 2019 at 02:58:34 UTC, Alex wrote:

Curious, what are these programs?


A terminal emulator gui (like xterm), a detachable terminal 
emulator (like gnu screen), a slack client, an irc client, and 
a bunch of http servers including d doc search, a work program, 
and a personal utility.




Ok, nothing useful ;) I was thinking you might be doing stuff 
like running a security system that did computer vision, or some 
type of advanced house monitoring and control(voice activated 
doors or something) ;)


Could you not, as a quick fix, just reboot and automate 
restarting those?


Maybe design an auto-restart which saves the state, shuts itself 
down and then relaunches itself and loads the data?


This could be done as a fail safe when memory consumption get's 
too high.


All of them would show growing memory time, some worse than 
others. You can see a lot of difference in them - gui programs, 
terminal programs, network server programs. But, I did write it 
all myself, so it could be a mistake I keep on making.


So far, I basically tracked down the terminal emulator things 
to being inefficient scrollback storage. I made the structs 
smaller and limited the amount saved more than before and cut 
this by half. The ddoc search was keeping the index in memory, 
that's fixed, but it still shows growing usage over time. Of 
course, restarting that is trivial if need be, but still, I 
wanna make sure I am doing it right too - especially if it is 
one of my underlying libraries to blame.


Gonna have to be one of those things you track down because it 
could be something as simple as the GC or something more serious.



You might have hook in to the GC and just write out stats, I 
believe there is a stats collector somewhere though.


Yes, indeed. I am starting to make serious progress now - 
mostly the fault is me storing excessive histories 
inefficiently. Should have been obvious in retrospect, but I 
didn't realize just how much overhead there was in my designs!



D should have a very good memory statistics library built(I guess 
it has something with the switches)... since it should have no 
issues tracking memory usage.


Every memory allocation must have a corresponding deallocation 
for stable programs. All allocations and deallocations have 
specific file locations or occur in the GC.


I don't see why it would be difficult to monitor this stuff. As I 
mentioned, I generally never use new precisely so I can track 
this stuff myself and have a nice printout of memory usage when I 
need it and even verify the net memory allocation 0 zero on 
program exit. I haven't messed with the GC but I imagine it too 
shouldn't be hard to add the info too.





Re: How to debug long-lived D program memory usage?

2019-04-18 Thread Alex via Digitalmars-d-learn

On Wednesday, 17 April 2019 at 16:27:02 UTC, Adam D. Ruppe wrote:
D programs are a vital part of my home computer infrastructure. 
I run some 60 D processes at almost any time and have 
recently been running out of memory.


Each individual process eats ~30-100 MB, but that times 60 = 
trouble. They start off small, like 5 MB, and grow over weeks 
or months, so it isn't something I can easily isolate in a 
debugger after recompiling.


I'm pretty sure this is the result of wasteful code somewhere 
in my underlying libraries, but nothing is obviously jumping 
out at me in the code. So I want to look at some of my existing 
processes and see just what is responsible for this.


I tried attaching to one and `call gc_stats()` in gdb... and it 
segfaulted. Whoops.





I am willing to recompile and run again, though I need to 
actually use the programs, so if instrumenting makes them 
unusable it won't really help. Is there a magic --DRT- argument 
perhaps? Or some trick with gdb attaching to a running process 
I don't know?


What I'm hoping to do is get an idea of which line of code 
allocates the most that isn't subsequently freed.



Curious, what are these programs?

You might have hook in to the GC and just write out stats, I 
believe there is a stats collector somewhere though.


I did this by replacing new and monitored and calculated the 
allocations. This didn't help for any GC issues but at least made 
sure all my allocations were correct.


What you could do is something similar to this and just output 
stuff to a text file(that is written every so often).


If they programs are not too large you could used named 
allocations that could then be graphed individually(or use the 
file locations, __FILE__, etc).


Search and replace all new's and allocs with your custom ones and 
override the GC's.


Should give you a good idea of what's going on.


Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn

On Monday, 15 April 2019 at 20:36:09 UTC, Anton Fediushin wrote:

On Monday, 15 April 2019 at 14:20:57 UTC, Alex wrote:
On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin 
wrote:
Hello! I am currently trying to add a custom `toString` 
method to an enum so that:
1. Enum members would still have numeric values and can be 
easily compared (things like `enum a { foo = "FOO", bar = 
"BAR”}` won't do, I want `a.foo < a.bar)`)

2. More custom methods can be implemented in the future

Obvious solution is to wrap an enum in a structure and 
utilize 'alias this' for subtyping like this:

```
struct Enum {
  private {
enum internal {
  foo,
  bar
}
internal m_enum;
  }
  this(internal i) { m_enum = i; }
  alias m_enum this;
  string toString() {
// custom implementation of toString
  }
}
```

This seems to work just fine for assigning and comparisons 
but passing Enum as a function argument does not work:

```
void fun(Enum e) {}

fun(Enum.foo);
---
Error: function fun(Enum e) is not callable using argument 
types (internal)

Cannot pass argument foo of type internal to parameter Enum e.
```

Of course, I could just define a bunch of functions that 
accept my enum as the first argument and call them using UFCS 
but it'd require to explicitly specify functions instead of D 
taking care of that (toString() for structures is called 
automagically by functions like writeln) and those functions 
would hang around here and there totally unorganized. I 
prefer to keep functions inside of structures and classes.


If there are other ways of achieving the same *and* keeping 
code clean and organized, please share.


Thank you in advance,
Anton.


yes,

import std.stdio, std.meta, std.traits, std.conv;

enum _MyEnum : int { a,b,c}

struct _Enum(T)
{
   T value;
   alias value this;
   // generate static field members
   static foreach(e, v; EnumMembers!T)
   {
 pragma(msg, "static MyEnum "~to!string(v)~" = 
MyEnum(T."~to!string(v)~");");
 mixin("static MyEnum "~to!string(v)~" = 
cast(MyEnum)(T."~to!string(v)~");");

   }
}

alias _Enum!_MyEnum MyEnum;

void foo(MyEnum e)
{
   writeln(to!int(e));
}
void main()
{
foo(MyEnum.a);
foo(MyEnum.b);
foo(MyEnum.c);
}

https://run.dlang.io/is/WOcLrZ

Note that value is never used, it just makes the cast work and 
treats the struct as an enum. Not sure if there is a way 
around that.


Thank you, this is the solution I have been looking for!


Yes, it is quite nice. D should simply allow enums to act as 
structs, they are effectively the same.[meaning they should 
should allow methods having methods]


You might want to add a little more functionality. I screwed up 
the code when generalizing it, here is a bit better version:


https://run.dlang.io/is/yyuy77

Using the struct you can leverage all the power of op's such as 
dispatching, assignment, etc.


Essentially one is just using a standard struct but using enum's 
to define the names and values(since they are auto filled).


D should just do away with enums and allow one to create a enum 
struct, e.g.,


struct S
{
enum : int
{
   a,b,c
}
}

Of which enum S : int { a,b,c } is short hand when one does not 
want to add new functionality or D could just treat enum like a 
struct by allowing methods inside the enum.


The code I created essentially emulates this, one can make the 
enum internal and private to hide it.


Re: Transparent cast from class to member pointer?

2019-04-15 Thread Alex via Digitalmars-d-learn

On Monday, 15 April 2019 at 15:07:10 UTC, Robert M. Münch wrote:

On 2019-04-15 08:19:57 +, Ali ‡ehreli

Bingo, I didn't know that I can do an 'alias this' using a 
function and not only a type... pretty cool. So, with several 
of these I can setup implicit conversions to different types. 
Thanks.


Well, as I know, multiple alias this won't work yet...
https://wiki.dlang.org/DIP66


Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn

On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote:
Hello! I am currently trying to add a custom `toString` method 
to an enum so that:
1. Enum members would still have numeric values and can be 
easily compared (things like `enum a { foo = "FOO", bar = 
"BAR”}` won't do, I want `a.foo < a.bar)`)

2. More custom methods can be implemented in the future

Obvious solution is to wrap an enum in a structure and utilize 
'alias this' for subtyping like this:

```
struct Enum {
  private {
enum internal {
  foo,
  bar
}
internal m_enum;
  }
  this(internal i) { m_enum = i; }
  alias m_enum this;
  string toString() {
// custom implementation of toString
  }
}
```

This seems to work just fine for assigning and comparisons but 
passing Enum as a function argument does not work:

```
void fun(Enum e) {}

fun(Enum.foo);
---
Error: function fun(Enum e) is not callable using argument 
types (internal)

Cannot pass argument foo of type internal to parameter Enum e.
```

Of course, I could just define a bunch of functions that accept 
my enum as the first argument and call them using UFCS but it'd 
require to explicitly specify functions instead of D taking 
care of that (toString() for structures is called automagically 
by functions like writeln) and those functions would hang 
around here and there totally unorganized. I prefer to keep 
functions inside of structures and classes.


If there are other ways of achieving the same *and* keeping 
code clean and organized, please share.


Thank you in advance,
Anton.



Should have been

struct _Enum(T)
{
T value;
alias value this;
static foreach(e, v; EnumMembers!T)
		mixin("static T "~to!string(v)~" = 
cast(_Enum)(T."~to!string(v)~");");

}



Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn

On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote:
Hello! I am currently trying to add a custom `toString` method 
to an enum so that:
1. Enum members would still have numeric values and can be 
easily compared (things like `enum a { foo = "FOO", bar = 
"BAR”}` won't do, I want `a.foo < a.bar)`)

2. More custom methods can be implemented in the future

Obvious solution is to wrap an enum in a structure and utilize 
'alias this' for subtyping like this:

```
struct Enum {
  private {
enum internal {
  foo,
  bar
}
internal m_enum;
  }
  this(internal i) { m_enum = i; }
  alias m_enum this;
  string toString() {
// custom implementation of toString
  }
}
```

This seems to work just fine for assigning and comparisons but 
passing Enum as a function argument does not work:

```
void fun(Enum e) {}

fun(Enum.foo);
---
Error: function fun(Enum e) is not callable using argument 
types (internal)

Cannot pass argument foo of type internal to parameter Enum e.
```

Of course, I could just define a bunch of functions that accept 
my enum as the first argument and call them using UFCS but it'd 
require to explicitly specify functions instead of D taking 
care of that (toString() for structures is called automagically 
by functions like writeln) and those functions would hang 
around here and there totally unorganized. I prefer to keep 
functions inside of structures and classes.


If there are other ways of achieving the same *and* keeping 
code clean and organized, please share.


Thank you in advance,
Anton.


yes,

import std.stdio, std.meta, std.traits, std.conv;

enum _MyEnum : int { a,b,c}

struct _Enum(T)
{
   T value;
   alias value this;
   // generate static field members
   static foreach(e, v; EnumMembers!T)
   {
 pragma(msg, "static MyEnum "~to!string(v)~" = 
MyEnum(T."~to!string(v)~");");
 mixin("static MyEnum "~to!string(v)~" = 
cast(MyEnum)(T."~to!string(v)~");");

   }
}

alias _Enum!_MyEnum MyEnum;

void foo(MyEnum e)
{
   writeln(to!int(e));
}
void main()
{
foo(MyEnum.a);
foo(MyEnum.b);
foo(MyEnum.c);
}

https://run.dlang.io/is/WOcLrZ

Note that value is never used, it just makes the cast work and 
treats the struct as an enum. Not sure if there is a way around 
that.








Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn

On Monday, 15 April 2019 at 13:38:33 UTC, Anton Fediushin wrote:


This does work unless I want to use it like this:
```
fun(Enum.foo);
---
Error: function fun(Enum e) is not callable using argument 
types (internal)

cannot pass argument foo of type internal to parameter Enum e
```


This is correct. And you enforced this behavior as you said

Enum.internal is private to make it inaccessible from any other 
place. All I want is a way to have > an enum that I could 
extend with my own methods.


So... either you pass the internals of Enum somewhere, or you 
don't...


Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn

On Monday, 15 April 2019 at 10:15:50 UTC, Anton Fediushin wrote:

On Monday, 15 April 2019 at 10:00:36 UTC, Alex wrote:

Enum.internal is private to make it inaccessible from any other 
place. All I want is a way to have an enum that I could extend 
with my own methods.


Something to make the following code work:
```
Enum a = Enum.foo;
Enum b = Enum.bar;
assert(a == Enum.foo);
assert(a < b);
assert(a.toString == "FOO");
assert(b.toString == "BAR");
writeln(a); // FOO
writeln(b); // BAR
```


This would:

´´´

struct Enum {
  private {
enum internal {
  foo,
  bar
}
internal m_enum;
  }
  this(internal i) { m_enum = i; }
  alias m_enum this;
  string toString() {
if(m_enum == internal.foo)
return "FOO";
else
return "BAR";
  }
}

void fun(Enum e) {}


void main(){
import std.stdio;
fun(Enum.init);
Enum a = Enum.foo;
Enum b = Enum.bar;
assert(a == Enum.foo);
assert(a < b);
assert(a.toString == "FOO");
assert(b.toString == "BAR");
writeln(a); // FOO
writeln(b); // BAR
}
´´´

Assuming, that automatic generation of "FOO" from foo was not 
part of your question :-p


Re: Subtyping of an enum

2019-04-15 Thread Alex via Digitalmars-d-learn

On Monday, 15 April 2019 at 08:39:24 UTC, Anton Fediushin wrote:
Hello! I am currently trying to add a custom `toString` method 
to an enum so that:
1. Enum members would still have numeric values and can be 
easily compared (things like `enum a { foo = "FOO", bar = 
"BAR”}` won't do, I want `a.foo < a.bar)`)

2. More custom methods can be implemented in the future

Obvious solution is to wrap an enum in a structure and utilize 
'alias this' for subtyping like this:

```
struct Enum {
  private {
enum internal {
  foo,
  bar
}
internal m_enum;
  }
  this(internal i) { m_enum = i; }
  alias m_enum this;
  string toString() {
// custom implementation of toString
  }
}
```

This seems to work just fine for assigning and comparisons but 
passing Enum as a function argument does not work:

```
void fun(Enum e) {}

fun(Enum.foo);
---
Error: function fun(Enum e) is not callable using argument 
types (internal)

Cannot pass argument foo of type internal to parameter Enum e.
```

Of course, I could just define a bunch of functions that accept 
my enum as the first argument and call them using UFCS but it'd 
require to explicitly specify functions instead of D taking 
care of that (toString() for structures is called automagically 
by functions like writeln) and those functions would hang 
around here and there totally unorganized. I prefer to keep 
functions inside of structures and classes.


Isn't this a reason for passing your internals encapsulated, like

´´´
Enum EnumInstance = Enum(Enum.internal.foo);
fun(EnumInstance);
´´´

?



If there are other ways of achieving the same *and* keeping 
code clean and organized, please share.


Thank you in advance,
Anton.


Otherwise, you could alwas define fun as

´´´
void fun(Enum.internal e) {}
´´´

but I assume, you want to avoid especially this.

In favor of my first proposition, also speaks the fact, that 
Enum.foo is somewhat awkward w.r.t. your question, as you treat 
the internal enum as a static member. Was this intended?




Re: bug in compiles?

2019-04-12 Thread Alex via Digitalmars-d-learn

On Friday, 12 April 2019 at 09:43:01 UTC, Jacob Carlborg wrote:

On 2019-04-11 20:13, Alex wrote:

The following code works when I comment out the static if

//static if (__traits(compiles, __traits(getAttributes, T)))
    static foreach(a;  __traits(getAttributes, T)) Attributes 
~=



There seems to be absolutely no reason why this code would 
fail with the static if but pass without it but in the first 
case I get no attributes because the __traits compiles fails.




__traits(compiles, __traits(getAttributes, T))

vs

__traits(getAttributes, T)

How could it not compile in the first case and yet work in the 
foreach?


The problem is that "__traits(getAttributes, T)" in it self is 
not valid code. It needs to be part of larger expression or 
statement. The following seems to work:


https://run.dlang.io/is/JWkdbQ



So this suggests that the error is due to an expression, I 
thought compiles could take expressions ;/


onlineapp.d(17): Error: tuple(3) has no effect
3-int

@(3) int a;
__traits(getAttributes, a);

I use the same semantics all over the place and it seems to work:

static if (__traits(compiles, __traits(isTemplate, T)))


The same issue will happen, but I believe static if works here 
without issue. If so this means that it is a bug. Maybe compiles 
can have handle certain primitives(such as bools here) but not 
others(such as tuples in the main case)?


It would make little sense to be forced to use a temp variable:

static if (__traits(compiles, auto x = __traits(isTemplate, T)))

(after all, a ; is not required either)


In fact, though:

Returns a bool true if all of the arguments compile (are 
semantically correct). The arguments can be symbols, types, or 
expressions that are syntactically correct. The arguments cannot 
be statements or declarations.


If there are no arguments, the result is false.

import std.stdio;

struct S
{
static int s1;
int s2;
}

int foo();
int bar();

void main()
{
writeln(__traits(compiles));  // false
writeln(__traits(compiles, foo)); // true
writeln(__traits(compiles, foo + 1)); // true
writeln(__traits(compiles,  + 1));// false
writeln(__traits(compiles, typeof(1)));   // true
writeln(__traits(compiles, S.s1));// true
writeln(__traits(compiles, S.s3));// false
writeln(__traits(compiles, 1,2,3,int,long,std));  // true
writeln(__traits(compiles, 3[1]));// false
writeln(__traits(compiles, 1,2,3,int,long,3[1])); // false


So ultimately it suggests a bug in the compiler.











Re: bug in compiles?

2019-04-11 Thread Alex via Digitalmars-d-learn

On Friday, 12 April 2019 at 00:02:36 UTC, Seb wrote:

On Thursday, 11 April 2019 at 23:55:18 UTC, Alex wrote:

to judge people objectively. This isn't a nursery school and 
we are not 3 year olds...


Exactly. So start behaving like a grown-up and professional.

When you ask someone for help on the street, do you curse at 
him too?


Um, where the hell did I "curse" at him? See, you are making up 
shit. Prove it! The posts are recorded!


See, you do want this to a nursery school... You want your little 
rules and regulations and no cursing or you'll get soap in your 
mouth.



You want to act like a grown up but you don't wanna be a grown 
up. Being a grown up sometimes requires you to not respond to 
people that you don't like. Yet you did! In the real world when 
someone says shit you don't like and you say shit back and then 
claim your in the right when you do the exact same shit doesn't 
cut it.


See, you decided to become part of this problem... because you 
are part of the problem.  You had nothing useful to say, nothing 
factual, nothing to add intelligently but you just decided on 
your own that you wanted to take sides play these little games.



"Must be something else in your code because it works fine for me 
with this shortened example.


https://run.dlang.io/is/o8xESB


Seriously? Do you think you have ESP? Your code isn't even close 
to was talking about ;/


Here is is updated that shows the error. You seem to fail to 
understand that it is impossible for it to be my code. There is 
no way the static if should fail yet the static foreach pass. 
They both go together, that is is the whole point of the static 
if, to make sure the foreach works, yet it says it doesn't work 
when it in fact does(as commenting out the line proves)."


I asked him if he thinks he has ESP, was that cursing him? I told 
him his code isn't close to what I was talking about, is that 
cursing?


I'm asking a damn question and making a statement. I was trying 
to explain to him that there is no way in hell that he could be 
right(well, there is a small chances but why would he assume 
that?) because he of the nature of the problem.


No where did I cuss him, attack him personally... it's something 
you guys decided to interpret as then you get offended and cause 
problems and you don't like me telling you to fuck off for saying 
I'm causing problems when you are causing problems.


See, you guys are the source. You think your job is to police the 
world because you are special. If Bauss got offended or didn't 
like my town then he can chose to not respond to me ever again, 
that solve that problem.


See, if I'm so fucking hostile to everyone and everyone never 
responds to me, guess the fuck what? No one responds, no 
problems. At most I could flood the site with useless posts or 
some shit like that(which I wouldn't do)..


But instead, what happens is people like you that think you are 
do-gooders and you are going to save the world from ilk like me 
only make things worse. That is typical of people like you(which 
I say because you have proven you are that type of person by 
responding rather than just ignoring). At least if you had 
something useful to say then maybe it would matter.


Instead, you say I am slinging poop and that gives you the right 
to sling poop back... but you also claim you are on the moral 
high ground.


Now, that is my assessment of the situation. I claim it is based 
on facts. You can object, but you have the right to just STFU and 
let other people STFU. But I imagine you will want to continue 
being part of the problem.


Seriously, what do you think will come out of your comments? They 
have no substance that can possibly have any good outcome. It's 
just like the shit I have said here, except my goal is to get you 
to realize that you are not contributing to the solution. Same as 
Parker with his need to police the the world. Do you really think 
it will make things better?











Re: bug in compiles?

2019-04-11 Thread Alex via Digitalmars-d-learn

On Thursday, 11 April 2019 at 23:04:46 UTC, Mike Parker wrote:

On Thursday, 11 April 2019 at 22:41:32 UTC, Alex wrote:



Seriously? Do you think you have ESP? Your code isn't even 
close to was talking about ;/


Here is is updated that shows the error. You seem to fail to 
understand that it is impossible for it to be my code.


If you continue to attack people who are trying to help you, 
the moderators will stop approving your posts. The hostility is 
unwelcome here. If the number of your posts that have not been 
approved didn’t serve as a warning, then this does.



Quick google search to help you with your problem:

https://www.quora.com/How-do-I-develop-a-thick-skin-and-stop-getting-offended-over-everything

https://www.arkansasonline.com/news/2019/feb/05/too-thin-skin-20190205/

https://medium.com/the-mission/welcome-to-the-thin-skinned-generation-where-everything-is-a-bloody-problem-8bd5cb336361

http://dare.wisc.edu/survey-results/1965-1970/emotional-states-and-attitudes/gg8

http://meanttobehappy.com/10-ways-you-too-can-stop-being-so-easily-offended/


You can't police the world. You seem to live in a PC fantasy 
world where where every little booboo is treated as a life and 
death situation. You are not helping with the world with your 
need to control it. What are you going to do when things get 
really bad in the world? Have a nervous break down? If you think 
the shit I say is hostile and attacking people you really have no 
clue what the real world is like. You seem to have control issues 
that you should get addressed if you are going to threaten people 
over moronic statements that you interpret with your bias to be 
hostile. I'm sorry, but I'm not going to play your games. Did you 
go after FeepingCreature? He was clearly more hostile than I was 
but I bet you didn't say a god damn thing to him... You want me 
to stop being "hostile" then you need to throw your badge in the 
trash and stop trying to police every conversation because you 
really are in no position to judge people objectively. This isn't 
a nursery school and we are not 3 year olds... and if you want to 
turn it in to that kinda atmosphere then you kiss my ass.






Re: bug in compiles?

2019-04-11 Thread Alex via Digitalmars-d-learn
On Thursday, 11 April 2019 at 19:42:05 UTC, Steven Schveighoffer 
wrote:

On 4/11/19 2:13 PM, Alex wrote:

The following code works when I comment out the static if

//static if (__traits(compiles, __traits(getAttributes, T)))
    static foreach(a;  __traits(getAttributes, T)) Attributes 
~=



There seems to be absolutely no reason why this code would 
fail with the static if but pass without it but in the first 
case I get no attributes because the __traits compiles fails.




__traits(compiles, __traits(getAttributes, T))

vs

__traits(getAttributes, T)

How could it not compile in the first case and yet work in the 
foreach?


It should. Do you have a larger example? One that can be played 
with?


-Steve


import std.stdio, std.traits, std.conv;


struct A(alias T)
{
static void foo()
{
		static if (__traits(compiles, __traits(getAttributes, T))) // 
Failing for some reason
		static foreach(a;  __traits(getAttributes, T)) pragma(msg, 
to!string(a), "-", typeof(a).stringof);

}
}

void main()
{
@(3) int a;

(A!a).foo();
}

Commenting out the static if allows the foreach to pass and 
everything works. The whole point of the static if is to let the 
foreach work.


Both are only dependent on

__traits(getAttributes, T)

So if that can't compile then how could the foreach work?

https://run.dlang.io/is/WlXCIZ




Re: bug in compiles?

2019-04-11 Thread Alex via Digitalmars-d-learn

On Thursday, 11 April 2019 at 20:49:45 UTC, bauss wrote:

On Thursday, 11 April 2019 at 18:13:48 UTC, Alex wrote:

The following code works when I comment out the static if

//static if (__traits(compiles, __traits(getAttributes, T)))
   static foreach(a;  __traits(getAttributes, T)) Attributes ~=


There seems to be absolutely no reason why this code would 
fail with the static if but pass without it but in the first 
case I get no attributes because the __traits compiles fails.




__traits(compiles, __traits(getAttributes, T))

vs

__traits(getAttributes, T)

How could it not compile in the first case and yet work in the 
foreach?


T is a local variable name passed to this code, which does 
generally return an

error when used in certain ways:


int x;

Code!x; // Basically the code at the top

if I try to do too much with x I get `cannot use local `x` as 
a parameter to a non-global template`. Usually when I try to 
pass T to another template I get this error.



But some things work. But I can't for the life of me 
understand why compiles is failing but the foreach loop is 
working.



The problem is that I am using compiles quite often and either 
I'm misusing it or it's got some bugs in it that worry me 
because maybe they are silently failing?


Note that I get no errors in my code and I do get the 
attributes so it is technically working.


It would be really nice to know why the compiles is failing, 
e.g.,


pragma(compilesmsg, __traits(getAttributes, T)) spits out the 
error.


Must be something else in your code because it works fine for 
me with this shortened example.


https://run.dlang.io/is/o8xESB


Seriously? Do you think you have ESP? Your code isn't even close 
to was talking about ;/


Here is is updated that shows the error. You seem to fail to 
understand that it is impossible for it to be my code. There is 
no way the static if should fail yet the static foreach pass. 
They both go together, that is is the whole point of the static 
if, to make sure the foreach works, yet it says it doesn't work 
when it in fact does(as commenting out the line proves).




import std.stdio, std.traits, std.conv;


struct A(alias T)
{
static void foo()
{
		static if (__traits(compiles, __traits(getAttributes, T))) // 
Failing for some reason
		static foreach(a;  __traits(getAttributes, T)) pragma(msg, 
to!string(a), "-", typeof(a).stringof);

}
}

void main()
{
@(3) int a;

(A!a).foo();


}



bug in compiles?

2019-04-11 Thread Alex via Digitalmars-d-learn

The following code works when I comment out the static if

//static if (__traits(compiles, __traits(getAttributes, T)))
   static foreach(a;  __traits(getAttributes, T)) Attributes ~=


There seems to be absolutely no reason why this code would fail 
with the static if but pass without it but in the first case I 
get no attributes because the __traits compiles fails.




__traits(compiles, __traits(getAttributes, T))

vs

__traits(getAttributes, T)

How could it not compile in the first case and yet work in the 
foreach?


T is a local variable name passed to this code, which does 
generally return an

error when used in certain ways:


int x;

Code!x; // Basically the code at the top

if I try to do too much with x I get `cannot use local `x` as a 
parameter to a non-global template`. Usually when I try to pass T 
to another template I get this error.



But some things work. But I can't for the life of me understand 
why compiles is failing but the foreach loop is working.



The problem is that I am using compiles quite often and either 
I'm misusing it or it's got some bugs in it that worry me because 
maybe they are silently failing?


Note that I get no errors in my code and I do get the attributes 
so it is technically working.


It would be really nice to know why the compiles is failing, e.g.,

pragma(compilesmsg, __traits(getAttributes, T)) spits out the 
error.








Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-10 Thread Alex via Digitalmars-d-learn

On Tuesday, 9 April 2019 at 21:00:55 UTC, H. S. Teoh wrote:
On Tue, Apr 09, 2019 at 08:49:17PM +, Alex via 
Digitalmars-d-learn wrote:

On Tuesday, 9 April 2019 at 18:56:58 UTC, H. S. Teoh wrote:

[...]
My point has been and is that there is a better way that is 
more natural.  I make no claims about anything else. It may be 
a cop out to say something that is a tautology but I make the 
claim as an emphasis that I believe that it is more true in 
this case than others. i.e., D's traits are PITA. Ok, they are 
not as bad as some things but I know they can be better since 
I have used other languages that have far more logical 
reflection(such as C#) than D.


As I said, __traits was never designed to be user-friendly, or 
even "logical".  It was supposed to be a quick-and-easy way to 
tap into compiler internals, and std.traits was supposed to be 
the proper API to introspection.  But std.traits, as it stands, 
is far, far from what it ought to be, and so we have today's 
sorry situation.


That's unfortunate... but at least they are generally pretty 
functional. I remember trying to do basic type comparisons in 
C#'s reflection and didn't have the capability(I think it got it 
later but I believe that was the time I left for D).


I can understanding adding traits int he way it was, but at some 
point it has to be brought to the same standard as the rest of 
the language. It's just far too important to neglect. After all, 
Even most of phobos is using meta programming.


(And on a side note: don't even get me started on is(...) 
expressions.)


Anyway, since you seem to be passionate about this, this could 
be your chance of making std.traits what it ought to have been 
all along, or making a sensible reflection library that can for 
all practical purposes replace std.traits.




The problem is, I believe to do it right, it should be done in 
the compiler(at least some changes) and I know virtually nothing 
about the design and internals, so it would be much more time 
consuming than I'd like). I feel if I were going to go down that 
route I'd rather spend time writing a compiler or getting better 
as functional programming.


I, for one thing, would appreciate a nicer API to introspection 
than what we currently have.  One of the main things that drew 
me to D was its metaprogramming capabilities, and while overall 
it has been a positive experience, there *are* flies in the 
ointment like __traits, is(...) expressions, etc..  If you or 
somebody else could come up with a saner way to do 
introspection in D, it'd be very much appreciated.


Basically same here. D is one of those love/hate relationships ;/ 
Meta programming is the next stage of evolution of programming. 
It is necessary for simplifying the complex problems of the 
modern computing world. D does have quite a nice syntax for most 
of this and does reasonably well(Although I believe functional 
programming blows D's type system out the water since it deals 
with structural mapping directly without "special cases").








Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-10 Thread Alex via Digitalmars-d-learn

On Wednesday, 10 April 2019 at 02:19:42 UTC, Adam D. Ruppe wrote:

On Tuesday, 9 April 2019 at 20:45:18 UTC, Alex wrote:

On Tuesday, 9 April 2019 at 18:33:21 UTC, Seb wrote:

Have you considered writing a DIP?


No, because I expect it won't even be considered.


You won't pass review if you don't show knowledge of the 
existing language, but there's a lot of people who are 
interested in a few changes.


A few years ago, there was a proposal for a magical `meta` 
namespace that would call into the compiler for syntax sugar on 
CT reflection. The old idea was to replace `__traits(X, args)` 
with `meta.X(args)` - pure syntax change - but you could 
perhaps revive and expand that as the entry point to your new 
idea and get some people on board.


Yeah, but that isn't really effective, virtually identical. I am 
talking about one level higher abstraction. I mean, it's all the 
same but I'd like things to be sorta based in oop and ranges(UFCS 
like stuff). I haven't though about it enough to know how it 
would work out but I imagine things could be done very nicely. 
E.g., some accessors get information and some process that 
information and one can combine and match as needed. X.Type.name, 
X.Members.Select!"foo".Memebers.Select!"bar"(assuming foo is an 
aggregate), etc.


Range functionality could even be used to process arrays(I guess 
the return of the traits would need to be ranges).


I'm skeptical of any reflection library, but compiler changes 
might be able to change that. The status quo for D's reflection 
libraries are:


1) weird bugs and/or omissions since the language does not let 
you express all the function parameter details as return values 
or local variables.


2) slow compile times (you said your thing was 10 seconds! 
that's utterly unacceptable)




It actually is not to bad. I haven't done much testing but at 
least repeatedly calling the Reflect on the same base type added 
little overhead(it wasn't scaling linearly with the number of 
calls). So maybe it has a chance. Still will never be as 
efficient as doing it internally though.


3) not actually significantly easier to use than language 
built-ins (if they are actually easier to use at all!)



So any library without compiler changes is, as of today, I 
believe *impossible* to get right, and getting as close as you 
can is horribly slow. So, your DIP would necessarily include 
language+compiler changes.


And the compiler is *already* (almost) fully capable, so 
changing that needs to show that the change is worth it. Maybe 
you can do that, even little cosmetic things can indeed be a 
win, but to write a convincing case here, you'll need to 
compare and contrast various cases.


I'd definitely rather see this in the compiler. I just no zero 
about Dmd's design and the specifics. It's not something I'm 
capable of doing without a significant investment that I'm not 
willing to do(only if I could add the design on top without 
having to "learn" everything about the compiler).



My goal was to sort of bridge the gap of the ideal solution and 
what we have. Something I could use personally to solve most of 
the frustrations I have but I wasn't thinking of trying to make 
it in to something ideal.




Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-10 Thread Alex via Digitalmars-d-learn

On Wednesday, 10 April 2019 at 14:06:53 UTC, Adam D. Ruppe wrote:

On Wednesday, 10 April 2019 at 10:18:35 UTC, H. S. Teoh wrote:

[...]


There's a little bit weird about it, but it makes sense.

I used to find it hard to even remember how it works, but then 
I had to describe it for the book and it all came together. 
Consider this.


[...]


I wonder if there are some interesting patterns of nesting is's?

is(...is(...is(...)...)...)







Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-09 Thread Alex via Digitalmars-d-learn

On Tuesday, 9 April 2019 at 18:56:58 UTC, H. S. Teoh wrote:
On Tue, Apr 09, 2019 at 06:33:21PM +, Seb via 
Digitalmars-d-learn wrote:

On Tuesday, 9 April 2019 at 16:30:53 UTC, Alex wrote:
> On Tuesday, 9 April 2019 at 14:59:03 UTC, Adam D. Ruppe 
> wrote:

> > [...]
> I didn't say the language. The point with the language is 
> that it could have built in semantics to do reflection in a 
> inform way(__traits is somewhat uniform but messy and then 
> one has std.traits which then makes it not uniform).
> 
> [...]


Have you considered writing a DIP?


I think the original intent was that __traits was supposed to 
be low-level, implementation-specific calls into the compiler, 
while std.traits is supposed to be the user-friendly syntactic 
sugar built on top that user code is supposed to use.


Unfortunately, it turned out that people prefer using __traits 
directly instead, and std.traits hasn't quite been keeping up 
with new __traits (e.g., AFAIK __traits(compiles) does not have 
a std.traits equivalent), and is also less familiar to many 
people, so now we have a mishmash of code sometimes using 
__traits and sometimes std.traits.




That may be the case but I recently saw some one here say "All 
the roads to hell are paved with good intentions", which isn't 
hyperbole. The better the initial plan the better things will 
turn out. Planning is the key to success, without it there is 
only luck.


My point has been and is that there is a better way that is more 
natural.  I make no claims about anything else. It may be a cop 
out to say something that is a tautology but I make the claim as 
an emphasis that I believe that it is more true in this case than 
others. i.e., D's traits are PITA. Ok, they are not as bad as 
some things but I know they can be better since I have used other 
languages that have far more logical reflection(such as C#) than 
D.


Because D has such an extensive meta programming language(the 
best I've seen for procedural) it needs a well thought out and 
natural reflection scheme. traits works but the more I use it the 
more I believe it could have been done better. It's one thing to 
use it here and there, but when you use it everywhere the issues 
multiply.


I think the library I through together at least demonstrates a 
much more natural way(and it was just a first attempt without any 
planning). I imagine something much better could be created with 
some proper planning and teamwork. My impression though is few 
people here actually care about such things.









Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-09 Thread Alex via Digitalmars-d-learn

On Tuesday, 9 April 2019 at 18:33:21 UTC, Seb wrote:

On Tuesday, 9 April 2019 at 16:30:53 UTC, Alex wrote:

On Tuesday, 9 April 2019 at 14:59:03 UTC, Adam D. Ruppe wrote:

[...]
I didn't say the language. The point with the language is that 
it could have built in semantics to do reflection in a inform 
way(__traits is somewhat uniform but messy and then one has 
std.traits which then makes it not uniform).


[...]


Have you considered writing a DIP?


No, because I expect it won't even be considered. The typical 
responses I have seen to this sort of thing is that "we already 
have it". Hence why I decided to write my own lib, and then had a 
few issues.


Andre did look in to some of the ideas and said he would pass it 
off to some GSOC students to look in to making it better, so 
maybe something will come out of that.


I believe that basically the compiler simply needs to present a 
better interface in to reflection. This should have been done 
from the get go but I guess __traits has been patched together 
over time as as things were needed.


Essentially the compiler would need to do what I have done and 
wrap __traits and std.traits in to a more uniform interface. It 
would add an extra layer of indirection though since the wrapper 
needs then to stay consistent with any traits changes.



My library works well but is incomplete and has some bugs to due 
bugs in traits. It is, though, rather slow and I haven't worked 
on fixing it. (actually it doesn't seem to add too much overhead 
after the first call but I imagine that is due to the template 
being instantiated already in the module, might multiply for each 
new module it is used in)



What I'm thinking is that maybe it is better to have some range 
like semantics used for reflection. I have thought about it much 
but I got the idea earlier.


Type.Reflect.Accessor.Accessor.Selector.Selector.Accessor...

The idea is that one can access stuff from the type and then use 
certain keywords to select and filter and then access more.


I say it is range like but I'm not sure if ranges would work or 
not or if it would just be dispatching.


E.g.,

MyClass.Reflect.TemplateParameters.Select!int.Names

Would get the template parameters of MyClass, then select only 
those with int and then get the names of those parameters.


(this could work in general for runtime too)

MyClass.SomeArray.Select("x.value == 4").name;

which is why I say it is sorta like ranges(which I'm thinking 
more of piping).



I just haven't thought about it much and there doesn't seem to be 
much interest in going this route... I think it would be a great 
addition to D but others seem to not care or are more interested 
in arguing to relatively insignificant issues.







Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-09 Thread Alex via Digitalmars-d-learn

On Tuesday, 9 April 2019 at 14:59:03 UTC, Adam D. Ruppe wrote:

On Tuesday, 9 April 2019 at 14:42:38 UTC, Alex wrote:

It basically proves my point that there are issues with D.


The language is fine in this case, it is a bug in the library.

I didn't say the language. The point with the language is that it 
could have built in semantics to do reflection in a inform 
way(__traits is somewhat uniform but messy and then one has 
std.traits which then makes it not uniform).


Though, I don't think the library can be fixed because the 
language doesn't have facilities to express these things 
through libraries but I also don't see the point in having 
a library at all. You incur compile time and run time costs for 
stuff you probably don't need and introduce another layer of 
bugs.


There is no runtime costs. The library may not be efficient but 
it is a better design. It abstracts most of the crap away and 
provides a very natural and uniform interface in to reflection.


Ultimately the compiler itself should provide this information 
directly. It has all the information when it compiles and it can 
lazily instantiate whatever information it has to calculate when 
it is requested. It could be very efficient, it is just doing 
what __traits does but more naturally.


Reflection should be a first class semantic in D given D's heavy 
meta programming capabilities.




Some of the lower level features are a little weird to see*, 
but they are easy to use once you get to know them.


* for example
   static if(is(typeof(overload) Params == __parameters))

to get the function parameter list (as the Params variable 
there). It is a little weird looking, but it isn't really hard 
to use.



The problem is that there is a semantic that supposedly exists to 
dot he same but fails. So it is not about weirdness but about 
non-uniformity. Why does it have to be that way? There is no 
reason, it is also a hack in the compiler. WHy not just wrap that 
code with something sane like


Type.Reflect!Parameters

?

This is exactly what my library does and it is a more sane 
looking solution. It's that simple. You can't disagree, you know 
it looks better(we could argue about the precise syntax but it is 
much better and more informative than what you posted).


Also, your snippet is meant to be used, later on, such as in a 
for each loop,


when we could do

static foreach(p; Type.Reflect!Parameters)

vs

static if(is(typeof(overload) Params == __parameters))
static foreach(p; Params)


It's 50% savings in both line space and character space, it's 
much faster to understand, it is uniform and natural while the 
second case is ugly, confusing, and bloated.



My point is that THERE IS NO NEED for such things. It is ALL 
about syntax. It's just wrappers. There is no new functionality. 
I'm just trying to make things "look" better because things that 
"look" better are easier to comprehend. Look better means that 
they are more uniform with common practices and experiences and 
allow for easier navigation in memory space.


You may be ok with having to remember special cases here and 
their... but it is a defect.  Suppose you program in 10 languages 
regularly, those special cases then are an order of magnitude 
more to remember... suppose you do other things with your life. 
If you don't and all you do is program D then you don't feel it 
as bad, if you do lots of other things then these are speed bumps 
that slows one down for no good reason at all(if you have one, 
besides, "some one has to code the new syntax and it takes work 
to make it better" then I'd like to hear it).


What I see if you justifying bad syntax just to justify it 
because it exists. If I'm wrong you just have to give me 
meaningful reasons why that is not the case.


I'm not looking to be confrontational, I'm looking to make things 
better. If you are right about what you claim then you can back 
it up, if not you can't except with fallacious arguments. My mind 
is willing to be changed as long as the change is provably good, 
else there is no point in going backwards.



I think the only argument you have is "It already exists and 
works and is not too bad"... which may be a good enough argument 
for you but isn't for me. That type of argument can be used to 
prevent progress on anything. "The cave is already exists and 
works to keep us sheltered and is not that bad so we don't need 
to build a nice house with AC that takes work to do". If all you 
have ever lived in is a cave then you can't possibly know if the 
house is better. It has to be built first to compare and 
contrast. The secret is that there is always something better 
than something else and it always should be sought after, at 
least theoretically(sure there is the practical issue of time, I 
accept that, but I don't accept denial that their isn't something 
better).



Hell, as much time that we have spent arguing over this shit we 
could have probably created a language feature 

Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-09 Thread Alex via Digitalmars-d-learn

On Monday, 8 April 2019 at 21:52:59 UTC, Adam D. Ruppe wrote:

On Saturday, 6 April 2019 at 12:20:28 UTC, Alex wrote:
 Error: variable 
`std.traits.ParameterDefaults!(foo).Get!1u.Get` only 
parameters or stack based variables can be `inout`


so i think that is a bug in the phobos library

see: 
https://forum.dlang.org/thread/qbqvkcoexxtlvlxob...@forum.dlang.org



I appreciate you looking in to it instead of writing it off. It 
basically proves my point that there are issues with D. These 
issues tend too be subtle and creep in to code and then that 
causes problems. I have been thrown off several times and when 
things don't work out I assume it is my code and when it's not 
I've already trashed my code trying to figure out what was wrong 
with it. These are the real problems with D and they come from 
two things: Not having a uniform and direct interface for 
reflection. Not having proper unit testing done for traits.


D is so heavy in meta programming that maybe it is time for a 
proper solution?


Re: const of AliasSeq is silently ignored

2019-04-09 Thread Alex via Digitalmars-d-learn

On Monday, 8 April 2019 at 19:56:50 UTC, Yuxuan Shui wrote:


In this example:

const(AliasSeq!(int, int)) a;
pragma(msg, typeof(a)); // (int, int)

This kind of make sense, since AliasSeq is not a "single" type. 
But silently dropping const seems bad, the compiler should 
probably report an error/warning in this case?


kinda makes sense and making sense are two different things. It 
has to make sense to the compiler.


While I see that you want to distribute const over the list, D is 
not designed to do this with anything that I know of. It could, 
without issue, but one must makes sure it does not contradict any 
other uses. If it doesn't then it could be a bonus feature.


Normally though one expects const to work on it's argument and so 
this also suggests having a const AliasSeq. Since we can't have a 
const AliasSeq there may be no issue redefining it to me what you 
want.


I agree that silently dropping things are bad. D does this 
sometimes and it can be a real pain.







Re: Templates - What's Up with the template keyword?

2019-04-09 Thread Alex via Digitalmars-d-learn

On Monday, 8 April 2019 at 12:23:28 UTC, Ron Tarrant wrote:
I'm digging into templates in an attempt to understand the 
signals-n-slots replacement for the observer pattern, but I've 
got a question I can't seem to find an answer for and an 
example for which I'm unable to solve the error.


First, the question...



Your confusion arises in your understanding of meta programming 
and templates. Templates are compile time expressions that use 
parameters. They are exactly analogous to parameters in typical 
programming but allow one to pass around CT information that 
doesn't exist at run time.


It is not the statement itself that is a template but the fact 
that it depends on a "template", which is a sort of abstract 
variable.


class C(T)

template C(T)

struct(T)

interface(T)

void foo(T)()

etc...


are all things that depend on a template parameter T. One can say 
that they are all "templated" or are "templates".


One can also say they are parameterized(meaning they take a 
parameter, but the parameters are CT "objects".



Essentially template is the most general and the rest are just 
syntactic sugar.


e.g.,

template CC(T)
{
   class C
   {
  T t;
   }
}


template CC is a "template"(just like a cookie cutter but that 
can configure itself depending on what T is, as this example 
shows that we can create a class with different type of t.



D has all kinda of stuff like this that help reduce the bloat of 
templates. Sometimes you must use one method over another for 
certain effects but usually you can pick and choose.


The idea is not to think of meta programming as something 
different than normal programming. It is still programming and 
there is enough of an overlap that it benefits one to realize 
they are the same thing but are at different levels in the type 
system. Meta programming works on the type hierarchy while 
"runtime programming" works in the type hierarchy. Meta 
programming is a super set of runtime programming.



The way I think of meta programming is that of coding runtime 
code. Run time code codes binary code. If I create a statement 
like


writeln("test");

I know that the code gets translated in to a call instruction, 
pointers are used, "test" exists in some location in memory, 
etc... it all is machine code though when I compile.


When I do something like this

foo!int();

I know that the first part of foo!int is "meta code" and it first 
gets translated in to runtime code and then that code gets 
translated in to machine code.


For example

void foo(T)(T s) { writeln(s); }

in this case, depending on what T is at compile time(which I get 
to decide in some way and so I know exactly what it is at compile 
time, in theory), foo takes on different versions.


if I call foo(3) then it is writeln(3) and T is an int(by 
deduction in the compiler) and the compiler can even optimize out 
certain things here because it also knows that 3 is known.(this 
is CTFE) There is nothing to optimize in that example though.


if I call foo(readln()) then the the compiler cannot optimize out 
but this code get translated in to writeln(readlin()) but T is a 
string(since readln returns a string and the compiler can deduce 
it).


But in general foo acts as all these possibilities(and all the 
possibilities are known by the compiler because they have to be 
known to compile and use).


So, what does templates do? They combine very similar code in to 
meta blocks that can then be easily used. They allow constructing 
very complex meta code such as


void foo(T)(T s) { static if (is(T == string)) writeln(s); }


and now foo is much more complicated because it has two 
completely different behaviors depending on what T is. You can't 
get that with run time code without a lot of work and it will 
never be efficient. But here the compiler will eventually know 
what T is and it can then choose the correct path at compile 
time. The if will disappear and so no extra overhead exists at 
runtime.


So, templates are very powerful things(the above doesn't even 
dent their power) The idea to bear in mind is that anything in D 
is a template if it takes a template parameter. Think of a 
template as a drawing template used to sketch something. It isn't 
the final result but it shapes the final result. It is more like 
a blueprint or a program in and of itself.


I think the hard part for many is that they can't balance the 
meta programming part with the run time part. This is very easy 
though if one always just keeps track of which side one is 
programming in and not to mix them up(mentally).


The meta programming part will always be obvious, it will depend 
on template parameters and use meta programming constructs. There 
are sometimes overlap between the two levels but it because 
natural once one gets enough of an understanding.


The hardest part about D is that it has so much meta programming 
stuff and there are sometimes bugs and special cases to do 
things, but the grammatical 

Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-08 Thread Alex via Digitalmars-d-learn

On Monday, 8 April 2019 at 12:26:28 UTC, Adam D. Ruppe wrote:

On Sunday, 7 April 2019 at 17:42:58 UTC, Alex wrote:
That is blatantly wrong. The code works EXACTLY the same way 
with and without using stringof.


In some cases, yeah. In the general case, no.

Your import hack* is only there because of stringof. Using the 
local symbol, there is no need to import the module. This means 
you avoid name conflicts with local symbols (like I pointed out 
in the linked post) and it has the potential to avoid 
protection violation errors, since you are no longer getting 
the symbol from your module; it is gotten somewhere else, 
ideally from its own module, and passed to you, which is 
allowed even for private or hidden symbols.


It also just makes the code a lot simpler, so there's no more 
need to jump through hoops to debug it.


But I've already said the point of using T.stringof is so that 
one gets what T is and that actually helps debugging. You don't 
think I didn't try using T first? Why would I type extra symbols 
if it work exactly fine the first time?


The reason I added `~T.stringof~` which is 11 more chars than T.` 
was precisely because the code wasn't working and I was trying to 
figure out why. to claim that it's absolutely wrong in all cases 
is ignorant.


I realize that in many cases it is unnecessary and in some cases 
it will break the code(requiring an import because one is not 
using the type but the id which is then hidden)...


But as I said, I did a search and replace that it didn't change 
squat as far as the code. The main issue was using that typeof 
which you didn't catch cause you were too busy trying to make an 
issue out of stringof.


The other issues either remain, even after using your "rule", or 
I had to refactor the code and use different traits(such as the 
one you recommended for fields).



My point is that you are going ape shit over using T.stringof, 
you posted no real reasons why it is absolutely unnecessary yet 
claimed was after I already shown that it has some use.


It's one thing to make a suggestion and say "You know, you don't 
have to use T.stringof since it will function better in general, 
try it without it and see if fixes your problems" rather than 
keep on beating a dead horse.



* 
https://github.com/IncipientDesigns/Dlang_Reflect/blob/master/mReflect.d#L61


I have removed all T.stringof's in the code and I still get 
the exact same errors.


That's the first step. Then, you clean up the hacks that were 
introduced to work around stringof's problems and get a better 
design that actually works, like passing private members from 
one module to another via alias parameters.


I don't know what you are talking bout passing private members 
from one module using alias parameters. There is only one module 
that does the reflection and all the "passing" of private 
parameters are done INSIDE that module.


If you mean that I should pass the initial class as an alias, 
well, I tried that initially and it worked for some things but 
failed for others so went back and forth between using alias and 
types. The problem is because there are issues in D's type system 
and when they create hard to track bug one has to screw with shit 
to figure out what is going on and that introduces dead ends 
which may or may not be modify the design incipiently... which is 
the whole damn reason I'm writing the reflection library in the 
first place... to avoid all these issues  with traits and provide 
a common interface...


I mean, half the shit in __traits looks like it could be in 
std.traits and there is no clear reason why there are two aspects 
of D that do essentially the same thing. Also there are many 
"holes" in traits that require compound solutions(such as filter 
members to specific types) which then creates ugly CT 
code(multiple nested loops).







one could just open the source if there was anything to "hide"


That's not necessarily true, it is possible to have code 
exclude private members. In fact, it is often considered good 
practice to do that for library releases for encapsulation!


I'm not sure what you are talking about here. I assume you mean 
the body, which I'm not talking about. The body is irrelevant. 
You can't even get the body using __traits. I'm talking about if 
the symbol exists, private or not, one can see it in the source 
code since, for it to be marked private it must show up in the 
source. So, "private" does actually hide shit in the source code. 
It may hide the functionality/body/binary code, but marking a 
member private doesn't HIDE anything in the source.




Protection is a runtime thing(except for CTFE).


Protection *only* exists at compile time - at runtime, there's 
zero checks. There's nothing preventing you from passing a 
pointer to a private member, for example.


No, you really are missing the point. Protection is a RUNTIME 
thing. Else one would just make all members public. Protection 
exists at compile 

Re: What Does @ Mean?

2019-04-08 Thread Alex via Digitalmars-d-learn

On Monday, 8 April 2019 at 11:58:49 UTC, Ron Tarrant wrote:
This is frustrating and makes me feel like a complete newb. 
Worse, it's impossible to search for. Ever try Googling a 
single character?


The D documentation also doesn't seem to explain the meaning of 
this or any other token. Sure, most of them are obvious, but 
this one eludes me. All I can find is this: 
https://dlang.org/spec/lex.html#tokens


Would someone please tell me what an at sign (@) means when 
it's used like this:


bool isLeaf() @property
{
   return children.length == 0;
}

In fact, I have no idea what @ means anywhere other than in an 
email address. Is this a common thing in contemporary languages?


And while I'm asking, does an underscore have special meaning 
when used either at the beginning or end of a variable name?


How about a double underscore?

I know underscores are sort of left over from C, but do these 
characters have special meaning in D or are they simply a 
convention... like T in a template definition?


Some of the attributes in D go with an "@".

https://forum.dlang.org/post/fiwfcsqmjsndcjixi...@forum.dlang.org
https://wiki.dlang.org/Language_Designs_Explained
https://dlang.org/spec/attribute.html


Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-07 Thread Alex via Digitalmars-d-learn

On Sunday, 7 April 2019 at 15:35:46 UTC, FeepingCreature wrote:

On Sunday, 7 April 2019 at 03:47:25 UTC, Alex wrote:

rules are meant to be broken.


No they're not! Almost by definition not!

More comprehensively, if you break a rule you take 
responsibility for the outcome. You wanna use stringof? "Don't 
use stringof for that." "rules are meant to be broken." -- 
"Wah, my code doesn't work, stringof is shit, bad design, fix D 
pls." Don't ask questions if you don't care about the answers. 
More importantly, don't ignore advice and then complain about 
it.


You are a moron. Almost by definition?

Here is some advice, don't be a pscyhopath.  You come in to a 
thread without any desire to help in any way but to throw your 
2c's and pretend you have something useful to say.


You really are an idiot. The problems I faced had nothing to do 
with stringof. I did a search and replace and replaced ALL 
instances and the EXACT same code and issues were generated.


That alone proves you are a moron.



You can't expect to lead by authority. Give a good reason why 
I should avoid it and change my current ways and I will.


Your code will break in surprising and unfixable ways. 
`stringof` is not *for* mixin. If you use it for mixin, you 
will be on your own design-wise. The language will not be 
changed to "fix" the "bugs" you run into. It will work until it 
won't work, and then you'll have to rewrite the entire thing 
anyway.


Don't use stringof for mixin.


go screw yourself. Please don't EVER respond to me again. I will 
ignore ALL your replies. I don't give a shit about your "advice". 
You clearly have no idea what you are talking about but have some 
need to act like an authority.


I just hope you don't have a badge and a gun... People like you 
end up killing other people because they don't do shit the way 
you think they should.


I'm just wondering if your that Adam guy... kinda fishy you both 
show up spouting the same BS and don't even back up your claims 
and when I say prove your claim by an example you spout the same 
BS.


Morons make claims, experts prove them, neither of you have 
proven anything but you have made a lot of claims.


It amazes me people like you freely exist in the world.

Again, LEAVE ME ALONE! But I bet you won't! Psychopaths like 
yourself just can't shut the fuck up when told to go away.







Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-07 Thread Alex via Digitalmars-d-learn

On Sunday, 7 April 2019 at 15:26:47 UTC, Adam D. Ruppe wrote:

On Sunday, 7 April 2019 at 03:47:25 UTC, Alex wrote:
What you need to tell me is why using .stringof is bad. You 
have simply conjured up a rule and are stating it but not 
giving any reason why it is not a good idea to follow when, in 
fact, not following can be shown to be beneficial.


You can't expect to lead by authority. Give a good reason why 
I should avoid it and change my current ways and I will.


I have a couple times times now.

https://forum.dlang.org/post/nvpsrxxkfmbbxognj...@forum.dlang.org

https://forum.dlang.org/post/fahrmmocegtthztzg...@forum.dlang.org

I go into a lot of detail in the link on the second link.

And the pains you are personally experiencing are a direct 
result of stringof. If you were to actually following my rule, 
you'd learn by doing how much better it is.


That is blatantly wrong. The code works EXACTLY the same way with 
and without using stringof.


You have not stated one type the reason why T string of is bad, 
you have just stated that it is bad, that is totally different.


I have removed all T.stringof's in the code and I still get the 
exact same errors. Your assessment that all the errors are due to 
T.stringof is wrong.


You need to provide code where using T directly works but 
T.stringof fails.


I did a search and replace for all the T.stringof's and the code 
produced the exact same output, so to say that is the source of 
all my problems is BS, and since I have already fixed most of the 
problems(the main ones of inout and protection still exist) after 
that, it shows that it had nothing to do with it.


I'm not saying it is a good idea, what I'm saying is that you 
haven't yet given any proof why it is a bad idea. Those links are 
you just stating it is bad.


If you want this not to be a waste of both of our times, next 
time post an example code that demonstrates the problem.





So it seems like the same design flaw exists? Have any work 
arounds for that?


It won't work for private, you can use static if 
__traits(compiles) as a filter to get rid of them.


That doesn't help. It is compile time, one should be able to get 
the information. It is pointless to exclude it at CT because one 
could just open the source if there was anything to "hide". 
Protection is a runtime thing(except for CTFE).





Re: CTFE & code generators based on PEG grammars?

2019-04-07 Thread Alex via Digitalmars-d-learn

On Sunday, 7 April 2019 at 06:39:05 UTC, zabruk wrote:

On Sunday, 7 April 2019 at 03:32:45 UTC, Alex wrote:
just execute them at CT if possible(and the possibility simply 
is if the inputs are known at CT).


imho, Bastiaan Veelo want to say about citate above: not just 
"if possible", but "only if needed and possible"


True, but the only if needed is vacuous. As the meta parser does 
it's thing it will come across RT function usage and it can 
easily tell if the arguments are known at CT and then will 
evaluate them.


So, unless I'm mistaken about how the meta compiler works, it 
won't ever trigger CTFE on any CTFE'able functions that are not 
used.


The reason being is that any time a function call is made, the 
arguments must be known(including hidden/global arguments) and if 
they are determined at CT already then a CT call can and must be 
made(that is what makes CTFE what it is).


One can't CTFE if one doesn't have the inputs to the function... 
and the compiler doesn't just randomly compile RT functions 
without knowing the inputs... and the inputs are found at the use 
site.


I don't even thing the "only if needed" makes a lot of sense. The 
only time they are needed is exactly when they are needed, there 
is no other use cause in CTFE.



Can you give me a simple example that shows where a CTFE occurs 
that isn't needed and the compiler would try to CTFE it? By need, 
we mean with all optimizations, else CTFE is never needed.






  1   2   3   4   5   6   >