Re: reduce condition nesting

2017-11-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:

Hello, is there way to reduce this condition:

if (c1) {
foo();
} else {
if (c2) {
bar();
} else {
if (c3) {
...
}
}
}


for instance in kotlin it can be replace with this:

when {
c1 -> foo(),
c2 -> bar(),
c3 -> ...
else -> someDefault()
}


do
{
if (c1) { foo(); break;}
if (c2) { bar(); break;}
if (c3) { baz(); break;}
someDefault();
} while (false);


Is d-apt.sourceforge.net functional?

2017-11-22 Thread Kai via Digitalmars-d-learn

I couldn't find the d-apt-keyring package anywhere

root@d9:~# cat /etc/apt/sources.list.d/d-apt.list
deb http://master.dl.sourceforge.net/project/d-apt/ d-apt main 
#APT repository for D
root@d9:~# LANG=C apt-get -y --allow-unauthenticated install 
--reinstall d-apt-keyring

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package d-apt-keyring

Cf.
http://d-apt.sourceforge.net/
https://sourceforge.net/projects/d-apt/files/dists/d-apt/

Meanwhile I just use 
http://downloads.dlang.org/releases/2.x/2.077.0/dmd_2.077.0-0_amd64.deb ...


glfwSetDropCallback undefined symbol

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

DCD and DMD says that the symbol is undefined!

However, I look into derelichtGLFW3. It has this symbol defined!

It looks like a bug for me!


Re: betterC and noboundscheck

2017-11-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 16:57:10 UTC, Joakim wrote:

On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:

[...]


betterC is a new feature that's still being worked on and still 
has holes in it:


https://github.com/dlang/dmd/pull/7151

I suggest you open an issue for it on bugzilla, as this sounds 
like either a hole or at least something that should be 
documented better:


https://issues.dlang.org


see also https://github.com/ldc-developers/ldc/pull/2426 (also 
WiP).


Re: Passing this to void *

2017-11-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:

I am a C++ game developer and I want to give it a try.

It seems "this" in Dlang is a reference instead of pointer.

How can I pass it as void *?

void foo(void *);

class Pizza {
public:
this() {
Pizza newone = this;
// works but newone is actually not this pizza.
foo();
// this does not work..
foo(this);
}
}

void main() {
Pizza pizza = new Pizza();
// this works...
foo();
}


Note that all the examples and advice in this thread apply to 
_classes_, not to structs.


The 'this' in a class is a pointer, since classes are reference 
types (like the 'this' pointer in C++) so you can cast it to a 
void* directly.


The 'this' in a struct is a value, since structs are value types. 
So if you want the address of the struct from within one of the 
struct's methods you need to use the & operator (then it becomes 
a pointer and you can cast it to a void* .




reduce condition nesting

2017-11-22 Thread Andrey via Digitalmars-d-learn

Hello, is there way to reduce this condition:

if (c1) {
foo();
} else {
if (c2) {
bar();
} else {
if (c3) {
...
}
}
}


for instance in kotlin it can be replace with this:

when {
c1 -> foo(),
c2 -> bar(),
c3 -> ...
else -> someDefault()
}




Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn

On Thursday, 23 November 2017 at 01:34:54 UTC, Ali Çehreli wrote:

On 11/22/2017 05:21 PM, Marc wrote:
On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M 
Davis wrote:
On Thursday, November 23, 2017 00:58:21 Marc via 
Digitalmars-d-learn wrote:

[...]


import std.traits;

enum countOfA = EnumMembers!A.length;

- Jonathna M Davis


This sounds more readable. I was going to write a "function 
extension" to enum but I think it isn't really needed. Thank 
you too.


As an eponymous template:

enum One { a }
enum Three { a, b, c }

import std.range : EnumMembers;
enum countOf(E) = EnumMembers!E.length;

unittest {
static assert(countOf!One == 1);
static assert(countOf!Three == 3);
}

void main() {
}

Ali


