Compatibility with D regexp constructor

2014-10-25 Thread Suliman via Digitalmars-d-learn

Where I can find compatibility with D online regexp constructor?


Two cases for improving error messages

2014-10-25 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. Please see the following and say whether they're OK to submit
as bugs for improving the error messages. Thanks.

ref int foo(ref int x) { return x ; }

void main () {
foo(3) ;
// Error: function rvalue_argument.foo (ref int x) is not callable
using argument types (int)
// Comment: argument ref int x of function rvalue_argument.foo cannot
bind to an rvalue would be clearer IMO

int i ;
ref ir = i ;
// Error: variable ref_type.main.ir only parameters or foreach
declarations can be ref
// Comment: add , return values after parameters
}


-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा



Re: Dynamically Loading a D DLL From a C Program in Linux

2014-10-25 Thread MGW via Digitalmars-d-learn

// MGW 05.01.14
// We model in D object C ++ QByteArray from Qt.
//
// Windows: dmd st1.d
//   Linux: dmd st1.d -L-ldl

import core.runtime; // Load DLL for Win
import std.stdio;// writeln

version(linux) {
import core.sys.posix.dlfcn;  // declare dlopen() и dlsym()

// On Linux these functions are not defined in core.runtime, 
here and it was necessary to add.
extern (C) void* rt_loadLibrary(const char* name) { return 
dlopen(name, RTLD_GLOBAL || RTLD_LAZY);  }
void* GetProcAddress(void* hLib, string nameFun) {  return 
dlsym(hLib, nameFun.ptr);}

}
version(Windows) {
import std.c.windows.windows;  // GetProcAddress для Windows
}
//it is important!!!
//At definition constructs and functions of members the attribute 
extern (C) is obligatory!
alias extern (C) void function(void*, char*)   
t_QByteArray_QByteArray;  t_QByteArray_QByteArray  
QByteArray_QByteArray;
alias extern (C) void* function(void*, char, int)  
t_QByteArray_fill;t_QByteArray_fill
QByteArray_fill;


// Struct QByteArray from qbytearray.h in include directory.
// inline char *QByteArray::data() { detach(); return d-data; } 
где d есть Data*

struct Data {
void* rref;
int   alloc;
int   size;
char* data;  // Here actually behind what it is 
necessary for us, the index on a file of bytes

char  array[1];
}

// == Experimental class DQByteArray ==
class DQByteArray {
Data* QtObj;   // Object: QtObj - its size of 4 bytes 
(32 digit version)

// --
// class D, call class C++
this(char* buf) {
QByteArray_QByteArray(QtObj, buf);
}
~this() {
// It is possible to find ~this and here it to register, 
but it is routine

}
// inline char *QByteArray::data() { detach(); return 
d-data; } где d есть Data*

char* data() {
return (*QtObj).data;
}
// D: Data** == C++: QByteArray Here also it became clear, 
that such object With ++, looking at it from D

void* fill(char ch, int resize=-1) {
return QByteArray_fill(QtObj, ch, resize);
}
}

int main(string[] args) {

// Files with QByteArray C++
version(linux)   {auto nameQtCore = libQtCore.so;  }
version(Windows) {auto nameQtCore = QtCore4.dll;   }

auto h = Runtime.loadLibrary(nameQtCore); // Load dll или so

// It is QByteArray::QByteArray(char*);
QByteArray_QByteArray = 
cast(t_QByteArray_QByteArray)GetProcAddress(h, 
_ZN10QByteArrayC1EPKc);

// QByteArray::fill(char*, int);
QByteArray_fill = cast(t_QByteArray_fill)GetProcAddress(h, 
_ZN10QByteArray4fillEci);


// Create my class
DQByteArray ba = new DQByteArray(cast(char*)ABC.ptr);
printf(\n ba.data() = %s, ba.data());

// Test fill() from C++
ba.fill('Z', 5);
printf(\n ba.data() = %s, ba.data());

return 0;
}


Re: Two cases for improving error messages

2014-10-25 Thread bearophile via Digitalmars-d-learn

Shriramana Sharma:


int i ;
ref ir = i ;
// Error: variable ref_type.main.ir only parameters or foreach
declarations can be ref
// Comment: add , return values after parameters
}


I like this.

Bye,
bearophile


Type name shadowing

2014-10-25 Thread ixid via Digitalmars-d-learn

T shadow(T = int)(T a) {
alias T = string;
T b = hi;
T c = 1; // Error

writeln(typeof(a).stringof); // int
writeln(typeof(b).stringof); // string

return a;
}


Are there uses for this shadowing of type names? It seems a 
little dangerous, for example ulong T could be shadowed by uint 
T. Is there a reason to allow it?


Pragma mangle and D shared objects

2014-10-25 Thread Etienne Cimon via Digitalmars-d-learn
I haven't been able to find much about pragma mangle. I'd like to do the 
following:


http://forum.dlang.org/thread/hznsrmviciaeirqkj...@forum.dlang.org#post-zhxnqqubyudteycwudzz:40forum.dlang.org

