Re: isCallable fails

2021-01-20 Thread frame via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 21:57:59 UTC, Paul Backus wrote:

On Wednesday, 20 January 2021 at 19:01:19 UTC, frame wrote:

Oh, look, it compiles with no errors. All that effort, and I 
*still* couldn't reproduce the issue you described in your 
original post. Guess I wasted my time for nothing!


Ok, really thank you for your effort - but I was talking about 
the error first (did you even read it?). I have explained that 
isCallable gets valid data and not void. I was asking if someone 
has encountered this kind of error before and can give my a hint 
what *could* going on.


I was not asking here to re-produce my code or debug since it 
cannot provide a snippet if I do not know what could cause the 
error.


The example code just should illustrate that isCallable is called 
on a valid way and "alias this" may cause the problem (as I 
assumed) - that's all.


Sorry, but you "wasting your time" more by unnecessarily 
commenting which isn't appropriate here.


--

So back to topic:

The error is a circular dependency (and no, it's not just in 
loadAllMatched!TDobj, it's something deeper). "alias get this" 
leads to parsing get() when isCallable is called. Is there any 
way to avoid running in this issue?





Re: Issue with socket recieve

2021-01-20 Thread Tim via Digitalmars-d-learn

On Thursday, 21 January 2021 at 03:35:05 UTC, Adam D. Ruppe wrote:

On Thursday, 21 January 2021 at 03:24:13 UTC, Tim wrote:

No, I don't. It should be all garbage collected right?


Yeah, but that's where the problem comes.

Note that by destructor, I mean *any* function in your code 
called `~this() {}`. If there are any and they call a memory 
allocation function, that's how you get the 
InvalidMemoryOperationError.


I'd have to look up if there's any other causes of that...


I am aware of this, there is absolutely no ~this in *my* code


Re: Issue with socket recieve

2021-01-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 21 January 2021 at 03:24:13 UTC, Tim wrote:

No, I don't. It should be all garbage collected right?


Yeah, but that's where the problem comes.

Note that by destructor, I mean *any* function in your code 
called `~this() {}`. If there are any and they call a memory 
allocation function, that's how you get the 
InvalidMemoryOperationError.


I'd have to look up if there's any other causes of that...




Re: Issue with socket recieve

2021-01-20 Thread Tim via Digitalmars-d-learn

On Thursday, 21 January 2021 at 03:21:41 UTC, Adam D. Ruppe wrote:

On Wednesday, 20 January 2021 at 21:31:54 UTC, Tim wrote:
Unable to open 'recv.c': Unable to read file 
'/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c' (Error: Unable to resolve non-existing file '/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c').

[snip]
generate a core.exception.InvalidMemoryOperationError that I 
can't catch.



None of this makes much sense given the code you provided. 
InvalidMemoryOperationError (the scariest thing in D btw, such 
a pain to debug) is usually caused by a class destructor 
somewhere, and none of those should be trying to resolve those 
files.


Do you have any destructors defined?


You seem like a rather switched-on fellow. Would you be able to 
send me an email at some point at tim.oli...@tutanota.com. I have 
a proposition for you.


Re: Issue with socket recieve

2021-01-20 Thread Tim via Digitalmars-d-learn

On Thursday, 21 January 2021 at 03:21:41 UTC, Adam D. Ruppe wrote:

On Wednesday, 20 January 2021 at 21:31:54 UTC, Tim wrote:
Unable to open 'recv.c': Unable to read file 
'/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c' (Error: Unable to resolve non-existing file '/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c').

[snip]
generate a core.exception.InvalidMemoryOperationError that I 
can't catch.



None of this makes much sense given the code you provided. 
InvalidMemoryOperationError (the scariest thing in D btw, such 
a pain to debug) is usually caused by a class destructor 
somewhere, and none of those should be trying to resolve those 
files.


Do you have any destructors defined?


No, I don't. It should be all garbage collected right?

Hahahaha, about 50% of my troubles are 
InvalidMemoryOperationError! It's so frustrating. This whole 
project has been a nightmare


Re: Issue with socket recieve

2021-01-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 21:31:54 UTC, Tim wrote:
Unable to open 'recv.c': Unable to read file 
'/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c' 
(Error: Unable to resolve non-existing file 
'/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c').

[snip]
generate a core.exception.InvalidMemoryOperationError that I 
can't catch.



None of this makes much sense given the code you provided. 
InvalidMemoryOperationError (the scariest thing in D btw, such a 
pain to debug) is usually caused by a class destructor somewhere, 
and none of those should be trying to resolve those files.


Do you have any destructors defined?


Re: Exit code -4

2021-01-20 Thread Tim via Digitalmars-d-learn

On Thursday, 21 January 2021 at 01:07:22 UTC, Paul Backus wrote:

On Thursday, 21 January 2021 at 00:49:26 UTC, Tim wrote:


Oh, so it's just signal 4, not -4?


The signal is 4. The exit status is -4.

https://en.wikipedia.org/wiki/Signal_(IPC)#POSIX_signals
https://en.wikipedia.org/wiki/Exit_status#POSIX


Those lines were super useful! Not sure why I couldn't find that 
info on my own ...


Re: Exit code -4

2021-01-20 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 21 January 2021 at 00:49:26 UTC, Tim wrote:


Oh, so it's just signal 4, not -4?


The signal is 4. The exit status is -4.

https://en.wikipedia.org/wiki/Signal_(IPC)#POSIX_signals
https://en.wikipedia.org/wiki/Exit_status#POSIX


Re: Exit code -4

2021-01-20 Thread Tim via Digitalmars-d-learn

On Thursday, 21 January 2021 at 00:47:36 UTC, Adam D. Ruppe wrote:

On Thursday, 21 January 2021 at 00:37:19 UTC, Tim wrote:

Hi all,

From time to time my program crashes with exit code -4. I 
can't seem to find much on the code. Does anyone know what 
this means and how to debug the issue?


Unix signal #4 is illegal instruction (negative returns usually 
mean it was killed by that number signal). Most common way to 
hit that is a broken function pointer or corrupted class 
leading into data that isn't supposed to be executed as code. 
Also possible an assert(0) got hit in release mode or something 
like that.