whoa, this is so easy and elegant. I'm falling in love with D. 
Thank you too!


Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Ali Çehreli via Digitalmars-d-learn

On 11/22/2017 05:21 PM, Marc wrote:

On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M Davis wrote:
On Thursday, November 23, 2017 00:58:21 Marc via Digitalmars-d-learn 
wrote:

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields


import std.traits;

enum countOfA = EnumMembers!A.length;

- Jonathna M Davis


This sounds more readable. I was going to write a "function extension" 
to enum but I think it isn't really needed. Thank you too.


As an eponymous template:

enum One { a }
enum Three { a, b, c }

import std.range : EnumMembers;
enum countOf(E) = EnumMembers!E.length;

unittest {
static assert(countOf!One == 1);
static assert(countOf!Three == 3);
}

void main() {
}

Ali


Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn
On Thursday, 23 November 2017 at 01:04:29 UTC, Jonathan M Davis 
wrote:
On Thursday, November 23, 2017 00:58:21 Marc via 
Digitalmars-d-learn wrote:

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields


import std.traits;

enum countOfA = EnumMembers!A.length;

- Jonathna M Davis


This sounds more readable. I was going to write a "function 
extension" to enum but I think it isn't really needed. Thank you 
too.


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, November 23, 2017 00:49:33 Mike Parker via Digitalmars-d-learn 
wrote:
> On Wednesday, 22 November 2017 at 22:45:53 UTC, A Guy With a
>
> Question wrote:
> > Out of curiosity, what does static mean in that context? When I
> > think of a static class I think of them in the context of Java
> > or C# where they can't be instantiated and where they are more
> > like namespaces that you can't directly import the contents of.
>
> Actually, they work exactly like Java's static nested classes in
> this context.
>
>
> class OuterClass {
>  ...
>  static class StaticNestedClass {
>  ...
>  }
> }
>
> OuterClass.StaticNestedClass nestedObject =
>   new OuterClass.StaticNestedClass();
>
> https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

I thought that that was the case, but my Java is so rusty that I wasn't
sure.

- Jonathan M Davis



Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, November 23, 2017 00:58:21 Marc via Digitalmars-d-learn wrote:
> for example:
>
> enum A { a = -10, b = -11, c = -12, d = -13, e = -34}
>
> enum int countOfA = coutOfFields(A); // 5 fields

import std.traits;

enum countOfA = EnumMembers!A.length;

- Jonathna M Davis



Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn
On Thursday, 23 November 2017 at 01:01:42 UTC, Michael V. 
Franklin wrote:

On Thursday, 23 November 2017 at 00:58:21 UTC, Marc wrote:

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields


https://dlang.org/spec/traits.html#allMembers

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = __traits(allMembers, A).length; // 5 fields

static assert(countOfA == 5);

Mike


This was fast! Thanks


Re: Can I count the of enum's fields at compile time?

2017-11-22 Thread Michael V. Franklin via Digitalmars-d-learn

On Thursday, 23 November 2017 at 00:58:21 UTC, Marc wrote:

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields


https://dlang.org/spec/traits.html#allMembers

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = __traits(allMembers, A).length; // 5 fields

static assert(countOfA == 5);

Mike


Can I count the of enum's fields at compile time?

2017-11-22 Thread Marc via Digitalmars-d-learn

for example:

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = coutOfFields(A); // 5 fields



Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 22:45:53 UTC, A Guy With a 
Question wrote:


Out of curiosity, what does static mean in that context? When I 
think of a static class I think of them in the context of Java 
or C# where they can't be instantiated and where they are more 
like namespaces that you can't directly import the contents of.


Actually, they work exactly like Java's static nested classes in 
this context.



class OuterClass {
...
static class StaticNestedClass {
...
}
}

OuterClass.StaticNestedClass nestedObject =
 new OuterClass.StaticNestedClass();

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, November 23, 2017 00:17:46 A Guy With a Question via 
Digitalmars-d-learn wrote:
> > here as non-static, nested class is associated with a specific
> > instance of the class and has access to that class instance via
> > its outer member.
> >
> > - Jonathan M Davis
>
> Hmmm...now you have me very intrigued. What is a use-case where
> you'd want to use a non-static embedded class? Sorry if I'm
> asking too many questions. But there's a lot to dig into with
> this language.

