Re: fPIC Error

2016-11-18 Thread deadalnix via Digitalmars-d-learn
On Thursday, 3 November 2016 at 06:11:48 UTC, rikki cattermole 
wrote:

[Environment32]
DFLAGS=-I/usr/include/dmd/phobos 
-I/usr/include/dmd/druntime/import -L-L/usr/lib/i386-linux-gnu 
-L--export-dynamic -fPIC -defaultlib=libphobos2.so


[Environment64]
DFLAGS=-I/usr/include/dmd/phobos 
-I/usr/include/dmd/druntime/import 
-L-L/usr/lib/x86_64-linux-gnu -L--export-dynamic -fPIC 
-defaultlib=libphobos2.so


I'm sorry bit that's horseshit. I'm not compiling everything with 
fPIC because DMD can't get it's act straight.




Re: function pointer when a function is overloaded.

2012-07-01 Thread deadalnix

Le 01/07/2012 01:59, dnewbie a écrit :

import std.stdio;

alias void function(int) fooInt;
alias void function(long) fooLong;

int main(string[] args)
{
fooInt f1 = &foo;
fooLong f2 = &foo;
f1(1L);
f2(1L);
return 0;
}

void foo(int i)
{
writeln("foo(int i)");
}

void foo(long i)
{
writeln("foo(long i)");
}


Thanks.


function pointer when a function is overloaded.

2012-06-30 Thread deadalnix
Simple question. How to I get a function pointer to one of the foo 
functions in this case :


void foo(int i);
void foo(long i);


Re: How do I force something onto the heap? (need for libev)

2012-03-06 Thread deadalnix

Le 06/03/2012 05:34, Tyler Jameson Little a écrit :

I've been playing with libev in D lately, and I've run into a problem.
I've been able to hack around it, but it'd like to find a better, more
general solution. Here's a link to the code:

https://github.com/beatgammit/fun-with-d/blob/master/libev/tcp_server.d

The code is a basic TCP server that responds to connections in a
non-blocking fashion. It's not perfect, and the current problem I'm
trying to solve is how to get my Socket instance (from accept) to the
handler.  Since everything is asynchronous, and the return value of
accept() will get lost (garbage collected, I think). When I try to get
the address of it, the compiler complains that it's not an lvalue.

As you can see, I've hacked around it by grabbing the handle and
recreating the Socket instance in the handler (line 14). The problem,
however, is that this can only assume a single type in AddressFamily.
Honestly, I will probably only need INET, INET6, or UNIX (which can be
set in a global), but this sounds a bit hacky to me.

I was able to hack around the problem for libev structs, because
creating a new instance returns a pointer, which can be assigned
somewhere else to be garbage collected later. This doesn't seem to be
the case for classes, however.

Initially, I solved this before by having a global Socket[] and adding
sockets to it as I received them, but this was an even worse hack to get
around the GC (see previous version in version control if interested).
This did, however, allow me to get a reference from the array to assign
to the data value (a void*), where it could be retrieved in the callback.

Are there any other options that I've missed that would make this
cleaner and more general?

Also, I'd be interested if someone notices some badness in my code that
could lead to nasty side-effects. I'm trying to make this example pretty
robust in terms of cleaning up after myself and doing things correctly.

Thanks so much!!


You can new stuff();

Alternatively, if you have of stuff passed by value, you can :

new stuff(stuffByValue); // Don't always work.

*([stuffByValue].ptr); // Crazy but works :D


Re: D runtime Garbage Collector details

2012-02-24 Thread deadalnix

Le 24/02/2012 00:49, Vadim a écrit :

I am looking for the details on D Garbage Collection implementation. I
will much appreciate if someone suggests the answers or give some links
to existing documentation clarifying the following points:

1. Is it Mark-n-Sweep or copy or generational collector or simple
reference counting? Or anything else? Any documentation on this would be
very helpful



Mark and sweep, but do not copy and isn't generationnal.


2. Sun/Oracle JVM publishes a number of counters in shared memory so
that user may easily monitor the memory usage and the GC statistics
real-time without affecting the application (and without modifying the
application). Is there anything similar for D GC?



You should ask the system for thoses infos.


