Re: Some user-made C functions and their D equivalents

2022-07-27 Thread pascal111 via Digitalmars-d-learn

On Thursday, 28 July 2022 at 00:36:54 UTC, ryuukk_ wrote:
I don't remember the exact syntax for GDC, but it should be 
pretty similar to DMD


You need to pass the module to the compiler

gdc main.d dcollect.d


I'm using CODE::BLOCKS IDE. How can I do it through it?


Re: Some user-made C functions and their D equivalents

2022-07-27 Thread ryuukk_ via Digitalmars-d-learn
I don't remember the exact syntax for GDC, but it should be 
pretty similar to DMD


You need to pass the module to the compiler

gdc main.d dcollect.d


Re: Some user-made C functions and their D equivalents

2022-07-27 Thread pascal111 via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 19:07:26 UTC, ryuukk_ wrote:

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:
I made a library of some useful functions while I was studying 
C, and I'm curious to know if D has equivalents or some ones 
for some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H


D ships with libc so everything in there is available

```C
#include
#include
#include
#include
#include
```


In d the equivalent imports:

```D
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import core.stdc.ctype;
import core.stdc.math;
```


Ok, but before that, I faced a problem when I tried to make my 
module. I tried to make a test. I made a testing module called 
"dcollect.d" and I put it in this path on my Ubuntu 
"/usr/lib/gcc/x86_64-linux-gnu/11/include/d".


module dcollect;

import std.stdio;

int foo22()
{

return 5;

}