The part I find ugly is this:

void* vp = dlsym(lib, _D6plugin11getInstanceFZC2bc2Bc\0.ptr);

I want to write a framework that stores a dynamic library name and 
symbol to execute, and downloads the dynamic library if it's not 
available. This would be in a long-running server/networking 
application, and needs to be simple to use.


The mangling makes it less obvious for the programmer writing a plugin. 
Does mangle make it possible to change this to dlsym(lib, 
myOwnMangledName), or would it still have strange symbols?


Also, I've never seen the thunkEBX change merged from here:

http://forum.dlang.org/thread/hznsrmviciaeirqkj...@forum.dlang.org?page=2#post-lg2lqi:241ga3:241:40digitalmars.com


Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

Hello everyone,

I am trying to understand UDA traits scoping while mixing in code.
Aiming to generate code based on UDA I wonder if the following is 
possible:


class A
{
   @Inject
   Logger logger;

   @Inject
   SomeOtherClass dependency;

   mixin injections!(A)

...
}

In injections function I want to iterate through members 
annotated with the @Inject attribute and generate some specific 
code.
So far in my attempts the compiler complains about unknown 
identifier A.
Should the A class be available to the compiler at the time of 
the mixin invocation?


Thanks for your help,
Rares


Re: Generating code based on UDA

2014-10-25 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 25 October 2014 at 13:37:56 UTC, Rares Pop wrote:
Aiming to generate code based on UDA I wonder if the following 
is possible:


Yes, and copy/pasting that works for me...


Re: Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

Thanks for the quick response.
What do you mean by copy/pasting ?

On Saturday, 25 October 2014 at 13:40:56 UTC, Adam D. Ruppe wrote:

On Saturday, 25 October 2014 at 13:37:56 UTC, Rares Pop wrote:
Aiming to generate code based on UDA I wonder if the following 
is possible:


Yes, and copy/pasting that works for me...




Re: Generating code based on UDA

2014-10-25 Thread Shammah Chancellor via Digitalmars-d-learn

On 2014-10-25 13:37:54 +, Rares Pop said:


Hello everyone,

I am trying to understand UDA traits scoping while mixing in code.
Aiming to generate code based on UDA I wonder if the following is possible:

class A
{
@Inject
Logger logger;

@Inject
SomeOtherClass dependency;

mixin injections!(A)

...
}

In injections function I want to iterate through members annotated 
with the @Inject attribute and generate some specific code.

So far in my attempts the compiler complains about unknown identifier A.
Should the A class be available to the compiler at the time of the 
mixin invocation?


Thanks for your help,
Rares


Very much possible.  Since I don't see what your template is doing, I'm 
going to give it a guess:


class A{
mixin injections;
}

template injections
{

 typeof(this);  // e.g. foreach( tra; 
__traits(getAttributes, typeof(this)))



}


typeof(this) will be A.



Re: Generating code based on UDA

2014-10-25 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 25 October 2014 at 13:45:29 UTC, Rares Pop wrote:

What do you mean by copy/pasting ?


I literally copied the code in your post (and fixed a missing 
semicolon) and got it to compile.


Passing A as an argument to injections should work. You can also 
use this and typeof(this) inside the injections template code to 
access the class. It should all work.


Re: Two cases for improving error messages

2014-10-25 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Oct 25, 2014 at 05:14:31PM +0530, Shriramana Sharma via 
Digitalmars-d-learn wrote:
 Hello. Please see the following and say whether they're OK to submit
 as bugs for improving the error messages. Thanks.
 
 ref int foo(ref int x) { return x ; }
 
 void main () {
 foo(3) ;
 // Error: function rvalue_argument.foo (ref int x) is not callable
 using argument types (int)
 // Comment: argument ref int x of function rvalue_argument.foo cannot
 bind to an rvalue would be clearer IMO
 
 int i ;
 ref ir = i ;
 // Error: variable ref_type.main.ir only parameters or foreach
 declarations can be ref
 // Comment: add , return values after parameters
 }
[...]

I agree with submitting both of these as enhancement requests. Please
tag them with diagnostic in the keywords field.


T

-- 
To provoke is to call someone stupid; to argue is to call each other stupid.


Re: Type name shadowing

2014-10-25 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Oct 25, 2014 at 12:28:39PM +, ixid via Digitalmars-d-learn wrote:
 T shadow(T = int)(T a) {
   alias T = string;
   T b = hi;
   T c = 1; // Error
 
   writeln(typeof(a).stringof); // int
   writeln(typeof(b).stringof); // string
 
   return a;
 }
 
 
 Are there uses for this shadowing of type names? It seems a little
 dangerous, for example ulong T could be shadowed by uint T. Is there a
 reason to allow it?

The problem gets worse than that. For example:

external_library.d
module external_library;
alias T = string;

main.d
module main;
void func(T = int)(T i) {
import external_library;
pragma(msg, T.stringof); // prints 'string'
}
void main() {
func(1);
}