3. Is there any performance tests of D GC efficiency? Is it possible to
write GC-predictable code (without manual allocate/free) or at least do
something like System.gc()?


Its performance are quite poor ATM, it is getting better, but still not 
as effeiscient as java's GC for example. You could disable it using the 
class GC on critical period of time, so you'd know when it run and when 
it cannot trigger itself. D allow also to manage memory manually, and if 
you don't generate garbage, GC will not trigger.


Re: Pure and higher-order functions

2012-02-23 Thread deadalnix

Le 23/02/2012 21:00, mist a écrit :

Hello!

I have been asked few question recently from a Haskell programmer about
D2 and, after experimenting a bit, have found that I really can't
provide a good answe myself, as I am not getting a design limititations
(if any).

Here is the snippet, it is pretty self-descriptive:
http://codepad.org/DBdCJYI2

Am i right, that all information about purity & Co is lost at runtime
and there is no way to write pure-aware higher-order function using
dynamic function pointers? That would have made me really sad :(


Information is lost because of what f2 accept as a type. f2'q param 
isn't pure, so you gat what you except.


Within f2, the fact that your function is pure is lost. f2 accept any 
function, pure or not.


pure function can be casted automatically to non pure, because it is 
safe, but the other way around, it is impossible (because purity 
constraint would be broken).


Re: inout problems

2012-02-22 Thread deadalnix

Le 22/02/2012 05:01, Andrej Mitrovic a écrit :

class Foo
{
 this(int) inout
 { }

 Foo makeFoo() { return new Foo(1); }
}

void main() { }

test.d(8): Error: cannot implicitly convert expression (new Foo(1)) of
type inout(Foo) to test.Foo

Is this a bug?


inout is supposed to propagate a parameter qualifier to the return type. 
Here, inout is useless, and it isn't surprising that it doesn't work.


Re: delegate as memeber

2012-02-22 Thread deadalnix

Le 21/02/2012 18:46, Jacob Carlborg a écrit :

On 2012-02-21 16:55, deadalnix wrote:

Le 21/02/2012 16:48, Adam D. Ruppe a écrit :

A possible workaround is to initialize the delegate
in the object's constructor.


It is a struct. And struct don't have default constructor. It lead to
very segfault prone code (I did try that).


You can implement a static opCall and use that instead of the constructor.



It's a cheap replacement because it doesn't allow to new.


Re: delegate as memeber

2012-02-22 Thread deadalnix

Le 22/02/2012 03:59, Vladimir Panteleev a écrit :

On Tuesday, 21 February 2012 at 15:41:58 UTC, deadalnix wrote:

Le 21/02/2012 16:32, Vladimir Panteleev a écrit :

On Tuesday, 21 February 2012 at 15:22:15 UTC, deadalnix wrote:

struct stuff {
private Exception delegate() exceptionBuilder = delegate Exception() {
return new Exception("foobar");
};
}

The following piece of code trigger a compiler error : delegate
module.stuff.__dgliteral1 function literals cannot be class members

Why is that ? Is it a bug or a feature ?


Delegates contain a context pointer. Your delegate literal has no
context.

You can't initialize it with the address of a method, either. For struct
methods, the context pointer is a pointer to the structure. You can't
have a .init that contains a pointer to an instance. You probably want
to use a function literal.


It doesn't work with function either.

But I need delegate here. The default one doesn't require a context,
but isn't it possible to pass null as a context, as none is needed ?
This value can be changer later, and definitively require to be a
delegate.


struct stuff {
private Exception function() exceptionBuilder =
&defaultExceptionBuilder;

private static Exception defaultExceptionBuilder() {
return new Exception("foobar");
};
}


This look very promizing !!!

I'll investigate in that direction. Thank you very much for the hint.


Re: delegate as memeber

2012-02-21 Thread deadalnix

Le 21/02/2012 17:30, Mantis a écrit :

21.02.2012 17:24, deadalnix пишет:

struct stuff {
private Exception delegate() exceptionBuilder = delegate Exception() {
return new Exception("foobar");
};
}

The following piece of code trigger a compiler error : delegate
module.stuff.__dgliteral1 function literals cannot be class members

Why is that ? Is it a bug or a feature ?



The compiler expects member initializers to be known at compile-time.
Since delegate carries closure, and closure is a run-time phenomena, you
cannot put it there. That's how I understand it, and I might be wrong.
Anyway, something like this is possible as a workaround:

struct Foo {
private Exception dg() {
if( m_Dg ) return m_Dg();
return new Exception( "foobar" );
}

private Exception delegate() m_Dg = null;
}


I think this the best solution after all.

But still I think the original code should be an error only if it use 
data out of the delegate scope. If it doesn't, frame pointer doesn't 
matter and null can be passed.


Re: delegate as memeber

2012-02-21 Thread deadalnix

Le 21/02/2012 16:48, Adam D. Ruppe a écrit :

A possible workaround is to initialize the delegate
in the object's constructor.


It is a struct. And struct don't have default constructor. It lead to 
very segfault prone code (I did try that).


Re: delegate as memeber

2012-02-21 Thread deadalnix

Le 21/02/2012 16:32, Vladimir Panteleev a écrit :

On Tuesday, 21 February 2012 at 15:22:15 UTC, deadalnix wrote:

struct stuff {
private Exception delegate() exceptionBuilder = delegate Exception() {
return new Exception("foobar");
};
}

The following piece of code trigger a compiler error : delegate
module.stuff.__dgliteral1 function literals cannot be class members

Why is that ? Is it a bug or a feature ?


Delegates contain a context pointer. Your delegate literal has no context.

You can't initialize it with the address of a method, either. For struct
methods, the context pointer is a pointer to the structure. You can't
have a .init that contains a pointer to an instance. You probably want
to use a function literal.


It doesn't work with function either.

But I need delegate here. The default one doesn't require a context, but 
isn't it possible to pass null as a context, as none is needed ? This 
value can be changer later, and definitively require to be a delegate.


delegate as memeber

2012-02-21 Thread deadalnix

struct stuff {
private Exception delegate() exceptionBuilder = delegate Exception() {
return new Exception("foobar");
};
}

The following piece of code trigger a compiler error : delegate 
module.stuff.__dgliteral1 function literals cannot be class members


Why is that ? Is it a bug or a feature ?


Re: Anti-OOP... stupid?

2012-02-14 Thread deadalnix
IMO, what would be stupid is that everything has to be object 
oriented.


You have problems where OOP is good, and other where it isn't. 
Use the tool that fit what you want to accomplish.


Screwdriver are great, but are useless when you are dealing with 
a nail.


On Tuesday, 14 February 2012 at 22:00:44 UTC, Zero wrote:

Hello!

I've recently started to work with D, and I'll start a "bigger" 
project soon, using it. For a few days I've been thinking about 
the approach I'll take here, and since I don't /have/ to use 
full OOP in D, I was wondering... how crazy is it to not use 
full OP nowadays?


Naturally one would use objects, but since this isn't C#, or 
Java, not freaking everything has to be an object. The C style 
kinda feels more natural to me. (I know, I know, C naturally 
can do some OOP like stuff, etc... but you get the idea, mainly 
normal functions, some globals, yada, yada.)


What I want to know from you people... is that stupid? To even 
think about doing something like this? Basically mixing 
procedural and OOP? I know about the dangers, and 
disadvantages, but if those don't scare one away, would you 
consider it bad, if someone did this, on a larger project?


I'm looking forward to your answers.

Zero





Re: Restrict access to "critical" functions

2011-12-15 Thread deadalnix

Le 14/12/2011 13:48, Timon Gehr a écrit :

On 12/14/2011 01:28 PM, Kagamin wrote:

Goal would be to have a possibility to compile and let run code from
random people (some of them perhaps evil minded), watch over the
processes and kill them, if they take too long or use up too much
memory.


I believe this is what SafeD is for.


SafeD eliminates the possibility of memory corruption, it does not
prevent the program from messing with the system.


Nothing does expect thing that doesn't have side effect.

So basically, the OP only want pures function. They exists in D, but I 
highly doubt you can produce anythoing usefull using only pure function.


Even haskell has non pure functions (IO monad for exemple).


Re: Array initialization quiz

2011-11-29 Thread deadalnix

Le 29/11/2011 04:11, Andrej Mitrovic a écrit :

On 11/29/11, bearophile  wrote:

Do you know why the compiler doesn't ask you for a cast, and why the run
does that?


Because foreach is broken?
http://d.puremagic.com/issues/show_bug.cgi?id=4510


No it has nothing to do with this bug. But actually, this exemple should 
generate a warning at least, or being illegal eventually. I being ubyte, 
the max value is 255, so you always ends up with a valid value of i and 
the loop never stop.


Re: Make a variable single-assignment?

2011-11-21 Thread deadalnix

Le 21/11/2011 15:04, Alex Rønne Petersen a écrit :

Hi,

Is there any way to make a variable single-assignment, regardless of its
type? I.e.:

void foo()
{
 int i = 0;
i = 2; // Error: i cannot be reassigned
}

I realize const and immutable will do this, but they are transitive and
infect the type, which I do *not* want. I simply want the variable to be
single-assignment. Is it possible?

- Alex


You can create a struct Final you could use as Final!(type) variable;

Overloading opAssign should do whatever you need.

I don't think adding to the core language what could ba achived with a 
nice abstraction should be done.


Re: Is there a portable way to limit memory/cpu usage of a D application?

2011-11-10 Thread deadalnix

Le 09/11/2011 18:12, Dejan Lekic a écrit :

I would be satisfied with something like POSIX.1-2001 setrlimit() . Sure
nothing prevents me from using setrlimit() in my D app, but perhaps it is
something to think about a portable way of doing that.

One thing I like about my Java apps is that I can always specify (in the
argument list) how much memory I want to allocate for them). Yes, it is a
JVM specific thing, but perhaps a GC should have options to set at least the
memory limit?