It would make sense with something like the nodes of a linked list if they
needed access to the container for some reason. Pretty much any case where a
an instance of a nested class is going to be associated with a specific
instance of its parent class and needs access to it would be a canditate.
It's not that uncommon to see cases in C++ or Java where you'd pass a
pointer to the "parent" to an instance of a nested class when it's created,
and having outer built-in is kind of like that.

Personally, I've never had a use for it. I don't even use classes much in D,
since I rarely need inheritance. And as I understand it, most D programs
don't use classes very heavily for that very reason. So, I have no idea how
common it is to use nested classes in this manner, but I expect that someone
has found it useful at some point.

I thought that this meaning of static for nested classes came from Java, but
it's been a while since I've done much with Java, so I don't know.

- Jonathan M Davis



Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
here as non-static, nested class is associated with a specific 
instance of the class and has access to that class instance via 
its outer member.


- Jonathan M Davis


Hmmm...now you have me very intrigued. What is a use-case where 
you'd want to use a non-static embedded class? Sorry if I'm 
asking too many questions. But there's a lot to dig into with 
this language.


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, November 22, 2017 22:45:53 A Guy With a Question via 
Digitalmars-d-learn wrote:
> On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven
>
> Schveighoffer wrote:
> > On 11/22/17 5:36 PM, Steven Schveighoffer wrote:
> >> This allows access to the outer class's members. So you need
> >> an instance to instantiate.
> >>
> >> I bet it's the same for interfaces.
> >
> > All that being said, the error message is quite lousy.
> >
> > -Steve
>
> Yup that worked. Thanks!
>
> Out of curiosity, what does static mean in that context? When I
> think of a static class I think of them in the context of Java or
> C# where they can't be instantiated and where they are more like
> namespaces that you can't directly import the contents of.

A static, nested class does not have access to the class that it's in. It's
basically a normal class that's namespaced within another class, whereas
non-static, nested class is associated with a specific instance of the class
and has access to that class instance via its outer member.

https://dlang.org/spec/class.html#nested
http://ddili.org/ders/d.en/nested.html

Similarly, if a struct, class, or function is nested within a function, and
it is not marked with static, then it has access to the function that it's
in, whereas if it's marked with static, then it acts more like a normal
struct/class/function and does not have access to the function that it's in
and is just namespaced within that function.

- Jonathan M Davis



Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 22:45:53 UTC, A Guy With a 
Question wrote:
On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven 
Schveighoffer wrote:

On 11/22/17 5:36 PM, Steven Schveighoffer wrote:
This allows access to the outer class's members. So you need 
an instance to instantiate.


I bet it's the same for interfaces.


All that being said, the error message is quite lousy.

-Steve


Yup that worked. Thanks!

Out of curiosity, what does static mean in that context? When I 
think of a static class I think of them in the context of Java 
or C# where they can't be instantiated and where they are more 
like namespaces that you can't directly import the contents of.


Clearly static has a slightly different meaning in D.


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven 
Schveighoffer wrote:

On 11/22/17 5:36 PM, Steven Schveighoffer wrote:
This allows access to the outer class's members. So you need 
an instance to instantiate.


I bet it's the same for interfaces.


All that being said, the error message is quite lousy.

-Steve


Yup that worked. Thanks!

Out of curiosity, what does static mean in that context? When I 
think of a static class I think of them in the context of Java or 
C# where they can't be instantiated and where they are more like 
namespaces that you can't directly import the contents of.


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/22/17 5:36 PM, Steven Schveighoffer wrote:
This allows access to the outer class's members. So you need an instance 
to instantiate.


I bet it's the same for interfaces.


All that being said, the error message is quite lousy.

-Steve


Re: Error: 'this' is only defined in non-static member functions