Easiest way to debug it is to open it in a debugger and look at 
the stack trace to see where it came from.


Oh, so it's just signal 4, not -4?


Re: Exit code -4

2021-01-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 21 January 2021 at 00:37:19 UTC, Tim wrote:

Hi all,

From time to time my program crashes with exit code -4. I can't 
seem to find much on the code. Does anyone know what this means 
and how to debug the issue?


Unix signal #4 is illegal instruction (negative returns usually 
mean it was killed by that number signal). Most common way to hit 
that is a broken function pointer or corrupted class leading into 
data that isn't supposed to be executed as code. Also possible an 
assert(0) got hit in release mode or something like that.


Easiest way to debug it is to open it in a debugger and look at 
the stack trace to see where it came from.


Exit code -4

2021-01-20 Thread Tim via Digitalmars-d-learn

Hi all,

From time to time my program crashes with exit code -4. I can't 
seem to find much on the code. Does anyone know what this means 
and how to debug the issue?


Re: isCallable fails

2021-01-20 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 20, 2021 at 09:57:59PM +, Paul Backus via Digitalmars-d-learn 
wrote:
> On Wednesday, 20 January 2021 at 19:01:19 UTC, frame wrote:
> > On Wednesday, 20 January 2021 at 13:11:09 UTC, Paul Backus
> > > 
> > > Please post an example with enough code to actually produce the
> > > error you're seeing.
> > 
> > I don't know when to stop posting code then :(
> 
> You should stop (and ideally start) with a Minimal, Complete,
> Verifiable Example:
> 
> https://idownvotedbecau.se/nomcve/
[...]

And if you're not sure how to arrive at such an example, DustMite is one
way of doing it:

https://github.com/CyberShadow/DustMite/wiki


--T


Re: isCallable fails

2021-01-20 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 19:01:19 UTC, frame wrote:

On Wednesday, 20 January 2021 at 13:11:09 UTC, Paul Backus


Please post an example with enough code to actually produce 
the error you're seeing.


I don't know when to stop posting code then :(


You should stop (and ideally start) with a Minimal, Complete, 
Verifiable Example:


https://idownvotedbecau.se/nomcve/

Just to illustrate why this matters, let me walk you through the 
steps it takes me to try and figure out what your issue is from 
just the posts you've made so far.


---

Step 1: go through each of your posts and copy each of the 
individual code snippets into a run.dlang.io window.


Result: https://run.dlang.io/is/nidfug

This code compiles--because it's all templates, and you haven't 
actually provided any example of the code that *uses* these 
templates.


---

Step 2: look at the comments and try to guess what the usage code 
must look like.


Result: https://run.dlang.io/is/P7Pcr1

First, we need to define a class with a member named `data` to 
pass as an argument to something. And data's type has to be 
`foo!bar`, so we need a type named `bar` to use for that.


What kind of type? Well, if we look at how `bar` is used in 
`foo`, we can see that it's compared with `null`, so we know it's 
a reference type--probably a class.


Now we can finally attempt to instantiate `something`. What 
happens when we do?


Error: template instance loadAllMatched!TDobj template 
loadAllMatched is not defined


Great, another piece of missing code.

---

Step 3: try to stub out loadAllMatched.

Result: https://run.dlang.io/is/uRj6HW

Error: template instance `isCallable!S` template `isCallable` 
is not defined


Sigh.

---

Step 4: add the missing import.