Kind regards


Well no, but you can ask the gc to collect and realease block to the 
system explicitely (and not like in Java, which you can only trigger 
collection for young object from code). So basically you can control how 
much memory your application is consuming.


Re: Double implicitly converted to real

2011-11-03 Thread deadalnix

Le 03/11/2011 15:39, Charles McAnany a écrit :

Hi. I noticed that one of the guarantees in TDPL is that any code that is valid 
in both C
and D should compile with the same result. But I'm seeing a different behavior 
here.
I'm trying to find the smallest double for which the comparison x+1/x = x is 
true.
I take a number way too small, and a number way too large, and then binary 
search (for 100
iterations) to get the desired number:

//add appropriate import std.stdio or #include
int main(){
 double min = 1;
 double max = 100;
 int iters = 0;
 double average;
 for(;iters<100; iters++){
 average = (min+max)/2;
 if( average + 1/average == average)
 max = average;
 else
 min = average;
 }
 printf("%f",average);
 return 0;
}

Here's the problem:
D (under DMD v2.051) gives this answer: 4294967296.00
C (gcc version 3.4.6 20060404): 134217728.00

It seems D is implicitly converting double to real. Is this the usual behavior?

Cheers,
Charles.


As long as you don't loose information, you can cast implicitely in D. 
If you loose information (or the compiler cannot prove that your are not 
loosing information) then an explicit cast is required.


