Logging Function Parameters

2018-03-17 Thread dom via Digitalmars-d-learn

Hi,

I am looking for a method to log the current function name + 
parameters.
Getting the name of the current function is simply possible with 
__PRETTY_FUNCTION__
Is there some possibility to generically access the parameters of 
a function such that they can be iterated and printed out?


currently i have something like this:
log.info(__PRETTY_FUNCTION__,  " ", parameter1, " ", parameter2);

i would like to get to something like that:
log.info(__PRETTY_FUNCTION__,  " ", parameters.join(", "));




Re: Removing array element in foreach, safe?

2016-09-05 Thread dom via Digitalmars-d-learn

On Monday, 5 September 2016 at 17:38:10 UTC, Daniel Kozak wrote:

On Monday, 5 September 2016 at 15:53:39 UTC, dom wrote:

is this code safe? if not how do i do it correctly?

static AsyncHttpGet[] openRequests;
static void updateRequests()
{
foreach(idx, req; openRequests)
{
if(req.state != Fiber.State.TERM)
req.call();
else
openRequests.remove(idx);
}
}

thx :)



openRequests = openRequests.filter!(a=>a.state != 
Fiber.State.TERM).array;

openRequests.each!((a){ a.call(); });


thx mate!


Re: delegates, lambdas and functions pitfall

2016-09-05 Thread dom via Digitalmars-d-learn
On Monday, 5 September 2016 at 12:32:49 UTC, Lodovico Giaretta 
wrote:

On Monday, 5 September 2016 at 12:15:35 UTC, dom wrote:

[...]


You misunderstood the error message and the lambda syntax (it 
also happened to me the first time).


The grammar says that you can use one of these syntaxes:

1) `(arguments) {block of code}`

2) `(arguments) => expression`, which is equivalent to 
`(arguments) {return expression;}`


3) `{block of code}`, which is equivalent to `(){block of 
code}`.


So if you write `(arguments) => {block of code}`, this is 
equivalent to (see rule 2) `(arguments) { return {block of 
code}; }`.


And because of rule 3, it becomes `(arguments) { return 
(){block of code} }`. So what you have is a function that 
returns a function. That's why it does not match your signature.


The fact that the compiler also adds nothrow, @nogc, @safe is 
not important in this case, as those attributes can be 
implicitly casted away.


thank you for that detailed answer.




Re: delegates, lambdas and functions pitfall

2016-09-05 Thread dom via Digitalmars-d-learn

On Monday, 5 September 2016 at 12:30:37 UTC, Daniel Kozak wrote:



Dne 5.9.2016 v 14:15 dom via Digitalmars-d-learn napsal(a):

...
but what is the difference between a lambda (=>) and a 
functions/delegates?
i think this is a major pitfall for newcomers, and should be 
adressed somehow.

Yes, RTFM :)


yes you are right, but laziness you know :D

those pages aren't especially beginner friendly x)
https://dlang.org/spec/expression.html#Lambda


Removing array element in foreach, safe?

2016-09-05 Thread dom via Digitalmars-d-learn

is this code safe? if not how do i do it correctly?

static AsyncHttpGet[] openRequests;
static void updateRequests()
{
foreach(idx, req; openRequests)
{
if(req.state != Fiber.State.TERM)
req.call();
else
openRequests.remove(idx);
}
}

thx :)


delegates, lambdas and functions pitfall

2016-09-05 Thread dom via Digitalmars-d-learn
I am about to write my own stupid and simple http client .. and i 
have added a callback function that has the received content as a 
parameter.


class AsyncHttpGet
{
this(string host, ushort port, string path, void 
delegate(string) callback )

{ ... }
}

My first attempt was to write:

auto f = new AsyncHttpGet("www.dprogramming.com", 80, 
"/index.php", (string content) => {

  ...
});

but this is does not work because my AsyncHttpGet takes a normal 
delegate and this => seems to add nothrow @nogc @safe to my 
delegate type.


The correct syntax is only marginally differnt, but took me quite 
a while to figure out:

( the missing arrow )

auto f = new AsyncHttpGet("www.dprogramming.com", 80, 
"/index.php", (string content)

{
 ... // this is of type function
});

i noticed that delegates are "more powerful" than functions. once 
the passed function e.g. needs to capture a value from the 
outside it becomes a delegate type. I have also read that a 
delegate can contain a reference to a class method bound to an 
instance.


int dummy = 0;
auto f = new AsyncHttpGet("www.dprogramming.com", 80, 
"/index.php", (string content)

{
 dummy = 1; // this is of type delegate
});

but what is the difference between a lambda (=>) and a 
functions/delegates?
i think this is a major pitfall for newcomers, and should be 
adressed somehow.


Re: Classes and Structs, Memory management questions

2016-09-02 Thread dom via Digitalmars-d-learn

On Friday, 2 September 2016 at 08:59:38 UTC, Andrea Fontana wrote:

On Friday, 2 September 2016 at 08:54:33 UTC, dom wrote:
i haven't read it fully yet, but i think this DIP contains 
some or all of my concerns