Imagine that the 'alias T' was not present in an earlier version of the
library, but now has been added by the library author. Suddenly, user
code breaks without warning.


T

-- 
Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG


Re: Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

On Saturday, 25 October 2014 at 13:53:35 UTC, Adam D. Ruppe wrote:

On Saturday, 25 October 2014 at 13:45:29 UTC, Rares Pop wrote:

What do you mean by copy/pasting ?


I literally copied the code in your post (and fixed a missing 
semicolon) and got it to compile.


Passing A as an argument to injections should work. You can 
also use this and typeof(this) inside the injections template 
code to access the class. It should all work.


Taking this one step further, it looks like the attributes are 
not available at the mixin scope.

Here is my code:

--

struct Inject {
//  immutable Scope scoped;
}

static string injections(T)()
{
pragma(msg, injections for : , T);
string result;
foreach(member; __traits(allMembers,T))
{   
enum fullName = format(%s.%s, T.stringof, member);
pragma(msg, member: , fullName);
auto attributes = __traits(getAttributes, fullName);
		enum dbg_msg = format (%s attributes are %s, fullName, 
attributes.stringof);

pragma(msg, dbg_msg);
foreach(attr;attributes){
pragma(msg, Checking attribute, attr);
}

}
return result;
}

class A {

this(){
}
}

class B {

@Inject A a;

mixin(injections!(B));

}

---
 when compiling this code this is the output I get:


Compiling using dmd...
injections for : B
member: B.a
B.a attributes are tuple()
member: B.toString
B.toString attributes are tuple()
member: B.toHash
B.toHash attributes are tuple()
member: B.opCmp
B.opCmp attributes are tuple()
member: B.opEquals
B.opEquals attributes are tuple()
member: B.Monitor
B.Monitor attributes are tuple()
member: B.factory
B.factory attributes are tuple()
--

B.a attributes are an empty tuple even though the member is 
annotated with @Inject.

Any ideas why?


Re: Pragma mangle and D shared objects

2014-10-25 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Oct 25, 2014 at 09:20:33AM -0400, Etienne Cimon via Digitalmars-d-learn 
wrote:
 I haven't been able to find much about pragma mangle. I'd like to do
 the following:
 
 http://forum.dlang.org/thread/hznsrmviciaeirqkj...@forum.dlang.org#post-zhxnqqubyudteycwudzz:40forum.dlang.org
 
 The part I find ugly is this:
 
 void* vp = dlsym(lib, _D6plugin11getInstanceFZC2bc2Bc\0.ptr);
 
 I want to write a framework that stores a dynamic library name and
 symbol to execute, and downloads the dynamic library if it's not
 available. This would be in a long-running server/networking
 application, and needs to be simple to use.

Perhaps the .mangleof built-in property might help you here? For
instance:

---plugin.d---
class MyClass;
MyClass getInstance();

---test.d---
import std.stdio;
import plugin;

void main() {
writeln(plugin.getInstance.mangleof);
}

Output:

_D6plugin11getInstanceFZC6plugin7MyClass

Granted, it's a bit ugly (you have to actually create a module called
'plugin' in order to get the right mangling), but at least it doesn't
require the user to learn how D's mangling scheme works. You just write
a function prototype for the function you're trying to lookup, and call
.mangleof on it.


 The mangling makes it less obvious for the programmer writing a
 plugin. Does mangle make it possible to change this to dlsym(lib,
 myOwnMangledName), or would it still have strange symbols?
[...]

What you *could* do, is to use .mangleof and clever regex'ing to make it
possible to do that. For example, something along these lines:

---dl_support.d---
alias ReturnType = ... /* whatever type you want */;
private ReturnType pluginFuncStubName(... /* arguments here */);

auto loadPluginFunction(string library, string funcName) {
auto r = regex(`pluginFuncStubName`);
auto mangledName =
pluginFuncStubName.mangleof.replace(r, funcName);
...
/* mangledName should now be the mangled string you need
 * to find the symbol */
}

Basically, use an unambiguous blatantly long name for your function
prototype, and do a search-and-replace to substitute that with the
desired function name.

Note that you still need to have a separate stub per function signature;
so if you want users to be able to load functions of arbitrary
signature, you probably need to make that a template parameter and have
the user pass in the desired function signature. For example:

auto loadPluginFunction(Signature)(string library, string funcName)
{
import std.traits : ReturnType, ParameterTypeTuple;
import std.regex : regex, replaceFirst;

// Declare a static function with the user-desired
// signature, with a nicely-substitutable name
static ReturnType!Signature
pluginFuncStubName(ParameterTypeTuple!Signature);
auto r = regex(`pluginFuncStubName`);
auto symbol = pluginFuncStubName.mangleof.replaceFirst(r, 
funcName);

// Proof of concept
import std.stdio;
writeln(symbol);

// ... Call dlsym to find function here

// Just to make this compile, replace with real function 
pointer in
// your code here.
return null;
}