So this implicit cast is expected.

Now, you are not using real in your code, so you shouldn't use real 
anywhere. Are you sure this is the actual issue ?


Finally, both compiler you are using are rather old ones. dmd is in 
version 2.056 now and gdc has 4.6.2 version (and using 2.055 frontend).


Re: template expressions in C++ to an equivalent in D

2011-10-28 Thread deadalnix

Le 28/10/2011 19:18, Dominic Jones a écrit :

Hello,

I want to compute, for example
d = a + b + c
where a..d are of some derived type, without incurring the cost of temporaries 
for each overloaded operation.

In a similar post a while ago, Walter Bright proposed using function literals 
instead of template expressions to do this. After looking at function literals, 
I can't work out what he had in mind with this suggestion. May someone present 
a succinct
complete example to demonstrate how solve this?

Many thanks,
Dominic Jones


I was thinking about porting http://eigen.tuxfamily.org/index.php in D. 
I'm currently on SFML2 but it goes quite fast (thanks to  Trass3r for 
porting SFML to D2 so a big part of the job is done).


You may want to do so ? Anyway, I'm not surefunction literal can do the 
trick. I would be interested on knowing more on the topic. If it is more 
suitable, maybe I should reconside my project to port eigen.


Deadalnix


Re: scope struct?

2011-10-17 Thread deadalnix
Nice trick ! However, in D, you have scope(exit) scope(success) and 
scope(failure) to do similar stuffs.