https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md


Check this:
https://dlang.org/phobos/std_experimental_allocator.html


thx that is very interesting. it seems like that can cover very 
complex allocation schemes with a general interface!


i have also found this to allocate a class on the stack
http://dlang.org/phobos/std_typecons.html#.scoped
class A { ... }
auto instance = scoped!A();

that has even quite a nice syntax!


Re: Classes and Structs, Memory management questions

2016-09-02 Thread dom via Digitalmars-d-learn
i haven't read it fully yet, but i think this DIP contains some 
or all of my concerns

https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md


Classes and Structs, Memory management questions

2016-09-02 Thread dom via Digitalmars-d-learn
from what i got Classes are always reference types and structs 
are value types in D. i am wondering why that is. for me the main 
difference between classes and structs is not how they are 
allocated, but that one has inhertiance, and the other hasn't. 
Supporting inheritance has some overhead, so at least the split 
between classes and structs makes sense to me.


I like garbage collection, but in many cases it's just 
unnecessary (like in the example below) or hurts performance on a 
bigger scale.


{
  FileReader reader = new  // Annoy the garbage collector for 
no reason?

  auto blob = reader.read();
  delete reader;
}

Since garbage collection is a very nice feature that I wouldn't 
wanna miss for certain scenarios I think D should give us the 
opportunity to determine how an object is allocated. In the 
example above putting it on the stack is probably a good idea. 
Having a self managed reference to the heap can be good too if 
manual memory management is wanted. Or of course let the GC 
manage it ( i love it for prototyping code and also as a D 
beginner it is beneficial that i just dont need to care about 
memory management).


Could somebody explain to me if this is seen as a problem 
why/whynot and how I should address that kind of issues in my 
code?




to auto or not to auto ( in foreach )

2016-07-16 Thread dom via Digitalmars-d-learn

foreach(auto v; msg)
  writeln(v);

gives an error that a basic type is expected

foreach(v; msg)
  writeln(v);

works

.. but why?


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

2016-07-15 Thread dom via Digitalmars-d-learn
i just had a scenario like the one below. when calling .array on 
the filterresult dmd goes nuts because of the init() function in 
Players. Renaming to initialize() solved the problem.


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


experienced programmers ofc know that .init is used in special 
ways and avoid it. newcomers like me have a hard time with such 
cases.


class Players
{
  void init() { ... }

  ...
}

auto candidates = players.filter!(x => {
  return x.active && x.getName().indexOf(searchstring) > -1;
}).array;





Re: cant run unittests

2016-07-14 Thread dom via Digitalmars-d-learn

On Thursday, 14 July 2016 at 00:33:50 UTC, ethgeh wrote:

On Wednesday, 13 July 2016 at 19:41:53 UTC, dom wrote:
how can i run my unittests for a dynamic library? some weird 
conflict is reported between main functions, my project doesnt 
contain any main function.


[...]


try to put this before the main of your application:

  "version(unittest){} else"

it looks like the default unittest config implies the switch 
"-main".


as i said my project doesnt contain a main() function


cant run unittests

2016-07-13 Thread dom via Digitalmars-d-learn
how can i run my unittests for a dynamic library? some weird 
conflict is reported between main functions, my project doesnt 
contain any main function.


i really love D, but problems like this make me wanna switch :/

using:
dub test --arch=x86

lucy ~master: building configuration "__test__library__"...
../../tmp/dub_test_root-0ce1acbf-ecb9-4abf-8199-e7a23bd783dc.d(21,12): Error: 
function D main conflicts with static import dub_test_root.main at 
../../tmp/dub_test_root-0ce1acbf-ecb9-4abf-8199-e7a23bd783dc.d(11,15)
dmd failed with exit code 1.

dub.json
{
"name": "lucy",
"description": "A minimal D application.",
"copyright": "Copyright © 2015, dom",
"authors": ["dom"],
"targetType": "dynamicLibrary",
"dflags": ["-fPIC"],
"dependencies": {
"msgpack-d": "~>1.0.0-beta.2",
"poodinis": "~>6.3.0"
}
}


Re: compile shared lib with dub

2015-06-25 Thread dom via Digitalmars-d-learn
ok i actually did everything right x3 ... but my code threw some 
warnings which are interpreted as errors by default.


/solved


compile shared lib with dub

2015-06-25 Thread dom via Digitalmars-d-learn
i want to build a shared library (.so) with dub. currently i 
compile with a shell script, but i'd like to use dub


[code]
dmd -c test.d -fPIC
dmd -ofcod4xalicebridge.so test.o -shared -g -w -debug 
-version=Have_cod4xalicebridge

[/code]

could anyone tell me how my dub.json has to look like?

my failing dub.json :/
[code]
{
name: cod4xalicebridge,
description: A minimal D application.,
copyright: Copyright © 2015, root,
authors: [root],
targetType: dynamicLibrary,
dflags: [-fPIC, -shared],
dependencies: {
}
}
[/code]