void main() {
auto f1 = loadPluginFunction!(int 
function(string,int))(mylib, func1);
auto f2 = loadPluginFunction!(void function(float))(mylib, 
func2);
}

Output:


_D4test33__T18loadPluginFunctionTPFAyaiZiZ18loadPluginFunctionFAyaAyaZ18func1FAyaiZi

_D4test30__T18loadPluginFunctionTPFfZvZ18loadPluginFunctionFAyaAyaZ18func2FfZv

This example isn't complete yet (you need to do something about the
loadPluginFunction component in the mangled name), but you should be
able to work out a way of producing the correct mangled name from here.
But at least it demonstrates how you can have a very nice API for your
users -- they just pass in the function prototype of the function they
want, and the library code takes care of deriving the correct mangled
names.

Hope this helps.


T

-- 
Just because you can, doesn't mean you should.


Re: Generating code based on UDA

2014-10-25 Thread Ali Çehreli via Digitalmars-d-learn

On 10/25/2014 07:45 AM, Rares Pop wrote:

On Saturday, 25 October 2014 at 13:53:35 UTC, Adam D. Ruppe wrote:

On Saturday, 25 October 2014 at 13:45:29 UTC, Rares Pop wrote:

What do you mean by copy/pasting ?


I literally copied the code in your post (and fixed a missing
semicolon) and got it to compile.

Passing A as an argument to injections should work. You can also use
this and typeof(this) inside the injections template code to access
the class. It should all work.


Taking this one step further, it looks like the attributes are not
available at the mixin scope.
Here is my code:

--

struct Inject {
//immutable Scope scoped;
}

static string injections(T)()
{
 pragma(msg, injections for : , T);
 string result;
 foreach(member; __traits(allMembers,T))
 {


import std.string;


 enum fullName = format(%s.%s, T.stringof, member);
 pragma(msg, member: , fullName);
 auto attributes = __traits(getAttributes, fullName);


You must mixin fullName:

  auto attributes = __traits(getAttributes, mixin(fullName));


 enum dbg_msg = format (%s attributes are %s, fullName,
attributes.stringof);
 pragma(msg, dbg_msg);
 foreach(attr;attributes){


Replace the body of this foreach with the following:

pragma(msg, Checking attribute of type, typeof(attr));
static if (is (typeof(attr) == Inject)) {
pragma(msg, Found one);

// Let's inject something:
result ~= q{
int foo() {
return 42;
}
};
}


 pragma(msg, Checking attribute, attr);
 }

 }
 return result;
}

class A {

 this(){
 }
}

class B {

 @Inject A a;


For an unknown reason to me, that UDA wants an Inject object, not the 
type itself:


@Inject() A a;

I am puzzled with that...



 mixin(injections!(B));

}


Then it works with the following main:

void main()
{
auto b = new B();
assert(b.foo() == 42);// It worked! :)
}

Here is the complete program:

struct Inject {
//immutable Scope scoped;
}

static string injections(T)()
{
pragma(msg, injections for : , T);
string result;
foreach(member; __traits(allMembers,T))
{
import std.string;

enum fullName = format(%s.%s, T.stringof, member);
pragma(msg, member: , fullName);
auto attributes = __traits(getAttributes, mixin(fullName));
enum dbg_msg = format (%s attributes are %s, fullName, 
attributes.stringof);

pragma(msg, dbg_msg);
foreach(attr;attributes){
pragma(msg, Checking attribute of type, typeof(attr));
static if (is (typeof(attr) == Inject)) {
pragma(msg, Found one);

// Let's inject something:
result ~= q{
int foo() {
return 42;
}
};
}
}
}
return result;
}

class A {

this(){
}
}

class B {

@Inject() A a;

mixin(injections!(B));

}

void main()
{
auto b = new B();
assert(b.foo() == 42);// It worked! :)
}

Ali



Re: Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

Ali,
Many thanks for your help.

What is the rationale for mixin(fullName) ?






Re: Pragma mangle and D shared objects

2014-10-25 Thread Etienne Cimon via Digitalmars-d-learn
That looks like exactly the solution I need, very clever. It'll take 
some time to wrap my head around it :-P


Re: Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

Ali,
Many thanks for your help.
Indeed it worked. What is the rationale behind the 
mixin(fullName) ?


However, in my project the injections function, the @Inject UDA 
struct and some other dependencies are defined in a library 
(libinfuse).


In this format the compiler gives the undefined identifier error 
I was mentioning in my first post.
source/infuse/injector.d-mixin-148(148): Error: undefined 
identifier B


From what I read in the documentation, source mixins should have 
the instantiation scope.

Is this a dmd compiler bug?

Thanks again for your input guys,
Rares


Re: Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

I've uploaded the code here:
https://github.com/fusionbeam/infuse


Re: Two cases for improving error messages

2014-10-25 Thread Shriramana Sharma via Digitalmars-d-learn
On Sat, Oct 25, 2014 at 8:00 PM, H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:

 I agree with submitting both of these as enhancement requests. Please
 tag them with diagnostic in the keywords field.

