Re: What is difference between struct and class?

2019-06-03 Thread Ron Tarrant via Digitalmars-d-learn

On Monday, 3 June 2019 at 09:43:25 UTC, Rnd wrote:

These similarities and differences should be highlighted in 
documentation etc since many new users have at least some 
knowledge of C/C++ and understanding will be easier.


Perhaps this will help: https://dlang.org/articles/ctod.html


Re: Reading Dicom files in Dlang

2019-06-03 Thread Rnd via Digitalmars-d-learn

On Monday, 3 June 2019 at 15:56:00 UTC, Rémy Mouëza wrote:

On Monday, 3 June 2019 at 14:19:40 UTC, Rnd wrote:

[...]


We can call C functions directly from D. First, the functions 
must be declared in D, as D's syntax is different from C's.


[...]



Thanks for a very comprehensive answer.


Re: Error: char 0x200b not allowed in identifier

2019-06-03 Thread zoujiaqing via Digitalmars-d-learn

On Monday, 3 June 2019 at 14:41:18 UTC, ag0aep6g wrote:

On 03.06.19 15:37, zoujiaqing wrote:

Error for code:
source/hunt/xml/Element.d(12,13): Error: char 0x200b not 
allowed in identifier


U+200B is: ZERO WIDTH SPACE. Somehow you got that invisible 
character into your code. You have to get rid of it.


To do it manually, navigate to the locations the compiler gives 
you and find the spot where the cursor seems to get stuck for 
one key press. That's where a zero width space is.


You can also open the file in a different non-Unicode encoding. 
The zero width spaces should stick out as non-ASCII symbols.


Or you could write a program that goes over the file and 
filters the zero width spaces out.


Or just re-write whole lines or the whole file by hand.


Thanks :)


Re: Reading Dicom files in Dlang

2019-06-03 Thread Rémy Mouëza via Digitalmars-d-learn

On Monday, 3 June 2019 at 14:19:40 UTC, Rnd wrote:

On Friday, 31 May 2019 at 16:43:28 UTC, rnd wrote:

On Friday, 31 May 2019 at 13:49:02 UTC, KnightMare wrote:

  struct Range {
private __vector(ushort) _outer;
private size_t _a, _b;

this(vector(ushort) data, size_t a, size_t b) {   // 
 line 457

  _outer = data;
  _a = a;
  _b = b;
}


imo problem is in string
private __vector(ushort)_outer;
it looks like template(vector!ushort) or function or this is 
vector from core.simd

and replacing it to private long _outer; fix the problem
paste code imebra.d and imebra_im.d to someplace


Best to download it from https://imebra.com/get-it/ so that 
all files are available to you.


I am still waiting for someone to help me with this.

Can we directly call C functions from D without going through 
swig?


We can call C functions directly from D. First, the functions 
must be declared in D, as D's syntax is different from C's.


There exists tools to help us with that :
- dstep, https://code.dlang.org/packages/dstep
- dpp, https://code.dlang.org/packages/dpp
The latter, dpp, acts like a preprocessor to allow inclusion with 
`#include` as would be done with C header files.


The swig D module is quite old and I would presume it is outdated 
- but I may be wrong.


Note that the standard C library is available in the `core.stdc` 
modules.


Sometimes, these tools manage to translate 95% of header files 
properly, but some manual tweaking is necessary to handle the 
remaining 5%. The following page in the D documentation explains 
how to interface with C: https://dlang.org/spec/interfaceToC.html 
.
Looking at the output generally gives a good inspiration of what 
code pattern to use to fix the issues.


C++ is a different matter. The binding tool ecosystem gives more 
mixed results than with C:

- dstep does not generate declarations for C++;
- dpp is still a work in progress; the last time I looked at its 
commits, I saw some unit tests checking the binding of simple 
classes - but I wouldn't expect it to work with much of the STL;
- There exist Binderoo: https://github.com/GooberMan/binderoo 
which is a mean to script C++ game using D, less active than the 
first two, but could be helpful, I haven't researched it much. 
The author gave a presentation during DConf 2017: 
https://www.youtube.com/watch?v=2B0-jukh4TU .


The following page of the D documentation explains how to 
interface with C++: https://dlang.org/spec/cpp_interface.html .


Interfacing with C is much easier than with C++. I was once in 
need of a C++ library without any C API; I wrote a wrapper for it:

- some C++ function within an `extern "C" {}` declaration,
- with helper function to create and delete my C++ class 
instances,

- the main C++ methods I wanted to expose,
- a D binding declaration, exposing the previous function to D,
- a higher level D mirroring the C++ classes,
- exception were caught in the C++ `extern "C" {}` function: I 
returned a special Result struct containing a boolean status, the 
result or null, and a possible error message. I would then throw 
a D exception to let the user code handle the error.


Nowadays, D's C++ support is better. If you can restrict to just 
a subset of the library, writing a wrapper might be feasible, but 
would include wrapping part of the STL not yet in the 
`core.stdcpp` modules (for libraries using the STL).




Re: Error: char 0x200b not allowed in identifier

2019-06-03 Thread ag0aep6g via Digitalmars-d-learn

On 03.06.19 15:37, zoujiaqing wrote:

Error for code:
source/hunt/xml/Element.d(12,13): Error: char 0x200b not allowed in 
identifier


U+200B is: ZERO WIDTH SPACE. Somehow you got that invisible character 
into your code. You have to get rid of it.


To do it manually, navigate to the locations the compiler gives you and 
find the spot where the cursor seems to get stuck for one key press. 
That's where a zero width space is.


You can also open the file in a different non-Unicode encoding. The zero 
width spaces should stick out as non-ASCII symbols.


Or you could write a program that goes over the file and filters the 
zero width spaces out.


Or just re-write whole lines or the whole file by hand.


Re: Reading Dicom files in Dlang

2019-06-03 Thread Rnd via Digitalmars-d-learn

On Friday, 31 May 2019 at 16:43:28 UTC, rnd wrote:

On Friday, 31 May 2019 at 13:49:02 UTC, KnightMare wrote:

  struct Range {
private __vector(ushort) _outer;
private size_t _a, _b;

this(vector(ushort) data, size_t a, size_t b) {   // 
 line 457

  _outer = data;
  _a = a;
  _b = b;
}


imo problem is in string
private __vector(ushort)_outer;
it looks like template(vector!ushort) or function or this is 
vector from core.simd

and replacing it to private long _outer; fix the problem
paste code imebra.d and imebra_im.d to someplace


Best to download it from https://imebra.com/get-it/ so that all 
files are available to you.


I am still waiting for someone to help me with this.

Can we directly call C functions from D without going through 
swig?




Error: char 0x200b not allowed in identifier

2019-06-03 Thread zoujiaqing via Digitalmars-d-learn

Error for code:
source/hunt/xml/Element.d(12,13): Error: char 0x200b not allowed 
in identifier
source/hunt/xml/Element.d(12,23): Error: character 0x200b is not 
a valid token
source/hunt/xml/Element.d(17,15): Error: char 0x200b not allowed 
in identifier
source/hunt/xml/Element.d(17,26): Error: character 0x200b is not 
a valid token
source/hunt/xml/Element.d(22,13): Error: char 0x200b not allowed 
in identifier
source/hunt/xml/Element.d(22,29): Error: character 0x200b is not 
a valid token
source/hunt/xml/Element.d(48,13): Error: char 0x200b not allowed 
in identifier
source/hunt/xml/Element.d(48,21): Error: character 0x200b is not 
a valid token
source/hunt/xml/Element.d(55,13): Error: char 0x200b not allowed 
in identifier
source/hunt/xml/Element.d(55,23): Error: character 0x200b is not 
a valid token
source/hunt/xml/Element.d(62,13): Error: char 0x200b not allowed 
in identifier
source/hunt/xml/Element.d(62,20): Error: character 0x200b is not 
a valid token



The Code file Element.d

```D
module hunt.xml.Element;

import hunt.xml.ElementType;

class Element
{
this(string name)
{
this._name = name;
}

Element addElement​(string name)
{
return new Element(name);
}

Element[] getElements​()
{
return this._elements;
}

Element getParentELement​()
{
return this._parentElement;
}

Element removeAttribute(string key)
{
this._attributes.remove(key);

return this;
}

Element addAttribute(string key, string value)
{
this._attributes[key] = value;

return this;
}

Element setAttribute(string key, string value)
{
this._attributes[key] = value;

return this;
}

Element addCDATA​(string cdata)
{
this._cdata = cdata;

return this;
}

Element addComment​(string comment)
{
this._comments ~= comment;

return this;
}

Element addText​(string text)
{
this._text = text;

return this;
}

private
{
string _name;
Element[] _elements;
Element _parentElement;
string[string] _attributes;
string[] _comments;
string _text;
string _cdata;
}
}
```


Re: How can I get a backtrace when my WndProc throws?

2019-06-03 Thread Markus via Digitalmars-d-learn

On Monday, 3 June 2019 at 12:09:18 UTC, Adam D. Ruppe wrote:

Don't use just e.msg, that's just the string the constructor 
sent, use e.toString and see if it works for you.


toString works, thanks! :)


Re: How can I get a backtrace when my WndProc throws?

2019-06-03 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 3 June 2019 at 11:19:22 UTC, Markus wrote:

a throw, has anyone an idea how to do this?


First thing I'd try is to just print the exception when you catch 
it. The toString method can do a backtrace.


Don't use just e.msg, that's just the string the constructor 
sent, use e.toString and see if it works for you.


How can I get a backtrace when my WndProc throws?

2019-06-03 Thread Markus via Digitalmars-d-learn
Hi, I'm using core.sys.windows.windows to make a Windows GUI, and 
the WindowProc must be nothrow. My code does not provide me a 
stack backtrace when some exception is thrown in there. I would 
like to have at least the number of the line that caused a throw, 
has anyone an idea how to do this? Thanks for reading.


Below is the code for the Windows application. I guess I need to 
improve window_procedure_nothrow() somehow.


import std;
import core.sys.windows.windows;

HWND main_window;

LRESULT window_procedure(
HWND hwnd,
uint msg,
WPARAM wparam,
LPARAM lparam)
{
if(msg == WM_CLOSE)
{
PostQuitMessage(0);
return 0;
}

if(msg == WM_KEYDOWN)
{
format("%d", 0.5); // trigger an exception

format("%d", 0.7);
}

return DefWindowProc(hwnd, msg, wparam, lparam);
}

extern(Windows) LRESULT window_procedure_nothrow(
HWND hwnd,
uint msg,
WPARAM wparam,
LPARAM lparam) nothrow
{
// this prints only a line number from within this function
	// and some stack backtrace of functions outside the window 
procedure

if(0)
{
		return assumeWontThrow(window_procedure(hwnd, msg, wparam, 
lparam));

}

// this prints no line number at all
	// can get only the exception message from 
collectException().msg	

if(1)
{
LRESULT result;
auto ce = collectException(
window_procedure(hwnd, msg, wparam, lparam),
result);
if(ce !is null)
{
assumeWontThrow(writeln(ce.msg));
TerminateProcess(GetCurrentProcess(), 0);
}
return result;
}
}

void main()
{
WNDCLASSEX wndclassex = {
style: 0,
lpfnWndProc: _procedure_nothrow,
lpszClassName: "a",
hInstance: GetModuleHandle(null),
hIcon: LoadIcon(null, MAKEINTRESOURCE(32517)),
hCursor: LoadCursor(null, MAKEINTRESOURCE(32512)) };

ATOM windowclass = RegisterClassEx();
assert(windowclass != 0);

main_window = CreateWindowEx(
0,
"a",
"test",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100,
100,
200,
200,
null,
null,
GetModuleHandle(null),
null);
assert(main_window != null);

for(;;)
{
MSG msg;
int ret = GetMessage(, null, 0, 0);

if(ret == 0) break;

assert(ret != -1);

TranslateMessage();
DispatchMessage();
}
}


Re: How to create GTK+ apps with Glade and D on windows

2019-06-03 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Friday, 31 May 2019 at 18:47:06 UTC, Obsidian Jackal wrote:
I'm new to D and want to create GTK+ apps. I have Visual 
Studio, Glade, the Gtk+ runtime, DMD, and DUB installed. What 
steps, guides, or advice should I follow to be able to be able 
to use these tools together to make a sane app?.


I am writing on my cell phone, so cannot address the whole thing. 
When I started to learn d, I was playing around a boiler plate to 
imitate something like javafx and it's builder. Here is a meson 
based project, which creates a gtkd project including a hello 
world window, a Glade file, its controller class, and an auto 
calling python script to update controller class after changing 
the UI. The paths in src/meson.build must be set before building 
the project creator. I am sorry, I could not find some free time 
to make it a more generic project builder, but I think it may 
give you some idea. Again I was a very beginner when I was 
working on it.


Re: How to create GTK+ apps with Glade and D on windows

2019-06-03 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Monday, 3 June 2019 at 10:32:25 UTC, Ferhat Kurtulmuş wrote:

On Friday, 31 May 2019 at 18:47:06 UTC, Obsidian Jackal wrote:

[...]


I am writing on my cell phone, so cannot address the whole 
thing. When I started to learn d, I was playing around a boiler 
plate to imitate something like javafx and it's builder. Here 
is a meson based project, which creates a gtkd project 
including a hello world window, a Glade file, its controller 
class, and an auto calling python script to update controller 
class after changing the UI. The paths in src/meson.build must 
be set before building the project creator. I am sorry, I could 
not find some free time to make it a more generic project 
builder, but I think it may give you some idea. Again I was a 
very beginner when I was working on it.


https://gitlab.com/aferust/gtkdappcreator


Re: What is difference between struct and class?

2019-06-03 Thread Rnd via Digitalmars-d-learn

On Monday, 3 June 2019 at 08:54:12 UTC, Jonathan M Davis wrote:
structs in D are basically the same as C++ classes that don't 
have inheritance and can be put on the stack or the heap, and 
classes in D are akin to C++ classes that use inheritance and 
are always put on the heap and used via pointers. D classes are 
similar to Java classes in that respect.


- Jonathan M Davis


Also struct in D seem to be very similar to classes in C except 
lack of inheritance.


These similarities and differences should be highlighted in 
documentation etc since many new users have at least some 
knowledge of C/C++ and understanding will be easier.


Re: What is difference between struct and class?

2019-06-03 Thread Mike Parker via Digitalmars-d-learn

On Monday, 3 June 2019 at 08:50:46 UTC, Mike Parker wrote:

On Monday, 3 June 2019 at 08:47:41 UTC, Mike Parker wrote:



If yes, when should one use 'new'?


Whenever you need to allocate something from the GC heap. In 
my experience, it's rare to need it with value types in D. I 
tend to use it primarily with classes and arrays.


Ali's book has an example using a struct-based linked list in 
the chapter on pointers:


https://forum.dlang.org/thread/rkmcvxftykhsvxofp...@forum.dlang.org


Wrong link. It's at:

http://ddili.org/ders/d.en/pointers.html


Re: What is difference between struct and class?

2019-06-03 Thread Mike Parker via Digitalmars-d-learn

On Monday, 3 June 2019 at 08:47:41 UTC, Mike Parker wrote:



If yes, when should one use 'new'?


Whenever you need to allocate something from the GC heap. In my 
experience, it's rare to need it with value types in D. I tend 
to use it primarily with classes and arrays.


Ali's book has an example using a struct-based linked list in the 
chapter on pointers:


https://forum.dlang.org/thread/rkmcvxftykhsvxofp...@forum.dlang.org


Re: What is difference between struct and class?

2019-06-03 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, June 3, 2019 1:13:44 AM MDT Rnd via Digitalmars-d-learn wrote:
> On Monday, 3 June 2019 at 06:01:15 UTC, Jonathan M Davis wrote:
> > On Sunday, June 2, 2019 9:40:43 PM MDT Rnd via
> >
> > Digitalmars-d-learn wrote:
> >> On Monday,
> >
> > http://ddili.org/ders/d.en/index.html
> >
> > If you want to know more about structs and classes
> > specifically, then you can go straight to the sections on them,
> > but you're going to understand a lot of things better if you
> > just read through the book.
> >
> > - Jonathan M Davis
>
> I know 'new' is not needed to create instances of structs but can
> one use 'new'?
>
> If yes, when should one use 'new'?

Yes, you can use new with structs, just like you can use it with ints or
floats or almost any type. It puts the struct on the heap instead of the
stack. When that makes sense depends on when you need to have a struct on
the heap instead of the stack. It's basically the same as why you'd want to
put a class without inheritance on the heap in C++. structs in D are
basically the same as C++ classes that don't have inheritance and can be put
on the stack or the heap, and classes in D are akin to C++ classes that use
inheritance and are always put on the heap and used via pointers. D classes
are similar to Java classes in that respect.

- Jonathan M Davis





Re: What is difference between struct and class?

2019-06-03 Thread Mike Parker via Digitalmars-d-learn

On Monday, 3 June 2019 at 07:13:44 UTC, Rnd wrote:



I know 'new' is not needed to create instances of structs but 
can one use 'new'?


Yes. It can be used with any value type to allocate a block of 
memory on the GC heap and return a pointer to that memory:


struct Foo { ... }
Foo* f = new Foo;

int* i = new int;




If yes, when should one use 'new'?


Whenever you need to allocate something from the GC heap. In my 
experience, it's rare to need it with value types in D. I tend to 
use it primarily with classes and arrays.





Re: Reading .pem files for secured

2019-06-03 Thread Dukc via Digitalmars-d-learn

On Friday, 31 May 2019 at 10:35:46 UTC, Dukc wrote:
if I understand the logic of Base64, it's that each character 
stores 6 bits. My private key .pem has 49 lines of 64 
characters worth of Base64, though the sat line isn't full. 
Anyway, this is data worth of over 18000 bits. The RSA key is 
supposed to be 4096 bits, so this can't be correct.


What am I missing?


I think that what I missed was that .pem key files do not only 
contain the key, they also contain the public exponent (even 
though it's always the same 0x10001), and in case of private key, 
the root parameters used to generate the key pair (not sure if it 
contains the public key also). That explains why the private key 
file is so much larger than the key bit count would dictate.


And by looking at members of RSA class at SecureD, I think it 
also keeps those extra components, implying that everything in 
.pem should go in (after decoding), not just the public or 
private key. Like Koppe said.




Re: What is difference between struct and class?

2019-06-03 Thread Rnd via Digitalmars-d-learn

On Monday, 3 June 2019 at 06:01:15 UTC, Jonathan M Davis wrote:
On Sunday, June 2, 2019 9:40:43 PM MDT Rnd via 
Digitalmars-d-learn wrote:

On Monday,

http://ddili.org/ders/d.en/index.html

If you want to know more about structs and classes 
specifically, then you can go straight to the sections on them, 
but you're going to understand a lot of things better if you 
just read through the book.


- Jonathan M Davis


I know 'new' is not needed to create instances of structs but can 
one use 'new'?


If yes, when should one use 'new'?



Re: What is difference between struct and class?

2019-06-03 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, June 2, 2019 9:40:43 PM MDT Rnd via Digitalmars-d-learn wrote:
> On Monday, 3 June 2019 at 00:47:27 UTC, Adam D. Ruppe wrote:
> > On Monday, 3 June 2019 at 00:17:08 UTC, Rnd wrote:
> >> What additional features do classes offer in D?
> >
> > Classes support built-in runtime polymorphism through
> > inheritance. structs don't.
> >
> > As a result of this, classes are a little bit heavier
> > resource-wise and are semantically always object references.
>
> I am not clear if structs can have constructors (this) and
> whether they can be multiple? Also can data be made private and
> getters and setters used to access them?

Yes structs can have constructors (but no default constructor - the default
value of a struct is its init value, which is defined by the values that the
struct's members are directly initialized with), and structs can have all of
the various functions that a class can have. The can also use private,
public, and package just like classes can (but not protected, since structs
have no inheritance).

Basically, structs go wherever they're declared and don't have inheritance,
whereas classes are always reference types and have inheritance. In general,
besides that, their abilities are pretty much the same, though there are
some differences that stem from the fact that classes are always reference
types, whereas structs aren't. I'd advise reading

http://ddili.org/ders/d.en/index.html

If you want to know more about structs and classes specifically, then you
can go straight to the sections on them, but you're going to understand a
lot of things better if you just read through the book.

- Jonathan M Davis