I personally use both, on a case by case basis.

Le 17/10/2011 06:47, Steve Teale a écrit :

Is not needed because structs are inherently scope.

I'm sure experienced D programmers do this all the time when they want
something done on exit from a scope, but I never had, and maybe there are
others who haven't, particularly if coming from a C++ 'use classes for
everything' background.

import std.stdio;

bool glob;

struct Sentinel
{
void function() doit;
bool already;
this(void function() f)
{
   doit = f;
   already = false;
}

~this()
{
   if (!already)
   {
  writeln("Doing it now");
  doit();
   }
   else
  writeln("Won't bother");
}

void dontBother() { already = true; }
}

void reset() { glob = false; }

void main(string[] args)
{
glob = true;
{
   Sentinel s = Sentinel(&reset);
   writeln("Doing stuff in the scope");
   if (args.length>= 2&&  args[1] == "db")
  s.dontBother();
}
writeln(glob);
}




Re: Implementing iterators for D custom data structures (best practices)

2011-10-17 Thread deadalnix
In addition, I would like to mention this confrence by Andrei 
Alexandrescu : http://blip.tv/boostcon/boostcon-2009-keynote-2452140


Le 17/10/2011 04:55, Jonathan M Davis a écrit :

On Monday, October 17, 2011 10:22:42 Granville Barnett wrote:

Hi All,

I've not used D for long so hopefully this question isn't too stupid...

Are there any best practices for implementing iterators in D? My
understanding is that D follows a similar design approach to the STL RE
containers - iterators - algs - adapters etc.