Done:

https://issues.dlang.org/show_bug.cgi?id=13655
https://issues.dlang.org/show_bug.cgi?id=13656

You did say *requests* in the plural so I filed them separately.

-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा



Re: Pragma mangle and D shared objects

2014-10-25 Thread Etienne Cimon via Digitalmars-d-learn

On 2014-10-25 11:56, Etienne Cimon wrote:

That looks like exactly the solution I need, very clever. It'll take
some time to wrap my head around it :-P


Just brainstorming here, but I think every dynamic library should hold a 
utility container (hash map?) that searches for and returns the mangled 
names in itself using regex match. This container would always be in the 
same module/function name in every dynamic library.


The mangling list would load itself using introspection through a 
`shared static this()`. For each module, for each class, insert mangling 
in the hashmap...


This seems ideal because then you basically have libraries that document 
themselves at runtime.


Of course, the return type and arguments would have to be decided in 
advance, but otherwise it allows loading and use of (possibly remote and 
unknown) DLLs, in a very simple way.


Re: Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

On Saturday, 25 October 2014 at 16:01:29 UTC, Rares Pop wrote:

I've uploaded the code here:
https://github.com/fusionbeam/infuse


compiling with ldc2 exhibits the same behaviour.
'Error: undefined identifier B'


Does it make sense to add attribute to operator overload functions ?

2014-10-25 Thread Jkpl via Digitalmars-d-learn
Everything is in the Q. I ask this because those functions are 
hidden behind symbols and keywords (+=, ~, in, etc.). It's not 
that obvious for a user who would write a custom type.


e.g:
---
struct myType
{
@safe nothrow opIndexAssign(t1 paramValue,t2 paramIndex){}
}
---

are the attributes necessary ? Are opXXX functions handled just 
like any other functs ? (traversal compat. of the attribs)


Re: Does it make sense to add attribute to operator overload functions ?

2014-10-25 Thread John Colvin via Digitalmars-d-learn

On Saturday, 25 October 2014 at 17:14:51 UTC, Jkpl wrote:
Everything is in the Q. I ask this because those functions are 
hidden behind symbols and keywords (+=, ~, in, etc.). It's not 
that obvious for a user who would write a custom type.


e.g:
---
struct myType
{
@safe nothrow opIndexAssign(t1 paramValue,t2 paramIndex){}
}
---

are the attributes necessary ? Are opXXX functions handled just 
like any other functs ? (traversal compat. of the attribs)


In every aspect they are ordinary functions, except they each 
have an additional unique way of being called (the relevant 
operator syntax).


In short, yes.


Re: Pragma mangle and D shared objects

2014-10-25 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Oct 25, 2014 at 12:15:19PM -0400, Etienne Cimon via Digitalmars-d-learn 
wrote:
 On 2014-10-25 11:56, Etienne Cimon wrote:
 That looks like exactly the solution I need, very clever. It'll take
 some time to wrap my head around it :-P

It's not that complicated, really. It's basically putting together 3
things:

1) .mangleof to extract a mangled symbol from some function declaration
2) regex to replace the function name in the mangled symbol
3) compile-time introspection to build a function declaration out of a
   user-specified signature.


 Just brainstorming here, but I think every dynamic library should hold
 a utility container (hash map?) that searches for and returns the
 mangled names in itself using regex match. This container would always
 be in the same module/function name in every dynamic library.

Actually, the object file (library) itself should already have a list of
exported symbols; you could then use core.demangle to extract the
function signatures from the mangled symbols and construct a hash of all
exported symbols and their types. The only thing is, I don't know of any
cross-platform method of retrieving the list of exported symbols -- the
Posix dlsym() family of functions only allow lookup by explicit symbol
name, no iteration primitives are specified. But the information is
definitely there, since that's how the OS's dynamic linker figures out
how to link dynamic libraries in the first place!


[...]
 Of course, the return type and arguments would have to be decided in
 advance, but otherwise it allows loading and use of (possibly remote
 and unknown) DLLs, in a very simple way.

Well, generally, in order to make use of the functions in the first
place, you'd need some kind of pre-determined return type and parameter
types, otherwise the program couldn't possibly know how to pass
arguments or interpret the return value!

But if you could extract the list of exported symbols from a library,
then you could demangle them to determine their signatures, and thereby
find all symbols matching some given signature.


T

-- 
Curiosity kills the cat. Moral: don't be the cat.


Re: Two cases for improving error messages

2014-10-25 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/25/14 7:44 AM, Shriramana Sharma via Digitalmars-d-learn wrote:

Hello. Please see the following and say whether they're OK to submit
as bugs for improving the error messages. Thanks.

ref int foo(ref int x) { return x ; }

void main () {
 foo(3) ;
// Error: function rvalue_argument.foo (ref int x) is not callable
using argument types (int)
// Comment: argument ref int x of function rvalue_argument.foo cannot
bind to an rvalue would be clearer IMO

 int i ;
 ref ir = i ;
// Error: variable ref_type.main.ir only parameters or foreach
declarations can be ref
// Comment: add , return values after parameters
}