Then I called "foo22" in a testing program and found an error 
like (this one in another try by telling code::blocks about local 
searching folder for the compiler I put the module in) "||=== 
Build: Debug in temp (compiler: GDC D Compiler) ===|
/home/pascal111/My Projects/D/temp/hello.d|23|undefined reference 
to `_D8dcollect5foo22FZi'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 
second(s)) ===|"


The testing program:

module main;

import std.stdio;
import std.string;
import std.conv;
import dcollect;

int main(string[] args)
{

string ch;

try{
ch=readln();
ch=strip(ch);

if(to!int(ch)<0)
throw new Exception ("You entered negative number!");}

catch (Exception e){
writeln(e.msg);}

writeln(foo22());

return 0;
}




Re: Particular exceptions names

2022-07-27 Thread kdevel via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 09:35:12 UTC, Ali Çehreli wrote:

The following program show an example as well as 'enforce', 
which I prefer over explicit if+throw+else:


Me too.


  enforce!MissingArguments(args.length == 42,
   format!"Too few arguments: 
%s"(args.length));


However, this particular form lowers the readability a lot. It is 
not the absence of arguments which is enforced but the length of 
the args array, i.e. the presence of the arguments. Therefore I 
prefer


   enforce (args.length == 42, new MissingArguments (...));



Re: Particular exceptions names

2022-07-27 Thread kdevel via Digitalmars-d-learn

On Tuesday, 26 July 2022 at 23:43:59 UTC, pascal111 wrote:

In next example code, it used user-made exception,



[...]



   try {
  if( b == 0 ) {
 throw new Exception("Cannot divide by zero!");
  } else {
 result = format("%s",a/b);
  }



[...]



void main () {
   int x = 50;
   int y = 0;


What about the case x = -2147483648, y = -1?




Re: Some user-made C functions and their D equivalents

2022-07-27 Thread ryuukk_ via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:
I made a library of some useful functions while I was studying 
C, and I'm curious to know if D has equivalents or some ones 
for some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H


D ships with libc so everything in there is available

```C
#include
#include
#include
#include
#include
```


In d the equivalent imports:

```D
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import core.stdc.ctype;
import core.stdc.math;
```


Re: Exclamation symbol "!" within functions syntax

2022-07-27 Thread pascal111 via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 11:09:19 UTC, Ali Çehreli wrote:

On 7/27/22 04:00, pascal111 wrote:
I noticed more than once that the exclamation "!" is used 
within functions typing, and it seems like an operator with 
new use, for example "to!int()", ".tee!(l => sum += 
l.length)", "enforce!MissingArguments...", so what dose it 
means?




The binary ! operator is used for specifying template 
arguments. I have some explanation here:



http://ddili.org/ders/d.en/templates.html#ix_templates.!,%20template%20instance

Ali


I think I got it now, it's easy, I thought it so advanced D 
feature.


Some user-made C functions and their D equivalents

2022-07-27 Thread pascal111 via Digitalmars-d-learn
I made a library of some useful functions while I was studying C, 
and I'm curious to know if D has equivalents or some ones for 
some of my functions, or I have to retype 'em again in D.


The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H


Re: BetterC Name Mangling Linker Errors

2022-07-27 Thread MyNameHere via Digitalmars-d-learn

Thank you, that seems to have been the source of the error.




Re: BetterC Name Mangling Linker Errors

2022-07-27 Thread Dennis via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 12:26:59 UTC, MyNameHere wrote:

```d
void Main(void* Instance)
{
WNDCLASSEXA WindowClass;
```


This is equivalent to `WNDCLASSEXA WindowClass = 
WNDCLASSEXA.init;`


If the struct's fields all initialize to 0, the compiler would 
simply set the variable's bytes to 0, but the definition in 
druntime gives fields with non-zero default value:


```D
struct WNDCLASSEXA {
UINT  cbSize = WNDCLASSEXA.sizeof; // <-- non zero init
UINT  style;
WNDPROC   lpfnWndProc;
int   cbClsExtra;
int   cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR   hCursor;
HBRUSHhbrBackground;
LPCSTRlpszMenuName;
LPCSTRlpszClassName;
HICON hIconSm;
}
```

Because of this, the compiler defines an 'init symbol' in 
druntime that gets copied into your variable to initialize it. 
Because druntime isn't linked when using BetterC, the linker 
fails to find the init symbol.


I think removing the default initialization will fix it:
```D
WNDCLASSEXA WindowClass = void;
```



BetterC Name Mangling Linker Errors

2022-07-27 Thread MyNameHere via Digitalmars-d-learn

I have included the source to a simple 64-bit Windows program.
It compiles and runs fine with ```dmd -m64 
-L="/Subsystem:Windows" -L="/Entry:Main" Main.d```. But compiling 
with ```-betterC``` using the following throws up a linker error, 
```dmd -m64 -betterC -L="/Subsystem:Windows" -L="/Entry:Main" 
Main.d```:


```Main.obj : error LNK2019: unresolved external symbol 
_D4core3sys7windows7winuser11WNDCLASSEXA6__initZ referenced in 
function Main```


And I'm not sure why.

```d
import core.sys.windows.winuser;
import core.sys.windows.winbase;

pragma(lib, "User32.lib");
pragma(lib, "Kernel32.lib");

extern(Windows)
void Main(void* Instance)
{
WNDCLASSEXA WindowClass;
with (WindowClass)
{
cbSize= WindowClass.sizeof;
lpfnWndProc   = 
hInstance = Instance;
hCursor   = LoadCursor(null, IDC_ARROW);
lpszClassName = "Window";
}
RegisterClassExA();
void *WindowHandle = CreateWindowExA(0,
 "Window",
 "Window",
 WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT,
 CW_USEDEFAULT,
 CW_USEDEFAULT,
 CW_USEDEFAULT,
 null,
 null,
 Instance,
 null);
ShowWindow(WindowHandle, SW_MAXIMIZE);

MSG Message;
while (GetMessage(, null, 0, 0))
{
TranslateMessage();
DispatchMessage();
}
}

extern(Windows)
long WindowProc(void* WindowHandle, uint Message, ulong WParam, 
long LParam) nothrow @system

{
if (Message == WM_DESTROY) ExitProcess(0);

return DefWindowProcA(WindowHandle, Message, WParam, LParam);
}
```


Re: Exclamation symbol "!" within functions syntax

2022-07-27 Thread Ali Çehreli via Digitalmars-d-learn

On 7/27/22 04:00, pascal111 wrote:
I noticed more than once that the exclamation "!" is used within 
functions typing, and it seems like an operator with new use, for 
example "to!int()", ".tee!(l => sum += l.length)", 
"enforce!MissingArguments...", so what dose it means?




The binary ! operator is used for specifying template arguments. I have 
some explanation here:



http://ddili.org/ders/d.en/templates.html#ix_templates.!,%20template%20instance

Ali


Exclamation symbol "!" within functions syntax

2022-07-27 Thread pascal111 via Digitalmars-d-learn
I noticed more than once that the exclamation "!" is used within 
functions typing, and it seems like an operator with new use, for 
example "to!int()", ".tee!(l => sum += l.length)", 
"enforce!MissingArguments...", so what dose it means?




Re: Particular exceptions names

2022-07-27 Thread pascal111 via Digitalmars-d-learn

On Wednesday, 27 July 2022 at 09:35:12 UTC, Ali Çehreli wrote:

On 7/26/22 16:43, pascal111 wrote:
> [...]

I am not sure I understand you correctly because the program 
you show throws Exception, which is not user-made at all.


[...]


It seems an advanced topic. It'll take some more studying to 
understand this code.


Re: Particular exceptions names

2022-07-27 Thread Ali Çehreli via Digitalmars-d-learn

On 7/26/22 16:43, pascal111 wrote:
> In next example code, it used user-made exception,

I am not sure I understand you correctly because the program you show 
throws Exception, which is not user-made at all.


If you want to throw a particual exception that you define, you need to 
inherit that type from Exception.


The following program show an example as well as 'enforce', which I 
prefer over explicit if+throw+else:


import std.stdio;
import std.format;

class MissingArguments : Exception {
  this(string msg, string file = __FILE__, size_t line = __LINE__) {
super(msg, file, line);
  }
}

void main(string[] args) {
  // if (args.length != 42) {
  //   throw new MissingArguments(args.length);
  // }

  import std.exception : enforce;
  enforce!MissingArguments(args.length == 42,
   format!"Too few arguments: %s"(args.length));

  // Program continues here... (No 'else' needed.)
}

Ali



Re: Particular exceptions names

2022-07-27 Thread frame via Digitalmars-d-learn

On Tuesday, 26 July 2022 at 23:43:59 UTC, pascal111 wrote:
In next example code, it used user-made exception, but what if 
I'm looking for a particular exception? from where can I get 
particular exception to arise it?


There is no mechanism to find a particular exceptions in D. You 
have simple to know which exception can be thrown by studying the 
documentation or source code.


If you just want to give the user an information which exception 
was thrown in run time, use typeid:


```d
try { ... }
catch(Exception e) {
writefln("%s was thrown", typeid(e));
}
```