Also, are there any examples of well written data structures that expose
iterators?...I recall that in D only arrays have them (I may be wrong, I
haven't looked). Any open source projects that are notoriously well
implemented?

Thanks in advance.


D code doesn't normally use iterators. It uses ranges, which are a similar but
generally superior concept.

http://www.informit.com/articles/printerfriendly.aspx?p=1407357

std.range and std.algorithm in particular make heavy use of ranges:

http://d-programming-language.org/phobos/std_range.html
http://d-programming-language.org/phobos/std_algorithm.html

- Jonathan M Davis




Re: operator "~" does not check type?

2011-10-12 Thread deadalnix

Le 12/10/2011 09:53, bearophile a écrit :

Jonathan M Davis:


int and dchar implicitly convert to one another for better or for worse.
Personally, I'd prefer that they didn't, but that's the way that it is, so I
don't believe that this is technically a bug.


char->int is OK, but int->char is not so OK. This programs (that compiles with 
no errors) seems to show a possible source of bugs, so I call this a design bug, 
worth fixing:



In D, the conversion is implicit if the compiler can detect it is same 
via bound checking. Here, the compiler can deduce that the int 10 is 
between 10 and 10 so can be safely converted.


Re: How convice people that D it's wonderfull in a hour ?

2011-10-09 Thread deadalnix

Le 09/10/2011 12:00, Zardoz a écrit :

Recently I've been asked if I could give a speech about D in my university. It
will be of one hour of long.
I not respond yet, but I think that I will do it. Actually I have the problem
that I don't know well how explain well too many features and things of D that
I like. I think that only talking about D's arrays and type system I will need
around half-hour.
Any recommendation of how I should focus it ?


I think you should show through several exemples. AS IT IS UNIVERSITY, I 
guess your public will know about programming.


Explain difference between struct and classes, and how it is great 
compared to C++ (no slicing for exemple).


Show some stuffs about first class function and delegates (for callback 
for exemple) coppared to how painful it is in C++ or java (using 
interface and useless object)


Then, gove a talk aboit metaprogramming (how you can implement behaviral 
pattern with no cost at runtime, how you can make very generic code like 
STL) and why it is way better than C++ (usability with stuffs like 
static if) or java (generic isn't metaprogramming).


And last but not least : explain memory model and how it help to deal 
with multithrading problems where other languages usually aren't good.


The point isn't to bash C++ or java, but to show how some problem you 
face in thoses languages can be solved elegantly in D.


If your public knows others languages than java and C++, then adapt the 
speach to what they know.


Re: I can't build dsfml2 or derelict.sfml whit dsss

2011-09-22 Thread deadalnix
DSFML2 doesn't exist right now. So no doubt you'll have trouble to 
compile it. I don't know what you are try to achieve, but definitively 
not what you believe.


Note that my answer in the mentionned link give you all you need to use 
SFML with D.


SFML2 is currently on a devellopement stage, so is DSFML2. Considering 
the current stage of DSFML2, you don't want to use it right now.


Le 22/09/2011 06:10, Cuauhtémoc Ledesma a écrit :

First at all sorry for my english.

I've tried to build any binding of sfml in a 32-bit machine with
archlinux. My problem with dsfml2 is similar to this
http://www.digitalmars.com/d/archives/digitalmars/D/learn/Buliding_DSFML2_64-bit_Linux_25694.html.
After installing mingw32-pthreads (what i don't know if is the correct
library) the problem persist and I don't know which library link. But
this only happens when i try to compile an individual file with dmd, not
with "dsss build".

After trying to build derelict (using the command "dsss net install
derelict") to get derelict.sfml, I figured that the problem maybe is
dsss, becouse every time I invoke it the output is as if I hadn't
written anything after the dsss command, what is not true. I know that
there is a derelict2 packages in yaourt, but i get some errors when I
try to install it.

This is insane , I can't get any binding of this particular library
becouse I can't even get that the tools work properly (I also tried in a
mac but I get some different errors). So if any one can help me to solve
any of this problems I will extremly grateful.

Thanks.





Re: Heap fucntion calls

2011-09-21 Thread deadalnix

Great answer ! Thank you very much, it answered almost everything !

But what about, in the exemple you gave me (which is great by the way) 
if foo as parameters ? Those parameters are passed on the stack by copy 
to the function, and then, copied to the heap (resulting in two copies) ?


Le 21/09/2011 19:56, Simen Kjaeraas a écrit :

void foo() {
int x = 5;
auto dg = () {x = 4;}
dg();
}

is roughly equivalent to:

typedef struct foo_dg_1_delegate {
void (*funcptr)(struct foo_dg_1_context*);
void* ptr;
};

typedef struct foo_dg_1_context {
int x;
};

void foo_dg_1(struct foo_dg_1_context* ctx) {
ctx->x = 4;
}

void foo(void) {
struct foo_dg_1_delegate dg;
struct foo_dg_1_context* ctx = (struct
foo_dg_1_context*)malloc(sizeof(struct foo_dg_1_context));
dg.funcptr = &foo_dg_1;
dg.ptr = ctx;
ctx->x = 5;
dg.funcptr(dg.ptr);
}





Heap fucntion calls

2011-09-21 Thread deadalnix
D has a wonderfull feature named delegate. Delegate can acess local 
data, thus would be dangerous if thoses data were on the stack. For what 
I understand, when a delegate can access the local data of a function, 
those data are set on the heap instead of the stack, resulting on a 
slower function call, but on a safe delegate behaviour.


I'm wondering what's going on behind the hood when such a function is 
called. are the parameter passed to the function on the stack and the 
copied on the heap ? In such a situation, data are copied two times. 
Will a postblit constructor be called two times ? Or is the function 
taggued as « heap function » and then only the pointer is passed in the 
function call ?


Secondly, how does thing like scope(exit) are handled in such a case ? 
When the constext is collected by the GC ? When the function ends it's 
execution ? The try {} finally {} analogy suggest the second one, but 
this is definitively not an exit of the scope, the scope being still 
accsible throw the delegate.


Those are exemple but more generaly, my question isn't about thoses 
exemples. It is about what really is going on. Let's say, what would be 
the C translation of such a function call or somethung similar.


Thank by adavnce,

deadalnix