I think both are clearer, please submit bugs!

BTW, don't be shy about submitting bugs, most of the devs watch the bug 
list and pick up on things that are just not going to happen. Worst case 
is that your bug just gets closed as wontfix.


-Steve


Re: Does it make sense to add attribute to operator overload functions ?

2014-10-25 Thread Jkpl via Digitalmars-d-learn

On Saturday, 25 October 2014 at 18:38:12 UTC, John Colvin wrote:

On Saturday, 25 October 2014 at 17:14:51 UTC, Jkpl wrote:
Everything is in the Q. I ask this because those functions are 
hidden behind symbols and keywords (+=, ~, in, etc.). It's not 
that obvious for a user who would write a custom type.


e.g:
---
struct myType
{
   @safe nothrow opIndexAssign(t1 paramValue,t2 paramIndex){}
}
---

are the attributes necessary ? Are opXXX functions handled 
just like any other functs ? (traversal compat. of the attribs)


In every aspect they are ordinary functions, except they each 
have an additional unique way of being called (the relevant 
operator syntax).


In short, yes.


Thx, A bit less confused by them now.



HTML Parsing lib

2014-10-25 Thread Suliman via Digitalmars-d-learn

I found only https://github.com/Bystroushaak/DHTMLParser

But I can't get it work:
C:\Users\Dima\Downloads\DHTMLParser-master\DHTMLParser-masterdmd 
find_links.d

OPTLINK (R) for Win32  Release 8.00.15
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
find_links.obj(find_links)
 Error 42: Symbol Undefined 
_D11dhtmlparser11parseStringFAyaZC11dhtmlparser11HTM

LElement
find_links.obj(find_links)
 Error 42: Symbol Undefined _D11dhtmlparser12__ModuleInfoZ
--- errorlevel 2

Is there any other HTML parsing lib, or maybe someone do know how 
to get it's work. Look like it's not compatible with current 
version of DMD


Re: HTML Parsing lib

2014-10-25 Thread MrSmith via Digitalmars-d-learn

On Saturday, 25 October 2014 at 19:46:01 UTC, MrSmith wrote:

On Saturday, 25 October 2014 at 19:44:25 UTC, Suliman wrote:

I found only https://github.com/Bystroushaak/DHTMLParser

But I can't get it work:
C:\Users\Dima\Downloads\DHTMLParser-master\DHTMLParser-masterdmd 
find_links.d

OPTLINK (R) for Win32  Release 8.00.15
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
find_links.obj(find_links)
Error 42: Symbol Undefined 
_D11dhtmlparser11parseStringFAyaZC11dhtmlparser11HTM

LElement
find_links.obj(find_links)
Error 42: Symbol Undefined _D11dhtmlparser12__ModuleInfoZ
--- errorlevel 2

Is there any other HTML parsing lib, or maybe someone do know 
how to get it's work. Look like it's not compatible with 
current version of DMD


You need to pass a library to compiler as well (all its files 
or .lib/.a file) if it is compiled as static library


You can try
dmd find_links.d dhtmlparser.d quote_escaper.d


Re: HTML Parsing lib

2014-10-25 Thread MrSmith via Digitalmars-d-learn

On Saturday, 25 October 2014 at 19:44:25 UTC, Suliman wrote:

I found only https://github.com/Bystroushaak/DHTMLParser

But I can't get it work:
C:\Users\Dima\Downloads\DHTMLParser-master\DHTMLParser-masterdmd 
find_links.d

OPTLINK (R) for Win32  Release 8.00.15
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
find_links.obj(find_links)
 Error 42: Symbol Undefined 
_D11dhtmlparser11parseStringFAyaZC11dhtmlparser11HTM

LElement
find_links.obj(find_links)
 Error 42: Symbol Undefined _D11dhtmlparser12__ModuleInfoZ
--- errorlevel 2

Is there any other HTML parsing lib, or maybe someone do know 
how to get it's work. Look like it's not compatible with 
current version of DMD


You need to pass a library to compiler as well (all its files or 
.lib/.a file) if it is compiled as static library


Re: HTML Parsing lib

2014-10-25 Thread Suliman via Digitalmars-d-learn


You need to pass a library to compiler as well (all its files 
or .lib/.a file) if it is compiled as static library


You can try
dmd find_links.d dhtmlparser.d quote_escaper.d



C:\Users\Dima\Downloads\DHTMLParser-master\DHTMLParser-masterdmd 
find_links.d quote_escaper.d

OPTLINK (R) for Win32  Release 8.00.15
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
find_links.obj(find_links)
 Error 42: Symbol Undefined 
_D11dhtmlparser11parseStringFAyaZC11dhtmlparser11HTM

LElement
find_links.obj(find_links)
 Error 42: Symbol Undefined _D11dhtmlparser12__ModuleInfoZ
--- errorlevel 2


Re: HTML Parsing lib

