Re: Call method of object variable

2020-10-16 Thread Simen Kjærås via Digitalmars-d-learn
On Friday, 16 October 2020 at 08:12:59 UTC, Andrey wrote: Hi, I have got: struct Qaz { wstring read() {return null;} wstring hear() {return "";} } void main() { // ... static if(some_condition) alias method = Qaz.hear; else alias method = Qaz.read; // ... Qaz

Re: Call method of object variable

2020-10-16 Thread Andrey via Digitalmars-d-learn
Thank you!

Call method of object variable

2020-10-16 Thread Andrey via Digitalmars-d-learn
Hi, I have got: struct Qaz { wstring read() {return null;} wstring hear() {return "";} } void main() { // ... static if(some_condition) alias method = Qaz.hear; else alias method = Qaz.read; // ... Qaz qaz; qaz.method(); // ??? } How to call alias

universal alpha?

2020-10-16 Thread Imperatorn via Digitalmars-d-learn
https://forum.dlang.org/post/e5ghnv$2bns$1...@digitaldaemon.com On Tuesday, 30 May 2006 at 04:29:51 UTC, BCS wrote: Does anyone have a link to a definition of universal alpha as used in the DMD docs? (see: http://www.digitalmars.com/d/lex.html#identifier) For any other lonely souls looking

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Paul Backus via Digitalmars-d-learn
On Friday, 16 October 2020 at 13:12:04 UTC, tchaloupka wrote: So when the exception is thrown within Foo destructor (and it's bad on it's own but can easily happen as destructors aren't nothrow @nogc by default). Is this behavior expected? This is a compiler/language bug. It was fixed in

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 9:12 AM, tchaloupka wrote: So when the exception is thrown within Foo destructor (and it's bad on it's own but can easily happen as destructors aren't nothrow @nogc by default). Is this behavior expected? I would say it's a bug. The compiler is going to call the member

Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread tchaloupka via Digitalmars-d-learn
Found a pretty nasty bug in vibe-d: https://github.com/vibe-d/vibe.d/issues/2484 And it's caused by this behavior. ```D import std; struct Foo { Bar bar; bool err; ~this() { // scope(failure) destroy(bar); // < this fixes the Bar destructor call enforce(!err,

Re: universal alpha?

2020-10-16 Thread Paul Backus via Digitalmars-d-learn
On Friday, 16 October 2020 at 13:27:57 UTC, Imperatorn wrote: https://forum.dlang.org/post/e5ghnv$2bns$1...@digitaldaemon.com On Tuesday, 30 May 2006 at 04:29:51 UTC, BCS wrote: Does anyone have a link to a definition of universal alpha as used in the DMD docs? (see:

Re: why do i need an extern(C): here?

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/15/20 2:42 PM, Ali Çehreli wrote: > I've recently done the same by calling dlopen() and dlsym() > directly. Runtime.loadLibrary documentation says "If the library > contains a D runtime it will be integrated with the current runtime." > That would explain why my program seg-faults for my

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 6:12 AM, tchaloupka wrote: > struct Foo { > Bar bar; > bool err; > > ~this() { > // scope(failure) destroy(bar); // < this fixes the Bar > destructor call > enforce(!err, "Test err"); Well, that check means "cannot continue", which means the compiler

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 11:11 AM, Ali Çehreli wrote: On 10/16/20 6:12 AM, tchaloupka wrote: > struct Foo { >  Bar bar; >  bool err; > >  ~this() { >  // scope(failure) destroy(bar); // < this fixes the Bar > destructor call >  enforce(!err, "Test err"); Well, that check

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Paul Backus via Digitalmars-d-learn
On Friday, 16 October 2020 at 15:19:51 UTC, Paul Backus wrote: On Friday, 16 October 2020 at 13:12:04 UTC, tchaloupka wrote: So when the exception is thrown within Foo destructor (and it's bad on it's own but can easily happen as destructors aren't nothrow @nogc by default). Is this behavior

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 9:05 AM, Steven Schveighoffer wrote: > The destruction of members is outside the destructor's purview. It can't > turn the destruction off, so it should logically be considered part of > an enclosing function. Thank you. Makes sense. Ali

Re: Forward referencing functions in D

2020-10-16 Thread Imperatorn via Digitalmars-d-learn
On Friday, 16 October 2020 at 19:55:53 UTC, wilcro wrote: The web page "Programming in D for C Programmers" (https://dlang.org/articles/ctod.html#forwardfunc) states that forward declarations are neither required nor permitted, and that the following construct is allowable: void myfunc() {

Re: Forward referencing functions in D

2020-10-16 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 16 October 2020 at 19:55:53 UTC, wilcro wrote: Evidently, I am misunderstanding something very elemental here; thanks for any enlightenment regarding this. Inside a function things happen in order, top to bottom, including declarations (you can only access local variables after

Re: Forward referencing functions in D

2020-10-16 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Oct 16, 2020 at 07:55:53PM +, wilcro via Digitalmars-d-learn wrote: > The web page "Programming in D for C Programmers" > (https://dlang.org/articles/ctod.html#forwardfunc) states that forward > declarations are neither required nor permitted, [...] > However, the following code will

Packing of Struct Fields

2020-10-16 Thread Per Nordlöw via Digitalmars-d-learn
Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S { int i; bool b; } struct T { S s; char c; } struct U { int i; bool b; char c; } ?

Re: Packing of Struct Fields

2020-10-16 Thread ag0aep6g via Digitalmars-d-learn
On 16.10.20 22:32, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S {     int i;     bool b; } struct T {     S s;     char c; } struct U {     int i;     bool b;     char c; } ? S.sizeof: 4 bytes for the int + 1 byte for

Re: Forward referencing functions in D

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 1:47 PM, wilcro wrote: > would > there be any reason to avoid placing the majority of code for a program > outside of the main function? Keeping scopes of symbols as small as possible is a general guideline in D and elsewhere but I wouldn't crowd my main() function with details of

Forward referencing functions in D

2020-10-16 Thread wilcro via Digitalmars-d-learn
The web page "Programming in D for C Programmers" (https://dlang.org/articles/ctod.html#forwardfunc) states that forward declarations are neither required nor permitted, and that the following construct is allowable: void myfunc() { forwardfunc(); } void forwardfunc() { ... //do

Re: Forward referencing functions in D

2020-10-16 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Oct 16, 2020 at 08:04:07PM +, Imperatorn via Digitalmars-d-learn wrote: [...] > I think it might be just because you havent defined the function yet > at that point. That's not correct; the following works: module mymodule; void func() {

Re: Forward referencing functions in D

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 4:47 PM, wilcro wrote: Thanks to all for your responses; as a related followup question, would there be any reason to avoid placing the majority of code for a program outside of the main function? Inner functions have benefits: 1. They are only accessible inside the function.

Re: Packing of Struct Fields

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 4:44 PM, ag0aep6g wrote: On 16.10.20 22:32, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S { int i; bool b; } struct T { S s; char c; } struct U { int i; bool b; char c; } ?

How can I convert Hexadecimal to RGB Color and vice-versa?

2020-10-16 Thread Marcone via Digitalmars-d-learn
How can I convert Hexadecimal to RGB Color and vice-versa?

Re: Packing of Struct Fields

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 1:32 PM, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S {     int i;     bool b; } struct T {     S s;     char c; } struct U {     int i;     bool b;     char c; } ? I have a function that dumps member

Re: Forward referencing functions in D

2020-10-16 Thread wilcro via Digitalmars-d-learn
On Friday, 16 October 2020 at 19:55:53 UTC, wilcro wrote: The web page "Programming in D for C Programmers" (https://dlang.org/articles/ctod.html#forwardfunc) states that forward declarations are neither required nor permitted, and that the following construct is allowable: void myfunc() {

Re: Forward referencing functions in D

2020-10-16 Thread wilcro via Digitalmars-d-learn
On Friday, 16 October 2020 at 19:55:53 UTC, wilcro wrote: The web page "Programming in D for C Programmers" (https://dlang.org/articles/ctod.html#forwardfunc) states that forward declarations are neither required nor permitted, and that the following construct is allowable: void myfunc() {

Why is `Appender._data` a pointer to its `Data`-store?

2020-10-16 Thread Per Nordlöw via Digitalmars-d-learn
Why is `Appender`'s store `Data` put directly as `private Data* _data;` instead of `private Data _data;` ? Removing the pointer indirection would give better locality. If it's about optimizing for empty `Appender`s then a `Appender*` should be used in those cases instead.

Re: How can I convert Hexadecimal to RGB Color and vice-versa?

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 1:10 PM, Marcone wrote: How can I convert Hexadecimal to RGB Color and vice-versa? On 10/16/20 1:10 PM, Marcone wrote: > How can I convert Hexadecimal to RGB Color and vice-versa? Do you mean from a string like "#123456" to the values of red, green, and blue? I am having more

Re: Why is `Appender._data` a pointer to its `Data`-store?

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 5:40 PM, Per Nordlöw wrote: Why is `Appender`'s store `Data` put directly as     `private Data* _data;` instead of     `private Data _data;` ? Removing the pointer indirection would give better locality. If it's about optimizing for empty `Appender`s then a `Appender*` should

Re: why do i need an extern(C): here?

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 4:12 PM, WhatMeWorry wrote: > Isn't dlopen() for Linux More like dlopen() is for Posix, which means it should be available on Windows as well. However, as I've discovered, a D shared library cannot be loaded with dlopen() because the main program and the shared library would do

Re: How convert String to Fixed wchar Array?

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 8:23 PM, Marcone wrote: How convert String to Fixed wchar Array? import std; void main(){     string name = "Marvin";     wchar[255] wtext = name.to!(wchar[]); // Can not convert.     writeln(wtext); } void main() { string name = "Marvin"; wchar[255] wtext; import

Re: why do i need an extern(C): here?

2020-10-16 Thread WhatMeWorry via Digitalmars-d-learn
On Friday, 16 October 2020 at 15:14:03 UTC, Ali Çehreli wrote: On 10/15/20 2:42 PM, Ali Çehreli wrote: > I've recently done the same by calling dlopen() and dlsym() > directly. Runtime.loadLibrary documentation says "If the library > contains a D runtime it will be integrated with the current

How convert String to Fixed wchar Array?

2020-10-16 Thread Marcone via Digitalmars-d-learn
How convert String to Fixed wchar Array? import std; void main(){ string name = "Marvin"; wchar[255] wtext = name.to!(wchar[]); // Can not convert. writeln(wtext); }

Re: Why is `Appender._data` a pointer to its `Data`-store?

2020-10-16 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 17 October 2020 at 00:06:31 UTC, Steven Schveighoffer wrote: Appender is ref counted IIRC. -Steve It's not; it uses the GC.