2017-11-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/22/17 5:19 PM, A Guy With a Question wrote:
I have an interface where I have a classes embedded in it's scope 
(trying to create something for the classes that implement the interface 
can use for unittesting).


     interface IExample
     {
     // stuff ...
     class Tester
     {

     }
     }

I'm trying to make an instance of it like:

     auto tester = new IExample.Tester();

And I get the above error.


Hm... not sure how this works for inner classes of Interfaces, but if 
it's anything like inner classes of classes, then they have a hidden 
'outer' pointer pointing at the owning instance of the class.


This allows access to the outer class's members. So you need an instance 
to instantiate.


I bet it's the same for interfaces.

What you probably want is a static class, which will remove that connection:

static class Tester
...

-Steve


Error: 'this' is only defined in non-static member functions

2017-11-22 Thread A Guy With a Question via Digitalmars-d-learn
I have an interface where I have a classes embedded in it's scope 
(trying to create something for the classes that implement the 
interface can use for unittesting).


interface IExample
{
// stuff ...
class Tester
{

}
}

I'm trying to make an instance of it like:

auto tester = new IExample.Tester();

And I get the above error.


Re: ESR on post-C landscape

2017-11-22 Thread Kagamin via Digitalmars-d-learn
Also 
http://ithare.com/chapter-vb-modular-architecture-client-side-programming-languages-for-games-including-resilience-to-reverse-engineering-and-portability/ scroll to the part about language choice.


Re: betterC and noboundscheck

2017-11-22 Thread Joakim via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:

Hello. I try compile simple example:

import core.stdc.stdio;
import std.algorithm : min;

extern (C) void main()
{
char[256] buf;
buf[] = '\0';

auto str = "hello world";
auto ln = min(buf.length, str.length);
buf[0..ln] = str[0..ln];
printf("%s\n", buf.ptr);
}

rdmd -betterC bettercarray2.d

and get error:

/tmp/.rdmd-1000/rdmd-bettercarray2.d-435C14EC3DAF09FFABF8ED6919B624C1/objs/bettercarray2.o:
 In function `main':
bettercarray2.d:(.text.main[main]+0xbc): undefined reference to 
`_d_arraycopy'

collect2: error: ld returned 1 exit status
Error: linker exited with status 1

If I understand correctly _d_arraycopy is part of druntime and 
it check bounds of array access.


If I add -noboundscheck flag all works fine.

dmd version is 2.076.1

Why -betterC flag not 'include' -noboundscheck flag?
It's bug or in some cases it's useful?


betterC is a new feature that's still being worked on and still 
has holes in it:


https://github.com/dlang/dmd/pull/7151

I suggest you open an issue for it on bugzilla, as this sounds 
like either a hole or at least something that should be 
documented better:


https://issues.dlang.org


Re: interfacing c++

2017-11-22 Thread drug via Digitalmars-d-learn

22.11.2017 19:06, Markus пишет:

another indicator (as documented) that GC destructor won't work
// extern(C++) classes don't have a classinfo pointer in their vtable so 
the GC can't finalize them
https://github.com/dlang/druntime/blob/3d8d4a45c01832fb657c16a656b6e1566d77fb21/src/rt/lifetime.d#L90 