Result: https://run.dlang.io/is/2by7sU

Oh, look, it compiles with no errors. All that effort, and I 
*still* couldn't reproduce the issue you described in your 
original post. Guess I wasted my time for nothing!


Re: isCallable fails

2021-01-20 Thread frame via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 19:01:19 UTC, frame wrote:

It also compiles if

value = new T


enum R = isCallable!S

throws:

Error: cannot infer type from template instance 
isCallable!(data), possible circular dependency


So that seems to be the problem. There is a circular dependency 
somewhere the compiler cannot resolve.


I believe it has something to do with "alias get this" so get() 
is parsed in this context or whatever. Any suggestions?





Issue with socket recieve

2021-01-20 Thread Tim via Digitalmars-d-learn

Hi all,

I'm having a really terrible bug that seemed to come from nowhere 
and is really hard to narrow down. I have a threaded message 
service that works via local TcpSocket. Every time I run it, 
either an error saying:
Unable to open 'recv.c': Unable to read file 
'/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c' 
(Error: Unable to resolve non-existing file 
'/build/glibc-ZN95T4/glibc-2.31/sysdeps/unix/sysv/linux/recv.c').
when socket.receive() is called. Sometimes I get the same thing 
but for socket.accept() instead (different file). This seems to 
generate a core.exception.InvalidMemoryOperationError that I 
can't catch.


Yesterday, this came up after a couple of minutes of the program 
running, now it happens straight away. I haven't touched this 
class in a week or so and can't think of why all of a sudden it 
has come up with this issue. Code dump below


-



module message_service;

import std.stdio;
import std.socket;
import core.thread;


/**
Threaded socket manager to serve inter-service communication

Calls delegates when the appropriate command is received
*/
class MessageService : Thread{
   /// The socket over which communication is possible
   private Socket server;
   /// The connection to the client that sends commands
   private Socket client;
   /// Associative array of command strings and relative 
functions to call

   private void delegate()[string] commands;
   /// Global buffer. Holds the command being recieved
   private char[1024] buffer = 0;


   ///
   this(ushort port, void delegate()[string] _commands){
   commands = _commands;
   server = new TcpSocket();
   server.setOption(SocketOptionLevel.SOCKET, 
SocketOption.REUSEADDR, true);

   server.bind(new InternetAddress(port));
   writefln("Message service started on port %d", port);
   super();
   start();
   }


   /// Loops over polling the client socket for commands
   void mainLoop(){
   while(true){
   pollMessages();
   }
   }


   /**
   Polls the client socket for characters to build up the 
message buffer


   When a string command is found, it calls the appropriate 
function

   */
   void pollMessages(){
   server.listen(1);
   if(client is null) client = server.accept();

   // Add to the message buffer
   auto buffLength = client.receive(buffer);

   if(buffLength){
   auto message = cast(string) buffer[0 .. buffLength];

   foreach(cmd; commands.byKey){
   if(cmd == message){
   // Reset the message buffer
   buffer = 0;
   commands[cmd]();
   return;
   }
   }
   }
   }
}


Re: How to use dguihub package ?

2021-01-20 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 20 January 2021 at 19:05:29 UTC, Vinod K Chandran 
wrote:

On Tuesday, 19 January 2021 at 16:52:18 UTC, Paul Backus wrote:
On Tuesday, 19 January 2021 at 16:22:35 UTC, Vinod K Chandran 
wrote:


b ? (tbinfo.fsState |= TBSTATE_ENABLED) : (tbinfo.fsState 
&= ~TBSTATE_ENABLED);


This means, "if b is true, set the TBSTATE_ENABLED flag to 
true; otherwise, set it to false."



Hi Paul Backus,
Thanks for the detailed reply. After reading your reply, I got 
the idea. But think there is one silly mistake in your reply. 
Forgive me if I am wrong.

Instead of
"if b is true, set the TBSTATE_ENABLED flag to true; otherwise, 
set it to false."


This is the meaning of that code.
if (b == true) {tbinfo.fsState = true ; } else {tbinfo.fsState 
= false;}


Because, TBSTATE_ENABLED is a  manifest constant and I cannot 
modify it.

What about simply writing this --
"tbinfo.fsState = b ; " This also worked.


Not quite. If you print out TBSTATE_ENABLED in binary, you will 
see that it is an integer with exactly one bit set to 1, and all 
others set to 0. You can do this with the following line of code:


writefln("%032b", TBSTATE_ENABLED);

By "the TBSTATE_ENABLED flag", I do not mean "the constant 
TBSTATE_ENABLED". What I mean is "the bit in tbinfo.fsState at 
the same position as the 1 bit in TBSTATE_ENABLED". To be 
clearer, I should have said something like "the 'enabled' flag in 
tbinfo.fsState".


If tbinfo.fsState were a struct instead of a bitfield:

struct FsState
{
bool enabled;
// other flags...
}

...then the meaning of the code would be:

if (b == true) {
tbinfo.fsState.enabled = true;
} else {
tbinfo.fsState.enabled = false;
}

Or more concisely:

tbinfo.fsState.enabled = b;

Of course, in reality, tbinfo.fsState is a bitfield, not a 
struct, so we cannot access the individual flags with syntax like 
`.enabled`. Instead, we have to use bitwise operators. But 
conceptually, it's the same thing.


By contrast, in your proposed version:

tbinfo.fsState = b;

...you are overwriting *all* of the flags at once, rather than 
just one of them. Even if this happens to work by coincidence 
(because "enabled" is the first flag), it will certainly cause 
you problems later on.


Re: How to use dguihub package ?

2021-01-20 Thread Vinod K Chandran via Digitalmars-d-learn

On Tuesday, 19 January 2021 at 16:52:18 UTC, Paul Backus wrote:
On Tuesday, 19 January 2021 at 16:22:35 UTC, Vinod K Chandran 
wrote:


b ? (tbinfo.fsState |= TBSTATE_ENABLED) : (tbinfo.fsState 
&= ~TBSTATE_ENABLED);


This means, "if b is true, set the TBSTATE_ENABLED flag to 
true; otherwise, set it to false."



Hi Paul Backus,
Thanks for the detailed reply. After reading your reply, I got 
the idea. But think there is one silly mistake in your reply. 
Forgive me if I am wrong.

Instead of
"if b is true, set the TBSTATE_ENABLED flag to true; otherwise, 
set it to false."


This is the meaning of that code.
if (b == true) {tbinfo.fsState = true ; } else {tbinfo.fsState = 
false;}


Because, TBSTATE_ENABLED is a  manifest constant and I cannot 
modify it.

What about simply writing this --
"tbinfo.fsState = b ; " This also worked.



Re: isCallable fails

2021-01-20 Thread frame via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 13:11:09 UTC, Paul Backus wrote:

On Wednesday, 20 January 2021 at 04:43:12 UTC, frame wrote:

struct foo(T) {
  T get() {
static if (is(T : bar)) {
if (value is null) {
value = fun!T;


Error: template instance `fun!T` template `fun` is not defined

Please post an example with enough code to actually produce the 
error you're seeing.


I don't know when to stop posting code then :(

fun!T just returns bar or null. Manually calling works.

but if it helps, here is fun!T:

TDobj fun(TDobj, T)(string propertyName, T needle) if (is(TDobj : 
bar)) {

   TDobj[] list = loadAllMatched!TDobj(propertyName, needle, 1);
   return list.length > 0 ? list[0] : null;
}

and actually called

value = fun!T("id", 123));

It also compiles if

value = new T



Re: isCallable fails

2021-01-20 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 04:43:12 UTC, frame wrote:

struct foo(T) {
  T get() {
static if (is(T : bar)) {
if (value is null) {
value = fun!T;


Error: template instance `fun!T` template `fun` is not defined

Please post an example with enough code to actually produce the 
error you're seeing.


Re: How can I create a Standalone Bundle Portable file application using Dlang?

2021-01-20 Thread Marcone via Digitalmars-d-learn

On Tuesday, 19 January 2021 at 14:20:06 UTC, Imperatorn wrote:

On Tuesday, 19 January 2021 at 11:10:25 UTC, Marcone wrote:

On Tuesday, 19 January 2021 at 06:25:31 UTC, Imperatorn wrote:

On Monday, 18 January 2021 at 19:42:22 UTC, Marcone wrote:
How can I create a Standalone Bundle Portable file 
application using Dlang?


Could you describe what you mean with "Bundle portable file 
application"?


All dependencies inside an exe file. Like Python Pyinstaller.


Do you with "dependencies" mean "resources"? In that case, yeah 
import is an option someone mentioned.


I do not mean resources .res, except if is possible use files 
inside resources without copy to hard disc and make accessible as 
it is in local path.


Re: Convert double to long if lossless

2021-01-20 Thread drug via Digitalmars-d-learn

On 1/19/21 9:28 PM, Per Nordlöw wrote:

On Tuesday, 19 January 2021 at 16:14:17 UTC, drug wrote:

   https://dlang.org/phobos/std_bitmanip.html#FloatRep


Doesn't this pattern already cover all possible cases of `value` needed?

void f(double value)
{
     auto lvalue = cast(long)value;
     if (lvalue == value) // `value` lacks fraction and in range 
[long.min .. long.max]

     {
     // use long lvalue
     return;
     }
     // use double value
}



Sure. You don't even need to use isFinite like I wrote above because 
special values like inf and nan become some long values and will never 
equal to itself.


Your question motivated me to do a little workout for my brains. Sorry 
if it confused you.