2014-10-25 Thread Suliman via Digitalmars-d-learn

On Saturday, 25 October 2014 at 19:51:48 UTC, Suliman wrote:


You need to pass a library to compiler as well (all its files 
or .lib/.a file) if it is compiled as static library


You can try
dmd find_links.d dhtmlparser.d quote_escaper.d



C:\Users\Dima\Downloads\DHTMLParser-master\DHTMLParser-masterdmd 
find_links.d quote_escaper.d

OPTLINK (R) for Win32  Release 8.00.15
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
find_links.obj(find_links)
 Error 42: Symbol Undefined 
_D11dhtmlparser11parseStringFAyaZC11dhtmlparser11HTM

LElement
find_links.obj(find_links)
 Error 42: Symbol Undefined _D11dhtmlparser12__ModuleInfoZ
--- errorlevel 2


Sorry I missed dhtmlparser.d


Re: HTML Parsing lib

2014-10-25 Thread Suliman via Digitalmars-d-learn

How I can build such App with DUB?



Re: HTML Parsing lib

2014-10-25 Thread MrSmith via Digitalmars-d-learn

On Saturday, 25 October 2014 at 19:55:10 UTC, Suliman wrote:

How I can build such App with DUB?


Unfortunately that library has no dub package.
But you can include it in your project.

See info here http://code.dlang.org/package-format


Re: Pragma mangle and D shared objects

2014-10-25 Thread John Colvin via Digitalmars-d-learn
On Saturday, 25 October 2014 at 18:40:23 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
Actually, the object file (library) itself should already have 
a list of
exported symbols; you could then use core.demangle to extract 
the
function signatures from the mangled symbols and construct a 
hash of all
exported symbols and their types. The only thing is, I don't 
know of any
cross-platform method of retrieving the list of exported 
symbols -- the
Posix dlsym() family of functions only allow lookup by explicit 
symbol
name, no iteration primitives are specified. But the 
information is
definitely there, since that's how the OS's dynamic linker 
figures out

how to link dynamic libraries in the first place!

T


I wonder what nm uses. AFAIK it works on everything posix.


Re: HTML Parsing lib

2014-10-25 Thread Adam D. Ruppe via Digitalmars-d-learn

Another option for html is my dom.d

https://github.com/adamdruppe/arsd

get dom.d and characterencodings.d in your project directory.

compile with dmd yourfile.d dom.d characterencodings.d

here's an example:

import arsd.dom;

void main() {
   auto document = new Document();

   // The example document will be defined inline here
   // We could also load the string from a file with
   // std.file.readText or the web with std.net.curl.get
   document.parseGarbage(`htmlhead
 meta name=author content=Adam D. Ruppe
 titleTest Document/title
   /head
   body
 pThis is the first paragraph of our a
href=test.htmltest document/a.
 pThis second paragraph also has a a
href=test2.htmllink/a.
 p id=custom-paragraphOld text/p
   /body
   /html`);

   import std.stdio;
   // retrieve and print some meta information
   writeln(document.title);
   writeln(document.getMeta(author));
   // show a paragraph’s text
   writeln(document.requireSelector(p).innerText);
   // modify all links
   document[a[href]].setValue(source, your-site);
   // change some html
   document.requireElementById(custom-paragraph).innerHTML =
New bHTML/b!;
   // show the new document
   writeln(document.toString());
}




You can replace the html string with something like
std.file.readText(yourfile.html); too


My library is meant to give an api similar to javascript.


I don't use dub so idk about how to use that, I just recommend
adding my files to your project if you wanna try it.


Re: Generating code based on UDA

2014-10-25 Thread Ali Çehreli via Digitalmars-d-learn

On 10/25/2014 08:56 AM, Rares Pop wrote:

 Indeed it worked. What is the rationale behind the mixin(fullName) ?

__traits(getAttributes) requires a symbol but fullName is a string. 
Mixing it in as code fulfills the requirement.


 However, in my project the injections function, the @Inject UDA struct
 and some other dependencies are defined in a library (libinfuse).

I am afraid it needs to be changed. :-/

 In this format the compiler gives the undefined identifier error I was
 mentioning in my first post.
 source/infuse/injector.d-mixin-148(148): Error: undefined identifier B

I found two solutions:

a) Do not define 'attributes' at all and use __traits(getAttributes) 
directly in the foreach loop:


foreach (attr; __traits(getAttributes, mixin(fullName))) {

b) Define attributes as a typeof of __traits(getAttributes) and use that 
in the foreach loop:


alias attributes = typeof(__traits(getAttributes, mixin(fullName)));
foreach (attr; attributes) {

I think what happens in both cases is that the entity that we iterate 
over maintains its TypeTuple'ness without trying to produce a value out 
of its members.


 Is this a dmd compiler bug?

I don't know but it is very confusing.

Ali



Where is a variable declared in a module allocated?

2014-10-25 Thread MachineCode via Digitalmars-d-learn
Where is a variable declared in a module allocated? is it same as 
a C's global?


for example:

module foo;
int myvar;


Re: Where is a variable declared in a module allocated?

2014-10-25 Thread John Colvin via Digitalmars-d-learn

On Saturday, 25 October 2014 at 21:52:13 UTC, MachineCode wrote:
Where is a variable declared in a module allocated? is it same 
as a C's global?


for example:

module foo;
int myvar;


that is in thread local storage.

__shared, shared or immutable cause the variable to be in classic 
global storage like in C.


See: http://dlang.org/migrate-to-shared.html


Re: Pragma mangle and D shared objects

2014-10-25 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Oct 25, 2014 at 08:05:18PM +, John Colvin via Digitalmars-d-learn 
wrote:
 On Saturday, 25 October 2014 at 18:40:23 UTC, H. S. Teoh via
 Digitalmars-d-learn wrote:
 Actually, the object file (library) itself should already have a list
 of exported symbols; you could then use core.demangle to extract the
 function signatures from the mangled symbols and construct a hash of
 all exported symbols and their types. The only thing is, I don't know
 of any cross-platform method of retrieving the list of exported
 symbols -- the Posix dlsym() family of functions only allow lookup by
 explicit symbol name, no iteration primitives are specified. But the
 information is definitely there, since that's how the OS's dynamic
 linker figures out how to link dynamic libraries in the first place!
 
 T
 
 I wonder what nm uses. AFAIK it works on everything posix.

Not sure what nm uses, but a lot of posix tools for manipulating object
files are based on binutils, which understands the local system's object
file format and deal directly with the binary representation. The
problem is, I don't know of any *standard* system functions that can do
this, so you'd have to rely on OS-specific stuff to make it work, which
is less than ideal.


T

-- 
Indifference will certainly be the downfall of mankind, but who cares? -- 
Miquel van Smoorenburg


Re: Pragma mangle and D shared objects

2014-10-25 Thread Etienne Cimon via Digitalmars-d-learn

On 2014-10-25 21:26, H. S. Teoh via Digitalmars-d-learn wrote:

Not sure what nm uses, but a lot of posix tools for manipulating object
files are based on binutils, which understands the local system's object
file format and deal directly with the binary representation. The
problem is, I don't know of any *standard* system functions that can do
this, so you'd have to rely on OS-specific stuff to make it work, which
is less than ideal.



Which makes it better to export the mangling into a container at 
compile-time! That way, you can build a standard interface into DLLs so 
that other D application know what they can call =)




Re: Pragma mangle and D shared objects

2014-10-25 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Oct 25, 2014 at 10:54:53PM -0400, Etienne Cimon via Digitalmars-d-learn 
wrote:
 On 2014-10-25 21:26, H. S. Teoh via Digitalmars-d-learn wrote:
 Not sure what nm uses, but a lot of posix tools for manipulating
 object files are based on binutils, which understands the local
 system's object file format and deal directly with the binary
 representation. The problem is, I don't know of any *standard* system
 functions that can do this, so you'd have to rely on OS-specific
 stuff to make it work, which is less than ideal.
 
 
 Which makes it better to export the mangling into a container at
 compile-time! That way, you can build a standard interface into DLLs
 so that other D application know what they can call =)

Hmm. You can probably use __traits(getAllMembers...) to introspect a
library module at compile-time and build a hash based on that, so that
it's completely automated. If you have this available as a mixin, you
could just mixin(exportLibrarySymbols()) in your module to produce the
hash.


T

-- 
Holy war is an oxymoron. -- Lazarus Long


Re: Generating code based on UDA

2014-10-25 Thread Rares Pop via Digitalmars-d-learn

I think it is a bug.
Executing linked code from a mixin statement should not reduce 
the scope of the mixin, IMHO.


I will file a bug report.

On Saturday, 25 October 2014 at 21:35:44 UTC, Ali Çehreli wrote:

On 10/25/2014 08:56 AM, Rares Pop wrote:

 Indeed it worked. What is the rationale behind the
mixin(fullName) ?

__traits(getAttributes) requires a symbol but fullName is a 
string. Mixing it in as code fulfills the requirement.


 However, in my project the injections function, the @Inject
UDA struct
 and some other dependencies are defined in a library
(libinfuse).

I am afraid it needs to be changed. :-/

 In this format the compiler gives the undefined identifier
error I was
 mentioning in my first post.
 source/infuse/injector.d-mixin-148(148): Error: undefined
identifier B

I found two solutions:

a) Do not define 'attributes' at all and use 
__traits(getAttributes) directly in the foreach loop:


foreach (attr; __traits(getAttributes, mixin(fullName))) {

b) Define attributes as a typeof of __traits(getAttributes) and 
use that in the foreach loop:


alias attributes = typeof(__traits(getAttributes, 
mixin(fullName)));

foreach (attr; attributes) {

I think what happens in both cases is that the entity that we 
iterate over maintains its TypeTuple'ness without trying to 
produce a value out of its members.


 Is this a dmd compiler bug?

I don't know but it is very confusing.

Ali