annoying :(

Markus
I've made a guess that more c++ support were added in the last frontend 
version, well I was wrong


Re: interfacing c++

2017-11-22 Thread Markus via Digitalmars-d-learn

another indicator (as documented) that GC destructor won't work
// extern(C++) classes don't have a classinfo pointer in their 
vtable so the GC can't finalize them

https://github.com/dlang/druntime/blob/3d8d4a45c01832fb657c16a656b6e1566d77fb21/src/rt/lifetime.d#L90
annoying :(

Markus


Re: interfacing c++

2017-11-22 Thread Markus via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 08:43:54 UTC, drug wrote:

22.11.2017 02:12, Markus пишет:
What about dtor - you allocate class using D GC but try to 
destroy it manually - namely this I guess gives you an error in 
rt_finalize2 because it tries to destroy object that has been 
destroyed.
hmm... I'm not sure. I compiled dmd and it's druntime in debug 
and I'm stepping through it. Seems there has to be a "ClassInfo" 
filled with data. In my case it's filled with garbage. So the 
call ClassInfo.destructor fails, because destructor is an invalid 
pointer. The GC.collect works, but doesn't call the destructor.

https://github.com/dlang/druntime/blob/3d8d4a45c01832fb657c16a656b6e1566d77fb21/src/rt/lifetime.d#L1391
I know constructors and destructors are not supported. And I get 
that point for move/copy... constructors. But I don't get it for 
simple construction and destruction.


Markus


Re: dmd/ldc failed with exit code -11

2017-11-22 Thread Petar via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:33:46 UTC, Anonymouse wrote:

On Tuesday, 21 November 2017 at 19:22:47 UTC, Anonymouse wrote:
Compiling a debug dmd and running the build command in gdb, it 
seems to be a stack overflow at ddmd/dtemplate.d:6241, 
TemplateInstance::needsCodegen().


After a lot of trial and error I managed to find /a/ line which 
lets it compile under -b plain and release.


void colour(Sink, Codes...)(auto ref Sink sink, const Codes 
codes)

{
// Sink is a LockingTextWriter or an Appender!string
// Codes is a tuple of named enum members

foreach (const code; codes)
{
import std.conv : to;

if (++numCodes > 1) sink.put(';');

sink.put((cast(size_t)code).to!string);  // <--
}

Change size_t to uint and it compiles, keep it size_t and the 
compiler segfaults. Tested on two machines, both running 
up-to-date Arch linux, both with dmd and ldc.


The bug is too ephemeral to reduce well, if a thing like order 
of arguments matters.


If this is an emergent property of the rest of the program, and 
the size_t merely fells the house of cards, is it even worth 
reporting when I can't reduce it?


You did a good investigation and I still think it's important to 
report it.


I managed to find a few other cases where people were having 
issues with needsCodegen:


https://github.com/ldc-developers/ldc/issues/2168#issuecomment-312709632
https://github.com/ldc-developers/ldc/issues/2336
https://github.com/ldc-developers/ldc/issues/2022#issuecomment-288481397
https://github.com/ldc-developers/ldc/issues/1297#issuecomment-184770787

So there's enough evidence that there's a bug somewhere around 
that part of the compiler and we should gather good test cases to 
narrow down the problem.


Re: Passing this to void *

2017-11-22 Thread Dukc via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 15:34:53 UTC, Adam D. Ruppe 
wrote:
No, in both cases, if you do as I say, you will be passing the 
same address.


I was referring to his version of the main function that used &. 
But yes, if you do a cast instead it works just as you say.





Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 15:36:22 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 22 November 2017 at 15:31:36 UTC, Tim Hsu wrote:
It seems in D, reference has its own address, am I right? 
unlike c++


The local variable does have its own address. Do not take its 
address - avoid  or 


Just cast the ref itself.


In D, a class this or Object variable is already like a C++ 
Foo*. If you & that, you get a Foo** - not what you want in 
most cases.


Thanks after doing some experiment I guess I finally understand
class App {
@property void *ptr() {
return cast(void *)(this);
}
}
void printaddr(void *ptr)
{
  writeln(ptr);
}
void main()
{
Pizza pza = new Pizza("XD");
Pizza pza2 = pza;
printaddr(pza.ptr);
printaddr(pza2.ptr);
printaddr();
printaddr();
}

Result:
A32000
A32000
19FDB0
19FDB4


Conclusion:
pza and pza2 is two different reference variable refer to same 
new-ed object.


Re: Passing this to void *

2017-11-22 Thread Dukc via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:31:36 UTC, Tim Hsu wrote:
It seems in D, reference has its own address, am I right? 
unlike c++


In case of classes, yes. But I think function parameter 
references do not (or rather, of course they have since they 
exist in memory but it's hidden). Not sure though.


Re: Passing this to void *

2017-11-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:23:58 UTC, Tim Hsu wrote:

   m_window = glfwCreateWindow();
   glfwSetWindowUserPointer(m_window, cast(void *)());


That that & out of there!

glfwSetWindowUserPointer(m_window, cast(void *)(this));

without the &, you are fine.


Then, on the other side, you cast back. Again, do NOT dereference 
it, just cast it:


App app = cast(App) user_ptr;


Re: Passing this to void *

2017-11-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:31:36 UTC, Tim Hsu wrote:
It seems in D, reference has its own address, am I right? 
unlike c++


The local variable does have its own address. Do not take its 
address - avoid  or 


Just cast the ref itself.


In D, a class this or Object variable is already like a C++ Foo*. 
If you & that, you get a Foo** - not what you want in most cases.


Re: Passing this to void *

2017-11-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:27:27 UTC, Dukc wrote:
It's worth noting that you will still be passing different 
addresses to foo(void*) because classes are reference types in 
D (structs are not). In the constructor you're passing the 
address of the class object itself, but in the main function 
you're passing the address of the reference.


No, in both cases, if you do as I say, you will be passing the 
same address.


You cast the reference itself. Do NOT use the & operator at any 
point with class objects in D (unless you legit know what you are 
doing). Just plain cast it:


`cast(void*) this` // ok. gets address of class object itself
`Object o; foo(cast(void*) o);` // also ok, does same thing

`cast(void*) ` // NOT OK! DO NOT DO THIS address of a local
`cast(void*) `// NOT OK! DO NOT DO THIS address of a local


Re: dmd/ldc failed with exit code -11

2017-11-22 Thread Anonymouse via Digitalmars-d-learn

On Tuesday, 21 November 2017 at 19:22:47 UTC, Anonymouse wrote:
Compiling a debug dmd and running the build command in gdb, it 
seems to be a stack overflow at ddmd/dtemplate.d:6241, 
TemplateInstance::needsCodegen().


After a lot of trial and error I managed to find /a/ line which 
lets it compile under -b plain and release.


void colour(Sink, Codes...)(auto ref Sink sink, const Codes codes)
{
// Sink is a LockingTextWriter or an Appender!string
// Codes is a tuple of named enum members

foreach (const code; codes)
{
import std.conv : to;

if (++numCodes > 1) sink.put(';');

sink.put((cast(size_t)code).to!string);  // <--
}

Change size_t to uint and it compiles, keep it size_t and the 
compiler segfaults. Tested on two machines, both running 
up-to-date Arch linux, both with dmd and ldc.


The bug is too ephemeral to reduce well, if a thing like order of 
arguments matters.


If this is an emergent property of the rest of the program, and 
the size_t merely fells the house of cards, is it even worth 
reporting when I can't reduce it?


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:27:27 UTC, Dukc wrote:
On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch 
wrote:

 will do.


Even if it were an lvalue, that would be the address of a 
local. You should basically NEVER do that with D classes.


Just `cast(void*) this` if you must pass it to such a function.


It's worth noting that you will still be passing different 
addresses to foo(void*) because classes are reference types in 
D (structs are not). In the constructor you're passing the 
address of the class object itself, but in the main function 
you're passing the address of the reference.


It seems in D, reference has its own address, am I right? unlike 
c++


Re: Passing this to void *

2017-11-22 Thread Stefan Koch via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:23:58 UTC, Tim Hsu wrote:


I am afraid what will happen when casting this reference to 
void *




a ref is a ptr.
The cast will produce a ptr which is valid as long as the ref is 
valid.




Re: Passing this to void *

2017-11-22 Thread Dukc via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch 
wrote:

 will do.


Even if it were an lvalue, that would be the address of a 
local. You should basically NEVER do that with D classes.


Just `cast(void*) this` if you must pass it to such a function.


It's worth noting that you will still be passing different 
addresses to foo(void*) because classes are reference types in D 
(structs are not). In the constructor you're passing the address 
of the class object itself, but in the main function you're 
passing the address of the reference.


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch 
wrote:

 will do.


Even if it were an lvalue, that would be the address of a 
local. You should basically NEVER do that with D classes.


Just `cast(void*) this` if you must pass it to such a function.


I am afraid what will happen when casting this reference to void *

glfwSetWindowUserPointer gives us a chance to provide a pointer 
to userdata. so that in callback function, we can retrieve the 
data and don't have to declare global variable.


class App {
public this() {

   m_window = glfwCreateWindow();
   glfwSetWindowUserPointer(m_window, cast(void *)());
}
}

How do I use this function in Dlang?

sorry for my bad english.


Re: betterC and noboundscheck

2017-11-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:

Why -betterC flag not 'include' -noboundscheck flag?


-noboundscheck is extremely harmful. If -betterC implied that, it 
would no longer be a better C, it would just be the same buggy C.


The compiler should perhaps inline the bounds check so it doesn't 
need the druntime function, but it certainly shouldn't skip it.




Re: Passing this to void *

2017-11-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote:

 will do.


Even if it were an lvalue, that would be the address of a local. 
You should basically NEVER do that with D classes.


Just `cast(void*) this` if you must pass it to such a function.


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:14:32 UTC, Stefan Koch wrote:

On Wednesday, 22 November 2017 at 15:11:08 UTC, Tim Hsu wrote:

On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch


 will do.


I've tried it in the first place.

...

Error: this is not an lvalue


In that case casting to void* should be fine.


But..The compiler still does not produce the executable...

foo();

app.d(17): Error: this is not an lvalue


Re: Passing this to void *

2017-11-22 Thread Stefan Koch via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:11:08 UTC, Tim Hsu wrote:

On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch


 will do.


I've tried it in the first place.

...

Error: this is not an lvalue


In that case casting to void* should be fine.


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote:

On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:

I am a C++ game developer and I want to give it a try.

It seems "this" in Dlang is a reference instead of pointer.

How can I pass it as void *?

void foo(void *);

class Pizza {
public:
this() {
Pizza newone = this;
// works but newone is actually not this pizza.
foo();
// this does not work..
foo(this);
}
}

void main() {
Pizza pizza = new Pizza();
// this works...
foo();
}


 will do.


I've tried it in the first place.

...

Error: this is not an lvalue


betterC and noboundscheck

2017-11-22 Thread Oleg B via Digitalmars-d-learn

Hello. I try compile simple example:

import core.stdc.stdio;
import std.algorithm : min;

extern (C) void main()
{
char[256] buf;
buf[] = '\0';

auto str = "hello world";
auto ln = min(buf.length, str.length);
buf[0..ln] = str[0..ln];
printf("%s\n", buf.ptr);
}

rdmd -betterC bettercarray2.d

and get error:

/tmp/.rdmd-1000/rdmd-bettercarray2.d-435C14EC3DAF09FFABF8ED6919B624C1/objs/bettercarray2.o:
 In function `main':
bettercarray2.d:(.text.main[main]+0xbc): undefined reference to 
`_d_arraycopy'

collect2: error: ld returned 1 exit status
Error: linker exited with status 1

If I understand correctly _d_arraycopy is part of druntime and it 
check bounds of array access.


If I add -noboundscheck flag all works fine.

dmd version is 2.076.1

Why -betterC flag not 'include' -noboundscheck flag?
It's bug or in some cases it's useful?


Re: Passing this to void *

2017-11-22 Thread Stefan Koch via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:

I am a C++ game developer and I want to give it a try.

It seems "this" in Dlang is a reference instead of pointer.

How can I pass it as void *?

void foo(void *);

class Pizza {
public:
this() {
Pizza newone = this;
// works but newone is actually not this pizza.
foo();
// this does not work..
foo(this);
}
}

void main() {
Pizza pizza = new Pizza();
// this works...
foo();
}


 will do.


Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

I am a C++ game developer and I want to give it a try.

It seems "this" in Dlang is a reference instead of pointer.

How can I pass it as void *?

void foo(void *);

class Pizza {
public:
this() {
Pizza newone = this;
// works but newone is actually not this pizza.
foo();
// this does not work..
foo(this);
}
}

void main() {
Pizza pizza = new Pizza();
// this works...
foo();
}


Re: writeln, alias this and dynamic arrays.

2017-11-22 Thread matthewh via Digitalmars-d-learn

Thank you all for the helpful responses.
I will read more about ranges.


Re: Having "in" for arrays

2017-11-22 Thread Dukc via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 10:32:48 UTC, lobo wrote:

On Wednesday, 22 November 2017 at 09:36:43 UTC, Dukc wrote:
On Wednesday, 22 November 2017 at 08:03:50 UTC, Fra Mecca 
wrote:

void main()
{
auto v = ["r", "i", "o"];
assert ("r" in v);
}


Also note that even if it wereimplemented, you search for 'r' 
instead of "r". "r" is a string, but you would want to search 
for a char.


Isn't 'v' an array of strings?  If it were a array of chars, 
then the search would be 'r'.


bye,
lobo


Oops, you're correct. My bad.


Re: Having "in" for arrays

2017-11-22 Thread lobo via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 09:36:43 UTC, Dukc wrote:

On Wednesday, 22 November 2017 at 08:03:50 UTC, Fra Mecca wrote:

void main()
{
auto v = ["r", "i", "o"];
assert ("r" in v);
}


Also note that even if it wereimplemented, you search for 'r' 
instead of "r". "r" is a string, but you would want to search 
for a char.


Isn't 'v' an array of strings?  If it were a array of chars, then 
the search would be 'r'.


bye,
lobo



Re: Having "in" for arrays

2017-11-22 Thread Dukc via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 08:03:50 UTC, Fra Mecca wrote:

void main()
{
auto v = ["r", "i", "o"];
assert ("r" in v);
}


Also note that even if it wereimplemented, you search for 'r' 
instead of "r". "r" is a string, but you would want to search for 
a char.





Re: Having "in" for arrays

2017-11-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, November 22, 2017 08:03:50 Fra Mecca via Digitalmars-d-learn 
wrote:
> Why doesn't D have a in keyword for arrays?
>
> The docs explains that you can use in only for associative arrays
> but I don't see the reasons for such decision.
>
>
> Example code:
>
> void main()
> {
>   auto v = ["r", "i", "o"];
>   assert ("r" in v);
> }
>
>
> That currently fails:
> main.d(4): Error: incompatible types for (("r") in (v)): 'string'
> and 'string[]'

in is supposed to be at worst O(log n), whereas to do the operation for an
array would be O(n). If you want to search for a specific element in an
array than use std.algorithm.find or std.algorithm.canFind.

- Jonathan M Davis



Re: interfacing c++

2017-11-22 Thread drug via Digitalmars-d-learn

22.11.2017 02:12, Markus пишет:

snip

I could do the instancing/destruction by functions and write a custom d 
class that calls these methods in this()/~this(). 
This is what I used to do as special members like ctor/dtor did not 
supported in D before, but your example of using ctor is interesting.
What about dtor - you allocate class using D GC but try to destroy it 
manually - namely this I guess gives you an error in rt_finalize2 
because it tries to destroy object that has been destroyed.


Re: interfacing c++

2017-11-22 Thread MGW via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 08:29:26 UTC, MGW wrote:

Possibly it will be interesting


https://pp.userapi.com/c639524/v639524332/60240/uH3jnxrchik.jpg



Re: interfacing c++

2017-11-22 Thread MGW via Digitalmars-d-learn

On Tuesday, 21 November 2017 at 23:12:33 UTC, Markus wrote:
hi, im trying to interface a cpp class. I'd like to interface a 
bigger library and I'm trying to figure out the minimum effort.


Possibly it will be interesting 
https://www.youtube.com/watch?v=HTgJaRRfLPk