Re: Very simple null reference escape

2019-06-02 Thread Basile B. via Digitalmars-d-learn

On Sunday, 2 June 2019 at 07:55:27 UTC, Amex wrote:

A.B

If A is null, crash.

A?.B : writeln("HAHA");

No crash, ignored, equivalent to

if (A is null) writeln("HAHA"); else A.B;


safeAccess from iz does this : 
https://github.com/Basile-z/iz/blob/master/import/iz/sugar.d#L1666


Re: Very simple null reference escape

2019-06-02 Thread Amex via Digitalmars-d-learn

On Sunday, 2 June 2019 at 14:37:48 UTC, Paul Backus wrote:

On Sunday, 2 June 2019 at 07:55:27 UTC, Amex wrote:

A.B

If A is null, crash.

A?.B : writeln("HAHA");

No crash, ignored, equivalent to

if (A is null) writeln("HAHA"); else A.B;


The "optional" package on dub [1] has a .dispatch method that 
does this:


auto d = some(A());

// Dispatch to one of its methods

d.dispatch.f(); // calls a.f, returns some(4)
d.dispatch.inner.g(); // calls a.inner.g, returns some(7)

// Use on a pointer or reference type as well
A* e = null;

// If there's no value in the reference type, dispatching
// works, and produces an optional
assert(e.dispatch.f() == none);
assert(e.dispatch.inner.g() == none);


Full example: https://run.dlang.io/is/SmsGQu

[1] https://code.dlang.org/packages/optional


thanks.


Re: Very simple null reference escape

2019-06-02 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 2 June 2019 at 07:55:27 UTC, Amex wrote:

A.B

If A is null, crash.

A?.B : writeln("HAHA");

No crash, ignored, equivalent to

if (A is null) writeln("HAHA"); else A.B;


The "optional" package on dub [1] has a .dispatch method that 
does this:


auto d = some(A());

// Dispatch to one of its methods

d.dispatch.f(); // calls a.f, returns some(4)
d.dispatch.inner.g(); // calls a.inner.g, returns some(7)

// Use on a pointer or reference type as well
A* e = null;

// If there's no value in the reference type, dispatching
// works, and produces an optional
assert(e.dispatch.f() == none);
assert(e.dispatch.inner.g() == none);


Full example: https://run.dlang.io/is/SmsGQu

[1] https://code.dlang.org/packages/optional


Re: this is null

2019-03-10 Thread ANtlord via Digitalmars-d-learn

On Sunday, 10 March 2019 at 14:25:56 UTC, spir wrote:


There is a typo in this instruction:

T* ptr = this.list.getFisrtFreeOrAdd(memViewLen).getPtr!T();
^^
rs
(may this explain your null? the compiler should complain)

diniz


Good catch! But I have the same typo within the definition of the 
method. I believe DMD screams about undefined method if it 
happens.


Re: this is null

2019-03-10 Thread spir via Digitalmars-d-learn

On 09/03/2019 21:10, ANtlord via Digitalmars-d-learn wrote:

On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:


You can end up with a null `this` reference if you dereference a null pointer 
to a struct and then call a method on the result. For example:




I can but my reference is not null before calling. Take a look at the line of 
code [0]. There is a check before the line.


https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 [0]


There is a typo in this instruction:

T* ptr = this.list.getFisrtFreeOrAdd(memViewLen).getPtr!T();
^^
rs
(may this explain your null? the compiler should complain)

diniz




Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

On Saturday, 9 March 2019 at 21:00:51 UTC, Ali Çehreli wrote:

I haven't run the code but which pointer is null? Try adding
I mean `this` by "this" word. You can see that `this` is null if 
you run gdb and before that line make `p/x this` [0]

this check as well:

  auto node = this.list.getFisrtFreeOrAdd(memViewLen);
  assert(node !is null);

I get segfault in `getFisrtFreeOrAdd` method. Before the line I 
have an assertion [1]. It looks like the program misses (I have 
no idea how) `list` object while calling its method 
`getFisrtFreeOrAdd`.


[0] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56
[1] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L19





Re: this is null

2019-03-09 Thread Ali Çehreli via Digitalmars-d-learn

On 03/09/2019 12:10 PM, ANtlord wrote:

On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:


You can end up with a null `this` reference if you dereference a null 
pointer to a struct and then call a method on the result. For example:




I can but my reference is not null before calling. Take a look at the 
line of code [0]. There is a check before the line.


https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 
[0]


I haven't run the code but which pointer is null? Try adding this check 
as well:


  auto node = this.list.getFisrtFreeOrAdd(memViewLen);
  assert(node !is null);

Ali


Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:


struct S
{
bool isThisNull() { return  is null; }
}

void main()
{
import.std.stdio;
S* p = null;
writeln((*p).isThisNull); // true
}

Interactive version: https://run.dlang.io/is/fgT2rS


Anyway, thank you! I didn't know about the feature.


Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:


You can end up with a null `this` reference if you dereference 
a null pointer to a struct and then call a method on the 
result. For example:




I can but my reference is not null before calling. Take a look at 
the line of code [0]. There is a check before the line.


https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 [0]


Re: this is null

2019-03-09 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 9 March 2019 at 19:18:38 UTC, ANtlord wrote:
Hello everyone! I've encountered the problem which I already 
encountered before. Unfortunately, I had no time in the 
previous time to report and to talk about it. So I decided to 
play making my own "malloc" function in pure D (betterC) at 
this time. And I encountered the issue one more time. `this` 
can be null. How? Please take a look at my project [0]. It gets 
the current heap break and tries to increase via a free list. 
So the segfault I meet happens there [1]. Tell me, please. What 
do I wrong?


[0] https://github.com/ANtlord/deadmemory
[1] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56


You can end up with a null `this` reference if you dereference a 
null pointer to a struct and then call a method on the result. 
For example:


struct S
{
bool isThisNull() { return  is null; }
}

void main()
{
import.std.stdio;
S* p = null;
writeln((*p).isThisNull); // true
}

Interactive version: https://run.dlang.io/is/fgT2rS


Re: assigment to null class object member compiled? is this a bug?

2018-10-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 19 October 2018 at 06:53:32 UTC, dangbinghoo wrote:

why the code bellow compiles?


D compilers are allowed to make that an error, but it might not.

With the current implementation,

dmd that.d

will compile, but

dmd -O that.d

will fail with an error. Yes, turning on optimizations happens to 
catch the null flow at compile time.


It is just a matter of compiler implementation happening to catch 
it or not.


Re: assigment to null class object member compiled? is this a bug?

2018-10-22 Thread Alex via Digitalmars-d-learn

On Monday, 22 October 2018 at 01:39:48 UTC, dangbinghoo wrote:

On Friday, 19 October 2018 at 09:08:32 UTC, Vijay Nayar wrote:
Technically the code you have is syntactically correct.  You 
are permitted to create a class variable without assigning it 
to a class object.  (Assigning it to a class object would look 
like "A a = new A();")


Which section of The D Programming Language book makes you 
think this would not compile?  I have the book as well, but 
I'm not quite sure what part of the book you're referring to.


the section 6.2, which is
---
A a;
a.x = 5;
---

the book explained this should be refused to compile.


thanks!

--
dangbinghoo


You are wrong, actually:
In the book, site 179 it states:
"If you try to access a non-static member of a reference and the 
compiler can prove statically that the reference would definitely 
be null, it will refuse to compile the code."


But the compiler is not able to prove that the reference is 
definitely null. At least in this case, with compiler, I think of.

That's why it called runtime error.


Re: assigment to null class object member compiled? is this a bug?

2018-10-21 Thread dangbinghoo via Digitalmars-d-learn

On Friday, 19 October 2018 at 09:08:32 UTC, Vijay Nayar wrote:
Technically the code you have is syntactically correct.  You 
are permitted to create a class variable without assigning it 
to a class object.  (Assigning it to a class object would look 
like "A a = new A();")


Which section of The D Programming Language book makes you 
think this would not compile?  I have the book as well, but I'm 
not quite sure what part of the book you're referring to.


the section 6.2, which is
---
A a;
a.x = 5;
---

the book explained this should be refused to compile.


thanks!

--
dangbinghoo


Re: assigment to null class object member compiled? is this a bug?

2018-10-19 Thread Vijay Nayar via Digitalmars-d-learn

On Friday, 19 October 2018 at 06:53:32 UTC, dangbinghoo wrote:

hi,

why the code bellow compiles?

---
import std.stdio;
class A {
int m;
}

void main() {
A a;
a.m = 1;
}
---

and running this code get:

`segmentation fault (core dumped)  ./test`

I consider this couldn't be compiled according to book Programming Language>.


The latest dmd (2.082) and LDC2 behaves the same.


Technically the code you have is syntactically correct.  You are 
permitted to create a class variable without assigning it to a 
class object.  (Assigning it to a class object would look like "A 
a = new A();")


Which section of The D Programming Language book makes you think 
this would not compile?  I have the book as well, but I'm not 
quite sure what part of the book you're referring to.


Re: toString contains null for struct with function/method

2018-04-09 Thread number via Digitalmars-d-learn

On Sunday, 8 April 2018 at 15:51:05 UTC, Paul Backus wrote:

On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote:

writeln(s2);// S2(0, null)


S2 is a nested struct [1], which means it has a hidden pointer 
field that's used to access its enclosing scope. If you change 
the definition to `static struct S2`, you'll get the same 
output for both structs.


[1] https://dlang.org/spec/struct.html#nested


Aha, its the nesting. And if i access the outer scope in the 
function its not null anymore.

Thank you!


Re: toString contains null for struct with function/method

2018-04-08 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote:
the write() shows a 'null' if the struct has a function/method. 
why is that?

```
import std.stdio;

void main()
{
struct S
{
int i;
}
S s;
writeln(s);// S(0)
writeln(typeid(s).sizeof); // 8

struct S2
{
int i;
this(this){}
}
S2 s2;
import std.conv: to;
writeln(s2);// S2(0, null)
writeln(typeid(s2).sizeof); // 8
}
```


S2 is a nested struct [1], which means it has a hidden pointer 
field that's used to access its enclosing scope. If you change 
the definition to `static struct S2`, you'll get the same output 
for both structs.


[1] https://dlang.org/spec/struct.html#nested


Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-08 Thread Jacob Carlborg via Digitalmars-d-learn
On Thursday, 8 March 2018 at 08:04:54 UTC, Nick Sabalausky 
(Abscissa) wrote:


Interesting. I was using vibe.d 'v0.8.3-rc.1' (which doesn't 
appear to work on run.dlang.io). But it does seem to work for 
me if I use 'v0.8.3-alpha.1'.


I wonder what could have changed to result in this?


It's a struct in "v0.8.3-rc.1" [1]. In "v0.8.3-rc.1" the new 
vibe-core package is the default configuration.


[1] 
https://github.com/vibe-d/vibe-core/blob/master/source/vibe/core/net.d#L467


--
/Jacob Carlborg


Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-08 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

On 03/08/2018 03:04 AM, Nick Sabalausky (Abscissa) wrote:


Interesting. I was using vibe.d 'v0.8.3-rc.1' (which doesn't appear to 
work on run.dlang.io). But it does seem to work for me if I use 
'v0.8.3-alpha.1'.


I wonder what could have changed to result in this?


https://github.com/vibe-d/vibe.d/issues/2108


Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-08 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

On 03/08/2018 01:13 AM, Nicholas Wilson wrote:


That does seem odd.
---
/+dub.sdl:
dependency "vibe-d" version="~>0.8.3-alpha.1"
+/
import vibe.core.net;
import std.stdio;
TCPConnection mySocket;

void main() {
     auto b = mySocket is null;
     writeln(b);
}
---
works fine on run.dlang.io


Interesting. I was using vibe.d 'v0.8.3-rc.1' (which doesn't appear to 
work on run.dlang.io). But it does seem to work for me if I use 
'v0.8.3-alpha.1'.


I wonder what could have changed to result in this?


Re: Can't "is null" an interface?!?! Incompatible types???

2018-03-07 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 8 March 2018 at 04:48:08 UTC, Nick Sabalausky 
(Abscissa) wrote:

-
import vibe.core.net;
TCPConnection mySocket;

void main() {
auto b = mySocket is null;
}
-

That's giving me:

-
Error: incompatible types for (mySocket) is (null): 
TCPConnection and typeof(null)

-

WTF?!?!

The type in question (vibe.core.net.TCPConnection) is an 
interface:

http://vibed.org/api/vibe.core.net/TCPConnection
https://github.com/vibe-d/vibe.d/blob/master/core/vibe/core/net.d#L344

The following works just fine:
-
interface IFoo {}

void main() {
IFoo i;
auto b = i is null;
}
-


That does seem odd.
---
/+dub.sdl:
dependency "vibe-d" version="~>0.8.3-alpha.1"
+/
import vibe.core.net;
import std.stdio;
TCPConnection mySocket;

void main() {
auto b = mySocket is null;
writeln(b);
}
---
works fine on run.dlang.io


Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 22 September 2016 at 19:51:31 UTC, Nordlöw wrote:

A known bug?


Yeah, it shows the line before instead of the line of. But it 
still shows basically where you are.


Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread ketmar via Digitalmars-d-learn

p.s. that is the reason it is not turned on by default, btw.


Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread ketmar via Digitalmars-d-learn

On Thursday, 22 September 2016 at 19:51:31 UTC, Nordlöw wrote:

A known bug?


prolly. segfault handler is highly non-standard hack, it may miss 
exact position or something. as is "it is not guaranteed to work, 
and if it will work, it is not guaranteed to work correctly".


Re: Stacktrace on Null Pointer Derefence

2016-09-22 Thread Nordlöw via Digitalmars-d-learn

On Thursday, 22 September 2016 at 00:46:19 UTC, ketmar wrote:

{ import etc.linux.memoryerror; registerMemoryErrorHandler(); }


Thx! That at least triggered an exception. However the line 
number for the innermost function is wrong. For instance


1   void boom()
2   {
3   int* y = null;
4   *y = 42;// boom
5   }

gives stacktrace

etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d(325)

??:? [0x404e6d]
??:? [0x404dba]
t_segFaulException.d:3 [0x402301]
t_segFaulException.d:22 [0x402370]
??:? [0x402a2e]
??:? [0x402978]
??:? [0x4029ea]
??:? [0x402978]
??:? [0x4028e9]
??:? [0x40247f]
??:? __libc_start_main [0x2867d82f]

A known bug?


Re: Stacktrace on Null Pointer Derefence

2016-09-21 Thread Jonathan Marler via Digitalmars-d-learn

On Wednesday, 21 September 2016 at 23:36:08 UTC, Nordlöw wrote:

Doing a null deref such as

int* y = null;
*y = 42;// boom

[...]


Can you include compiler command line?  I use -g -gs -debug to 
get stack traces on windows.


Re: Stacktrace on Null Pointer Derefence

2016-09-21 Thread ketmar via Digitalmars-d-learn

On Wednesday, 21 September 2016 at 23:36:08 UTC, Nordlöw wrote:


Is there a way to get a stacktrace instead?


{ import etc.linux.memoryerror; registerMemoryErrorHandler(); }

warning: this is hightly system-specific, and may work or not 
work depending of the moon phase, and may broke on any system 
upgrade.


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Adam D. Ruppe via Digitalmars-d-learn
Don't use string == null, it is true for empty strings since null 
and an empty string are almost interchangable.


You can try if(string is null) - is instead of ==. Though usually 
in D, I just if(string.length == 0) and treat empty and null the 
same way.


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread tcak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 11:45:31 UTC, tcak wrote:

I have written a function as follows:

public bool setCookie(
string name,
string value,
long maxAgeInSeconds = long.min,
string expiresOnGMTDate=null,
string path=null,
string domain=null,
bool secure=false
) shared{

// if headers are sent already, leave
if( headersSent ) return false;

// name cannot be empty
if( (name is null) || (name.length = 0) ) return false;

writeln(
Name: , name,
  Max Age: , maxAgeInSeconds,
  Expires null: , (expiresOnGMTDate == null),
  Path equals null: , (path == null),
  Domain null: , (domain is null)
);

return true;
}



Here is the testing code:

responseObject.setCookie( A, B );
auto now = std.datetime.Clock.currTime().toSimpleString();
//writeln(Now |, now, |);
responseObject.setCookie( Response Time, now );


Here is the results:

Name: A  Max Age: -9223372036854775808  Expires null: true  
Path equals null: true  Domain null: true


Name: Response Time  Max Age: -9223372036854775808  Expires 
null: true  Path equals null: false  Domain null: false



I don't know what is happening though, somehow path and domain 
parameters in second use of function are not null even I 
haven't given any value to them.


If I uncomment the writeln line in test code, it turns 
normal. I am so much confused right now. What is happening here?


Well, I have tried same code without objects in a test code, and 
it is null now as expected. Completely same code though.


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread tcak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 14:49:24 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:42:33 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:30:07 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:25:56 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote:
By the way, I am using DMD64 D Compiler v2.067.0 on 
Ubuntu 14.04.


I have Archlinux DMD64 D Compiler v2.067.0 and it works OK 
for me.


WOW

rdmd app.d(without params):
Ok rdmd and dub works because they are use ldc, but do not me 
ask how. I always think that dub and rdmd should use dmd 
compiler until I tell them otherwise


I try it with DMD64 D Compiler v2.066 and same problem occured. 
So probably some backend problem. You should create an issue on 
https://issues.dlang.org


Reported. I simplified the example as well. Removed a big part of 
code and used assert instead of writeln. My rdmd generates 
assertion exception with simplified code. I hope it can be tested 
later as well.


https://issues.dlang.org/show_bug.cgi?id=14430


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Daniel Kozak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 14:42:33 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:30:07 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:25:56 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote:
By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 
14.04.


I have Archlinux DMD64 D Compiler v2.067.0 and it works OK 
for me.


WOW

rdmd app.d(without params):
Ok rdmd and dub works because they are use ldc, but do not me 
ask how. I always think that dub and rdmd should use dmd 
compiler until I tell them otherwise


I try it with DMD64 D Compiler v2.066 and same problem occured. 
So probably some backend problem. You should create an issue on 
https://issues.dlang.org


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Daniel Kozák via Digitalmars-d-learn

On Thu, 09 Apr 2015 11:45:30 +
tcak via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:

 I have written a function as follows:
 
 public bool setCookie(
   string name,
   string value,
   long maxAgeInSeconds = long.min,
   string expiresOnGMTDate=null,
   string path=null,
   string domain=null,
   bool secure=false
 ) shared{
 
   // if headers are sent already, leave
   if( headersSent ) return false;
 
   // name cannot be empty
   if( (name is null) || (name.length = 0) ) return false;
 
   writeln(
   Name: , name,
 Max Age: , maxAgeInSeconds,
 Expires null: , (expiresOnGMTDate == null),
 Path equals null: , (path == null),
 Domain null: , (domain is null)
   );
 
   return true;
 }
 
 
 
 Here is the testing code:
 
 responseObject.setCookie( A, B );
 auto now = std.datetime.Clock.currTime().toSimpleString();
 //writeln(Now |, now, |);
 responseObject.setCookie( Response Time, now );
 
 
 Here is the results:
 
 Name: A  Max Age: -9223372036854775808  Expires null: true  Path 
 equals null: true  Domain null: true
 
 Name: Response Time  Max Age: -9223372036854775808  Expires null: 
 true  Path equals null: false  Domain null: false
 
 
 I don't know what is happening though, somehow path and domain 
 parameters in second use of function are not null even I haven't 
 given any value to them.
 
 If I uncomment the writeln line in test code, it turns normal. 
 I am so much confused right now. What is happening here?

Can you post full example somewhere, this code works ok for me:

import std.stdio;
import std.datetime;

class Response
{
public bool setCookie(
string name,
string value,
long maxAgeInSeconds = long.min,
string expiresOnGMTDate=null,
string path=null,
string domain=null,
bool secure=false
) shared
{

// name cannot be empty
if( (name is null) || (name.length = 0) ) return false;

writeln(
Name: , name,
  Max Age: , maxAgeInSeconds,
  Expires null: , (expiresOnGMTDate == null),
  Path equals null: , (path == null),
  Domain null: , (domain is null)
);

return true;
}
}

void main()
{
auto response = new shared Response();
response.setCookie( A, B );
auto now = std.datetime.Clock.currTime().toSimpleString();
//writeln(Now |, now, |);
response.setCookie( Response Time, now );
}


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Daniel Kozák via Digitalmars-d-learn

On Thu, 09 Apr 2015 11:04:47 -0400
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 
 Note that the bad behavior (which was just fixed BTW) is
 if(somearr), which used to mean if(somearr.ptr), and now it's a
 compiler error.
 
 -Steve

Yeah, because of this I must change almost 1k lines in my codebase :(



Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Daniel Kozák via Digitalmars-d-learn

On Thu, 09 Apr 2015 11:04:47 -0400
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 
 Note that the bad behavior (which was just fixed BTW) is
 if(somearr), which used to mean if(somearr.ptr), and now it's a
 compiler error.
 
 -Steve

Yeah, because of this I must change almost 1k lines in my codebase



Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Daniel Kozak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 14:30:07 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:25:56 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote:
By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 
14.04.


I have Archlinux DMD64 D Compiler v2.067.0 and it works OK for 
me.


WOW

rdmd app.d(without params):
Ok rdmd and dub works because they are use ldc, but do not me ask 
how. I always think that dub and rdmd should use dmd compiler 
until I tell them otherwise




Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread tcak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 13:32:38 UTC, Adam D. Ruppe wrote:
Don't use string == null, it is true for empty strings since 
null and an empty string are almost interchangable.


You can try if(string is null) - is instead of ==. Though 
usually in D, I just if(string.length == 0) and treat empty and 
null the same way.


I replaced all == with is for path and domain. Here are results:

Name: A 1  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 2  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 3  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 4  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 5  Expires null: true  Path equals null: false  Domain 
null: false

~
Name: A 6  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 7  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 8  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 9  Expires null: true  Path equals null: false  Domain 
null: false

~
Name: A10  Expires null: true  Path equals null: false  Domain 
null: false
Name: A11  Expires null: true  Path equals null: false  Domain 
null: false
Name: A12  Expires null: true  Path equals null: false  Domain 
null: false
Name: A13  Expires null: true  Path equals null: false  Domain 
null: false



Could you try the code yourself as well? Because something is 
clearly wrong here. I even have removed shared from everywhere, 
results are still as above. There is no way Expires becomes 
null, and Path and Domain become false.


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/9/15 9:32 AM, Adam D. Ruppe wrote:

Don't use string == null, it is true for empty strings since null and an
empty string are almost interchangable.


I think this is not good advice. Comparing string to null is perfectly 
fine with ==. It's fine *because* null and empty strings are the same 
thing, so comparing against null is essentially comparing against an 
empty string.



You can try if(string is null) - is instead of ==. Though usually in D,
I just if(string.length == 0) and treat empty and null the same way.


This is likely not what you want, it's generally not important where a 
string is located.


Note that the bad behavior (which was just fixed BTW) is if(somearr), 
which used to mean if(somearr.ptr), and now it's a compiler error.


-Steve


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Daniel Kozak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 14:25:56 UTC, Daniel Kozak wrote:

On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote:
By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 
14.04.


I have Archlinux DMD64 D Compiler v2.067.0 and it works OK for 
me.


WOW

rdmd app.d(without params):
Name: A 1  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 2  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 3  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 4  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 5  Expires null: true  Path equals null: true  Domain 
null: true

~
Name: A 6  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 7  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 8  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 9  Expires null: true  Path equals null: true  Domain 
null: true

~
Name: A10  Expires null: true  Path equals null: true  Domain 
null: true
Name: A11  Expires null: true  Path equals null: true  Domain 
null: true
Name: A12  Expires null: true  Path equals null: true  Domain 
null: true
Name: A13  Expires null: true  Path equals null: true  Domain 
null: true




dmd -O:
Name: A 1  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 2  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 3  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 4  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 5  Expires null: true  Path equals null: false  Domain 
null: false

~
Name: A 6  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 7  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 8  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 9  Expires null: true  Path equals null: false  Domain 
null: false

~
Name: A10  Expires null: true  Path equals null: false  Domain 
null: false
Name: A11  Expires null: true  Path equals null: false  Domain 
null: false
Name: A12  Expires null: true  Path equals null: false  Domain 
null: false
Name: A13  Expires null: true  Path equals null: false  Domain 
null: false


dmd -release:
Name: A 1  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 2  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 3  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 4  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 5  Expires null: true  Path equals null: false  Domain 
null: false

~
Name: A 6  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 7  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 8  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 9  Expires null: true  Path equals null: false  Domain 
null: false

~
Name: A10  Expires null: true  Path equals null: false  Domain 
null: false
Name: A11  Expires null: true  Path equals null: false  Domain 
null: false
Name: A12  Expires null: true  Path equals null: false  Domain 
null: false
Name: A13  Expires null: true  Path equals null: false  Domain 
null: false


with ldc everything is ok



Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 9 April 2015 at 15:04:47 UTC, Steven Schveighoffer 
wrote:
You can try if(string is null) - is instead of ==. Though 
usually in D,
I just if(string.length == 0) and treat empty and null the 
same way.


This is likely not what you want, it's generally not important 
where a string is located.


I think you were replying to the first sentence, but I recommend 
the second sentence: just always use `if(string.length)` and 
forget abotu where it is stored.


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread tcak via Digitalmars-d-learn
By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 
14.04.


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread Daniel Kozak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote:
By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 
14.04.


I have Archlinux DMD64 D Compiler v2.067.0 and it works OK for me.


Re: Parameter is null by default. No value is given. Code says it is not null.

2015-04-09 Thread tcak via Digitalmars-d-learn

On Thursday, 9 April 2015 at 12:06:49 UTC, Daniel Kozák wrote:


On Thu, 09 Apr 2015 11:45:30 +
tcak via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com wrote:



Can you post full example somewhere, this code works ok for me:

import std.stdio;
import std.datetime;

class Response
{
public bool setCookie(
string name,
string value,
long maxAgeInSeconds = long.min,
string expiresOnGMTDate=null,
string path=null,
string domain=null,
bool secure=false
) shared
{

// name cannot be empty
if( (name is null) || (name.length = 0) ) return 
false;


writeln(
Name: , name,
  Max Age: , maxAgeInSeconds,
  Expires null: , (expiresOnGMTDate == 
null),

  Path equals null: , (path == null),
  Domain null: , (domain is null)
);

return true;
}
}

void main()
{
auto response = new shared Response();
response.setCookie( A, B );
auto now = std.datetime.Clock.currTime().toSimpleString();
//writeln(Now |, now, |);
response.setCookie( Response Time, now );
}


I have listed full code down below. It is written in test.d file 
and I run it with rdmd test.d. Its result is as below for me:


tolga@tolga-H97M-D3H:~/Desktop$ rdmd test.d
Name: A 1  Expires null: true  Path equals null: true  Domain 
null: true
Name: A 2  Expires null: true  Path equals null: true  Domain 
null: false
Name: A 3  Expires null: true  Path equals null: true  Domain 
null: false
Name: A 4  Expires null: true  Path equals null: true  Domain 
null: false
Name: A 5  Expires null: true  Path equals null: true  Domain 
null: false

~
Name: A 6  Expires null: true  Path equals null: false  Domain 
null: false
Name: A 7  Expires null: true  Path equals null: true  Domain 
null: false
Name: A 8  Expires null: true  Path equals null: true  Domain 
null: false
Name: A 9  Expires null: true  Path equals null: true  Domain 
null: false

~
Name: A10  Expires null: true  Path equals null: false  Domain 
null: false
Name: A11  Expires null: true  Path equals null: true  Domain 
null: false
Name: A12  Expires null: true  Path equals null: true  Domain 
null: false
Name: A13  Expires null: true  Path equals null: true  Domain 
null: false



[[code]]

import std.stdio;
import std.datetime;

public class HttpResponse{
public void setCookie(
string name,
string value,
long maxAgeInSeconds = long.min,
string expiresOnGMTDate=null,
string path=null,
string domain=null,
bool secure=false
) shared{
writeln(
Name: , name,
  Expires null: , (expiresOnGMTDate == null),
  Path equals null: , (path == null),
  Domain null: , (domain is null)
);
}

public void hellYeah(
string name,
string value,
long maxAgeInSeconds = long.min,
string expiresOnGMTDate=null,
string path=null,
string domain=null,
bool secure=false
) shared{
writeln(
Name: , name,
  Expires null: , (expiresOnGMTDate == null),
  Path equals null: , (path == null),
  Domain null: , (domain is null)
);
}

public void hellYeah2(
string name,
string value,
long maxAgeInSeconds = long.min,
string expiresOnGMTDate=null,
string path=null,
string domain=null,
bool secure=false
) shared{
writeln(
Name: , name,
  Expires null: , (expiresOnGMTDate == null),
  Path equals null: , (path == null),
  Domain null: , (domain is null)
);
}
}

void main(){
auto responseObject = new shared HttpResponse();

responseObject.hellYeah( A 1, B1 );
responseObject.hellYeah( A 2, B2 );
responseObject.hellYeah( A 3, B3 );
responseObject.hellYeah( A 4, B4 );
responseObject.hellYeah( A 5, B5 );

writeln(~);

responseObject.setCookie( A 6, B6 );
responseObject.setCookie( A 7, B7 );
responseObject.setCookie( A 8, B8 );
responseObject.setCookie( A 9, B9 );

writeln(~);

responseObject.hellYeah2( A10, B10 );
responseObject.hellYeah2( A11, B11 );
responseObject.hellYeah2( A12, B12 );
responseObject.hellYeah2( A13, B13 );
}


Re: Cannot alias null

2014-06-13 Thread via Digitalmars-d-learn
On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
What I was really trying to do was D'ify C expressions like 
this:


  typedef ((struct t*)0) blah;


This doesn't compile for me with GCC, and I don't know what it's 
supposed to mean. ((struct t*) 0) is a value, not a type...


Where does it come from?


Re: Cannot alias null

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn

On Thursday, 12 June 2014 at 22:54:20 UTC, Ali Çehreli wrote:

On 06/12/2014 03:38 PM, monarch_dodra wrote:
 So there's something special about null.

The difference is that null is an expression. It is the same 
limitation as not being able to alias a literal.


alias zero = 0;
alias blah = null;


Oh! Right. That makes sense.

So you should use enum instead:
enum zero = 0;
enum blah = null;

Thanks.


Re: Cannot alias null

2014-06-13 Thread Tom Browder via Digitalmars-d-learn
On Fri, Jun 13, 2014 at 7:59 AM, via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via
 Digitalmars-d-learn wrote:

 What I was really trying to do was D'ify C expressions like this:

   typedef ((struct t*)0) blah;


 This doesn't compile for me with GCC, and I don't know what it's supposed to
 mean. ((struct t*) 0) is a value, not a type...

Sorry, you're correct.  It is from a C macro and would be used for an
rvalue.  Something like this:

$ cat chdr.h
struct t;
#define t_nullptr ((struct t*)0)
struct t* t_ptr = t_nullptr;

After pre-processing with gcc -E -P that should read:

$ cat chdr.h.i
struct t;
struct t* t_ptr = ((struct t*)0);

which does compile.

So I'm not sure how to translate that into D.   I do know my first
attempt here doesn't work, even with it being surrounded by extern (C)
{}:

$ cat chdr.d
struct t;
struct t* t_ptr = null;

 Where does it come from?

The usage comes from many of the C API headers in the BRL-CAD package
(http://brlcad.org).

Best,

-Tom


Re: Cannot alias null

2014-06-13 Thread Philpax via Digitalmars-d-learn
On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via 
Digitalmars-d-learn wrote:

On Fri, Jun 13, 2014 at 7:59 AM, via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:

On Thursday, 12 June 2014 at 21:07:47 UTC, Tom Browder via
Digitalmars-d-learn wrote:


What I was really trying to do was D'ify C expressions like 
this:


  typedef ((struct t*)0) blah;



This doesn't compile for me with GCC, and I don't know what 
it's supposed to

mean. ((struct t*) 0) is a value, not a type...


Sorry, you're correct.  It is from a C macro and would be used 
for an

rvalue.  Something like this:

$ cat chdr.h
struct t;
#define t_nullptr ((struct t*)0)
struct t* t_ptr = t_nullptr;

After pre-processing with gcc -E -P that should read:

$ cat chdr.h.i
struct t;
struct t* t_ptr = ((struct t*)0);

which does compile.

So I'm not sure how to translate that into D.   I do know my 
first
attempt here doesn't work, even with it being surrounded by 
extern (C)

{}:

$ cat chdr.d
struct t;
struct t* t_ptr = null;


Where does it come from?


The usage comes from many of the C API headers in the BRL-CAD 
package

(http://brlcad.org).

Best,

-Tom


Remove the struct from the pointer:

struct t;
t* t_ptr = null;


Re: Cannot alias null

2014-06-13 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
So I'm not sure how to translate that into D.   I do know my 
first
attempt here doesn't work, even with it being surrounded by 
extern (C)

{}:

$ cat chdr.d
struct t;
struct t* t_ptr = null;


This seems to work fine for me. What's the problem?

Note this isn't *strictly* the same as what you have in C, as 
null is not strongly bindded to a single type. If you want a 
null pointer which is pre-emptivelly strongly, then you can use 
an eponymous template:


enum nullptr(T) = (T*).init;

auto p = nullptr!t;

Throw in an alias, and you got it working exactly like an C:


enum nullptr(T) = (T*).init;
alias t_nullptr = nullptr!t;
struct t* t_ptr = t_nullptr;

That said, I'd advise against this. Just use null and move on. 
It's idiomatic.


Re: Cannot alias null

2014-06-13 Thread Tom Browder via Digitalmars-d-learn
On Fri, Jun 13, 2014 at 10:15 AM, monarch_dodra via
Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:
 On Friday, 13 June 2014 at 15:05:49 UTC, Tom Browder via Digitalmars-d-learn
 wrote:

 So I'm not sure how to translate that into D.   I do know my first
 attempt here doesn't work, even with it being surrounded by extern (C)
 {}:

 $ cat chdr.d
 struct t;
 struct t* t_ptr = null;


 This seems to work fine for me. What's the problem?

I use dmd -c .d (note file name change) and get:

t.d(2): Error: { } expected following aggregate declaration
t.d(2): Error: Declaration expected, not '*'

Taking Philpax's  suggestion I try

$ cat t.d
struct t;
t* t_ptr = null;

$ dmd -c t.d

and get a good build.

 Note this isn't *strictly* the same as what you have in C, as null is not
...
 strongly bindded to a single type. If you want a null pointer which is
 pre-emptivelly strongly, then you can use an eponymous template:
...
 That said, I'd advise against this. Just use null and move on. It's
 idiomatic.

So this is the correct (i.e., good enough) solution then:

$ cat t.d
struct t;
t* t_ptr = null;

Sounds good to me.

Thanks all.

Best,

-Tom


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

On 06/12/2014 01:26 PM, Tom Browder via Digitalmars-d-learn wrote:

 This will not compile:

alias blah = null;

 The dmd message are:

 di/test_hdr.d(10): Error: basic type expected, not null
 di/test_hdr.d(10): Error: semicolon expected to close alias declaration
 di/test_hdr.d(10): Error: Declaration expected, not 'null'

 Are there any other objects that cannot be aliased?

alias works only with types. Being an expression (not an object), null 
cannot not work with alias.


Ali



Re: Cannot alias null

2014-06-12 Thread Andrew Edwards via Digitalmars-d-learn

On 6/12/14, 4:29 PM, Ali Çehreli wrote:

On 06/12/2014 01:26 PM, Tom Browder via Digitalmars-d-learn wrote:

  This will not compile:
 
 alias blah = null;
 
  The dmd message are:
 
  di/test_hdr.d(10): Error: basic type expected, not null
  di/test_hdr.d(10): Error: semicolon expected to close alias declaration
  di/test_hdr.d(10): Error: Declaration expected, not 'null'
 
  Are there any other objects that cannot be aliased?

alias works only with types. Being an expression (not an object), null
cannot not work with alias.

Ali



void foo() {}
alias bar = foo();

Am I just misunderstanding what is meant by types?


Re: Cannot alias null

2014-06-12 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn 
wrote:
 This will not compile:
 
   alias blah = null;
[...]

'null' is a value, not a type. Try:

alias blah = typeof(null);


T

-- 
If it's green, it's biology, If it stinks, it's chemistry, If it has numbers 
it's math, If it doesn't work, it's technology.


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

On 06/12/2014 01:36 PM, Andrew Edwards wrote:

 void foo() {}
 alias bar = foo();

 Am I just misunderstanding what is meant by types?

Seems to be an old behavior. That does not compile with 2.066:

Error: function declaration without return type. (Note that constructors 
are always named 'this')


The following compiles though:

alias bar = foo;

I stand corrected: alias works not only with types but with symbols as 
well. I was right about the original code though: Aliases cannot be 
used for expressions.


Ali



Re: Cannot alias null

2014-06-12 Thread Tom Browder via Digitalmars-d-learn
On Thu, Jun 12, 2014 at 3:42 PM, H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via Digitalmars-d-learn 
 wrote:
 This will not compile:

   alias blah = null;
 [...]

 'null' is a value, not a type. Try:

 alias blah = typeof(null);

Great, that works!

What I was really trying to do was D'ify C expressions like this:

  typedef ((struct t*)0) blah;

So, taking your advice, I found this to work (at least it compiles as
a translation:

  alias blah = typeof(null);

Thanks,T and Ali.

Best,

-Tom


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

On 06/12/2014 02:06 PM, Tom Browder via Digitalmars-d-learn wrote:

 What I was really trying to do was D'ify C expressions like this:

typedef ((struct t*)0) blah;

Is that actually a function pointer typedef? I can't parse that line. :)

 So, taking your advice, I found this to work (at least it compiles as
 a translation:

alias blah = typeof(null);

I suspect you need something else. :)

Ali



Re: Cannot alias null

2014-06-12 Thread Tom Browder via Digitalmars-d-learn
On Thu, Jun 12, 2014 at 4:17 PM, Ali Çehreli
digitalmars-d-learn@puremagic.com wrote:
 On 06/12/2014 02:06 PM, Tom Browder via Digitalmars-d-learn wrote:
...
 What I was really trying to do was D'ify C expressions like this:

typedef ((struct t*)0) blah;
...
 So, taking your advice, I found this to work (at least it compiles as
 a translation:

alias blah = typeof(null);

 I suspect you need something else. :)

Undoubtedly, indeed!  [Still a WIP (work in progress).]

Best,

-Tom



Re: Cannot alias null

2014-06-12 Thread Adam D. Ruppe via Digitalmars-d-learn

since null is a value maybe you want

enum blah = null;

you may also give it a type after the enum word


Re: Cannot alias null

2014-06-12 Thread Tom Browder via Digitalmars-d-learn
On Thu, Jun 12, 2014 at 4:58 PM, Adam D. Ruppe via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com wrote:
 since null is a value maybe you want

 enum blah = null;

That works.

 you may also give it a type after the enum word

But I can't get any other variant to work so far.

-Tom


Re: Cannot alias null

2014-06-12 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 12 June 2014 at 20:44:16 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
On Thu, Jun 12, 2014 at 03:26:13PM -0500, Tom Browder via 
Digitalmars-d-learn wrote:

This will not compile:

  alias blah = null;

[...]

'null' is a value, not a type. Try:

alias blah = typeof(null);


T


Yet you can alias variables...

int i;
alias j = i;

So there's something special about null.


Re: Cannot alias null

2014-06-12 Thread monarch_dodra via Digitalmars-d-learn

On Thursday, 12 June 2014 at 21:58:32 UTC, Adam D. Ruppe wrote:

since null is a value maybe you want

enum blah = null;

you may also give it a type after the enum word


I *think* the issue might be that null is an rvalue? Because 
you can alias variable names all you want. I do it all the time 
for templates where I *may* need a temporary.


eg:

void foo(T)(T val)
{
static if (isUnsigned!T)
alias uval = val;
else
auto uval = unsigned(val);
...
}

It's also quite useful with varargs:
alias a0 = args[0];

Also, you can't alias things like int.init either. I'm not sure 
the rvalue thing is the source, because these work:


//struct S
{
static int i;
static int j() @property;
}
alias a = S.i;
alias b = S.j;
//

I'd consider filling a bug report.


Re: Cannot alias null

2014-06-12 Thread Ali Çehreli via Digitalmars-d-learn

On 06/12/2014 03:38 PM, monarch_dodra wrote:

 Yet you can alias variables...

 int i;
 alias j = i;

Initially I forgot about the fact that symbols can be alias'ed as well. 
So that's fine.


 So there's something special about null.

The difference is that null is an expression. It is the same limitation 
as not being able to alias a literal.


alias zero = 0;
alias blah = null;

Those two declarations fail for the same reason:

  Error: basic type expected, not 0
  Error: semicolon expected to close alias declaration
  Error: basic type expected, not null
  Error: semicolon expected to close alias declaration

The pair of error messages are somewhat silly: The first one is 
misleading because as we know, it should say basic type *or symbol* 
expected; and the second one is bogus because there actually is a 
semicolon there: :p


Ali



Re: Why is null lowercase?

2013-01-27 Thread Phil Lavoie

DO YOU PREFER A LANGUAGE ALL IN UPPERCASE?



Hahahaha! I find it ugly too. I prefer lowercaps null, as in Java.


Re: Why is null lowercase?

2013-01-27 Thread Phil Lavoie

On Friday, 25 January 2013 at 16:11:57 UTC, Maxim Fomin wrote:

On Friday, 25 January 2013 at 14:22:20 UTC, Don wrote:

On Friday, 25 January 2013 at 01:17:44 UTC, Ali Çehreli wrote:

On 01/24/2013 12:42 PM, Matthew Caron wrote:

 for not null checks

 if ( ptr !is null) ...

 And too much perl has me wanting to write:

 if (ptr is not null)

IIRC, the !is operator is thanks to bearophile.


No, it's from 2002 (well, it was !==, renamed to !is in 2005).
Bearophile only joined us about the time D2 began, in late 
2007.


It would be nice a to have a wiki page about D history written 
by old-timers.

I vote for that too!


Re: Why is null lowercase?

2013-01-25 Thread Don

On Friday, 25 January 2013 at 01:17:44 UTC, Ali Çehreli wrote:

On 01/24/2013 12:42 PM, Matthew Caron wrote:

 for not null checks

 if ( ptr !is null) ...

 And too much perl has me wanting to write:

 if (ptr is not null)

IIRC, the !is operator is thanks to bearophile.


No, it's from 2002 (well, it was !==, renamed to !is in 2005).
Bearophile only joined us about the time D2 began, in late 2007.


Re: Why is null lowercase?

2013-01-25 Thread Ali Çehreli

On 01/25/2013 06:22 AM, Don wrote:

 IIRC, the !is operator is thanks to bearophile.

 No, it's from 2002 (well, it was !==, renamed to !is in 2005).
 Bearophile only joined us about the time D2 began, in late 2007.

Ok. How about !in then? Did he lobby for that one? :)

Ali



Re: Why is null lowercase?

2013-01-25 Thread Maxim Fomin

On Friday, 25 January 2013 at 14:22:20 UTC, Don wrote:

On Friday, 25 January 2013 at 01:17:44 UTC, Ali Çehreli wrote:

On 01/24/2013 12:42 PM, Matthew Caron wrote:

 for not null checks

 if ( ptr !is null) ...

 And too much perl has me wanting to write:

 if (ptr is not null)

IIRC, the !is operator is thanks to bearophile.


No, it's from 2002 (well, it was !==, renamed to !is in 2005).
Bearophile only joined us about the time D2 began, in late 2007.


It would be nice a to have a wiki page about D history written by 
old-timers.


Re: Why is null lowercase?

2013-01-25 Thread Era Scarecrow

On Friday, 25 January 2013 at 14:43:01 UTC, Ali Çehreli wrote:

On 01/25/2013 06:22 AM, Don wrote:
No, it's from 2002 (well, it was !==, renamed to !is in 2005). 
Bearophile only joined us about the time D2 began, in late 
2007.


Ok. How about !in then? Did he lobby for that one? :)


   //hmmm doesn't read right
   if (ptr in not null)

   //huh? is it an array or an AA now?
   if (ptr not in null)

   //ummm feels like an AA. I'm sure if we used
   //it it would become second nature.
   if (ptr !in null)

   //silently converts to...?
   if (!(ptr in null))

   //make sense to me. Course in java === was
   //used for ptr checking rather than contents.
   if (ptr === null) //is null
   if (ptr !== null) //not null, both stand out

   //mentally I reverse !is to equal 'is not'.
   //I know I'm comparing pointers.
   if (ptr !is null)


  Code example:
  string[string] aa;
  string* ptr;

  if (dog   in aa)//returns ptr
  if (dog  !in aa)//i think it's bool of 'found'
  ptr = dog in aa;
  if (ptr in null)  //errors, not an aa, searching null?
  if (ptr!in null)  //not searching null?

  change to...
  string[string[string]] aa;
  string[string]* ptr; //(string[string])* ptr; ??

  if (dog   in aa)   //returns ptr (of an aa), search
  if (dog  !in aa)   //still makes sense... a search.
  ptr = dog in aa;
  if (ptr in null) //becomes ((*ptr) in aa), search?

  //(!((*ptr) in null))//AA search or pointer compare?
  if (ptr!in null)

  null can still be replaced by any variable/pointer, if that 
pointer is an aa as well... Ugg I don't wanna find out all the 
combinations to figure it out...


 After looking at all these 'in' should be reserved for array 
searching, not pointer checking. It makes more sense to me that 
way.


Re: Why is null lowercase?

2013-01-25 Thread Ali Çehreli

On 01/25/2013 10:31 AM, Era Scarecrow wrote:
 On Friday, 25 January 2013 at 14:43:01 UTC, Ali Çehreli wrote:
 On 01/25/2013 06:22 AM, Don wrote:
 No, it's from 2002 (well, it was !==, renamed to !is in 2005).
 Bearophile only joined us about the time D2 began, in late 2007.

 Ok. How about !in then? Did he lobby for that one? :)

 //ummm feels like an AA. I'm sure if we used
 //it it would become second nature.
 if (ptr !in null)

Isn't that an error to apply the in operator to null? The expression 
above is syntactic sugar for the following one:


  !null.opBinaryRight!in(ptr))

Yes, there is also opBinary!in but it doesn't make sense to me to put 
the container on the left-hand side ever:


  if (myContainer in myElement)

Doesn't make sense.

 After looking at all these 'in' should be reserved for array searching,
 not pointer checking. It makes more sense to me that way.

Sorry if I implied otherwise. Yes, 'in' should be for that purpose. I 
merely tried to remember what syntax has been bearophile's strong 
suggestion. ;)


Ali



Re: Why is null lowercase?

2013-01-25 Thread Era Scarecrow

On Friday, 25 January 2013 at 18:57:06 UTC, Ali Çehreli wrote:

On 01/25/2013 10:31 AM, Era Scarecrow wrote:
After looking at all these 'in' should be reserved for array 
searching, not pointer checking. It makes more sense to me 
that way.


Sorry if I implied otherwise. Yes, 'in' should be for that 
purpose. I merely tried to remember what syntax has been 
bearophile's strong suggestion. ;)


 Not so much implied as considered. Had it been used, it would be 
obvious there was problems with it, both with meanings and added 
ambiguities.


 Oh well. One thing at a time..


Re: Why is null lowercase?

2013-01-24 Thread bearophile

Matthew Caron:

Of all of the differences between C and D, the one which I have 
the most difficulty adapting to is null being lowercase. Does 
anyone know why this decision was made?


Probably because writing all in uppercase ugly. null is a keyword 
like the others, and they are in lowercase. DO YOU PREFER A 
LANGUAGE ALL IN UPPERCASE?


Bye,
bearophile


Re: Why is null lowercase?

2013-01-24 Thread Mike Parker

On Thursday, 24 January 2013 at 12:56:03 UTC, Matthew Caron wrote:

This is probably a question for Walter, but maybe others know.

Of all of the differences between C and D, the one which I have 
the most difficulty adapting to is null being lowercase. Does 
anyone know why this decision was made?


In the world of C and C++, 'NULL' is a macro. Macros, by 
convention, are all uppercase. Contrast that with C++11 which 
provides for 'nullptr', a type rather than a macro. Consider 
Java, which also has a lowercase null. In D, null follows the 
same convention as other built-ins, so it is lowercase. To me, it 
makes perfect sense. There are no macros in D, so I wouldn't have 
expected to see NULL to begin with. *That* would have been highly 
inconsistent.


Re: Why is null lowercase?

2013-01-24 Thread monarch_dodra

On Thursday, 24 January 2013 at 12:56:03 UTC, Matthew Caron wrote:

This is probably a question for Walter, but maybe others know.

Of all of the differences between C and D, the one which I have 
the most difficulty adapting to is null being lowercase. Does 
anyone know why this decision was made?


Keep in mind that strictly speeking, NULL != null:

NULL is a C macro that expands to 0.
null is a D keyword that cannot be implicitly cast to an integer.

This is a source of bugs:

//
void foo(int);
void foo(int*);
//

in C++:
foo(NULL); //Calls void foo(int)

in D:
foo(null); //Calls void foo(int*)

Having code that is valid in both C++ and D, but having a 
different behavior would be VERY bad.



BTW, you can be thankful that it is *just* null, because what 
it really is C++11's null_ptr. Which is worst.


Re: Why is null lowercase?

2013-01-24 Thread Leandro Motta Barros
Hi,

In C, NULL is a #define, and #defines are typically all-caps. In D,
null is real keyword recognized by the compiler, and those are
typically lowercase. I am just guessing here, but I'd say the choice
for 'null' instead of 'NULL' is just to be coherent with this.

Personally, I kinda like 'null'. :-)

LMB




On Thu, Jan 24, 2013 at 10:56 AM, Matthew Caron matt.ca...@redlion.net wrote:
 This is probably a question for Walter, but maybe others know.

 Of all of the differences between C and D, the one which I have the most
 difficulty adapting to is null being lowercase. Does anyone know why this
 decision was made?
 --
 Matthew Caron, Software Build Engineer
 Sixnet, a Red Lion business | www.sixnet.com
 +1 (518) 877-5173 x138 office


Re: Why is null lowercase?

2013-01-24 Thread Rob T

On Thursday, 24 January 2013 at 12:56:03 UTC, Matthew Caron wrote:

This is probably a question for Walter, but maybe others know.

Of all of the differences between C and D, the one which I have 
the most difficulty adapting to is null being lowercase. Does 
anyone know why this decision was made?


You'll get used to it, it's actually much better than typing in 
NULL, and it's a real type instead on an int, which never worked 
well in C.


Just be warned that when checking for null *do not* use equality 
operator


if ( ptr == null) ...

instead use the identity operator is

if ( ptr is null) ...

for not null checks

if ( ptr !is null) ...

BTW, half of what you thought worked well in C/C++ will get 
turned upside down if you stick with D, and once you get it, 
moving back to C/C++ becomes unbearable.


--rt


Re: Why is null lowercase?

2013-01-24 Thread Ali Çehreli

On 01/24/2013 04:56 AM, Matthew Caron wrote:

This is probably a question for Walter, but maybe others know.

Of all of the differences between C and D, the one which I have the most
difficulty adapting to is null being lowercase. Does anyone know why
this decision was made?


Similarly, the common macros TRUE and FALSE are replaced by the 'true' 
and 'false' keywords.


Ali


Re: Why is null lowercase?

2013-01-24 Thread Matthew Caron

On 01/24/2013 12:50 PM, Ali Çehreli wrote:

Similarly, the common macros TRUE and FALSE are replaced by the 'true'
and 'false' keywords.


Ironically, those don't bother me because I never used them.

--
Matthew Caron, Software Build Engineer
Sixnet, a Red Lion business | www.sixnet.com
+1 (518) 877-5173 x138 office


Re: Why is null lowercase?

2013-01-24 Thread Matthew Caron

On 01/24/2013 12:04 PM, Rob T wrote:

You'll get used to it, it's actually much better than typing in NULL,
and it's a real type instead on an int, which never worked well in C.

Just be warned that when checking for null *do not* use equality operator


Yeah, the compiler helped me find that one out. That takes a little 
getting used to as well. Old habits and such.



for not null checks

if ( ptr !is null) ...


And too much perl has me wanting to write:

if (ptr is not null)


BTW, half of what you thought worked well in C/C++ will get turned
upside down if you stick with D, and once you get it, moving back to
C/C++ becomes unbearable.


It already is. I have very little desire to do anything in any other 
language. C and C++ are too primitive. Java and C# don't play as nicely 
with native libraries as I'd like and require a whole VM which consumes 
gobs of memory and takes forever to start. D gives me the features I 
want from Java and C# while falling somewhere between them and C in 
terms of speed. I can get more done, faster, in D.


Meanwhile, it seems like the rest of the world is moving towards writing 
everything in JavaScript, and that's just leaving me scratching my head 
in amazement.

--
Matthew Caron, Software Build Engineer
Sixnet, a Red Lion business | www.sixnet.com
+1 (518) 877-5173 x138 office


Re: Why is null lowercase?

2013-01-24 Thread Ali Çehreli

On 01/24/2013 12:42 PM, Matthew Caron wrote:

 for not null checks

 if ( ptr !is null) ...

 And too much perl has me wanting to write:

 if (ptr is not null)

IIRC, the !is operator is thanks to bearophile. We would have to reverse 
the logic before he insisted on !is: :)


if (!(ptr is null))

Ali



Re: HWND is NULL but GetLastError returns 0x00

2009-10-17 Thread Zarathustra
I filled out all fields of wndclassex and operands of createWindowEx exactly 
the same like in identical (working) C program, and there are still the same 
problem.
//
void main(){
  try{
ptr handle;
//Window wnd = new Window();
WndClassEx wndClass;

wndClass.size  = 0x0030;
wndClass.style = 0x;
wndClass.wndProc   = cast(ptr)wndProc;
wndClass.clsExtraBytes = 0x;
wndClass.wndExtraBytes = 0x;
wndClass.hInstance = kernel32.getModuleHandle(null);
wndClass.hIcon = user32.loadIcon(null, 0x7F00);
wndClass.hCursor   = user32.loadCursor(null, 0x7F00);
wndClass.hbrBackground = gdi32.getStockObject(0x);
wndClass.menuName  = null;
wndClass.className = cast(str)clsname;
wndClass.hIconSm   = user32.loadIcon(null, 0x7F00);


if(!user32.registerClassEx(cast(ptr)wndClass)){
  user32.messageBox(null, 
user32.translateErrorCode(kernel32.getLastError()), cast(str)error, 
0x);
  assert(false, Window class registring failed);
}

handle = user32.createWindowEx(
  0,
  wndClass.className,
  cast(str)applicationcaption,
  0x00CF,
  0,
  0,
  640,
  480,
  null,
  null,
  kernel32.getModuleHandle(null),
  null
);

if(handle is null){
  // ERROR_SUCCESS
  user32.messageBox(null, 
user32.translateErrorCode(kernel32.getLastError()), cast(str)error, 
0x);
  assert(handle);
}
  }
  catch(Object o){
MsgBox(cast(char[])o.toString);
  }
}
//

div0 Wrote:

 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1
 
 Zarathustra wrote:
 
  I have the problem with the following code in D2:
  CreateWindowEx returns NULL but I haven't got idea why?
 
 
 snip
 
 That's because your are not properly processing all of the messages that
 are involved in window creation.
 
 See:
 
 http://msdn.microsoft.com/en-us/library/ms632635(VS.85).aspx
 
 or better yet, have a look at my port of atlwin.h:
 
 http://www.sstk.co.uk/atlWinD.php
 
 all of this tedious shit has been done before.
 
 - --
 My enormous talent is exceeded only by my outrageous laziness.
 http://www.ssTk.co.uk
 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.7 (MingW32)
 Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
 
 iD8DBQFK2Pq7T9LetA9XoXwRAqHlAKDPIZwXCWSvjNNviUX4SEguGFA+awCgoV7j
 VHP6w/x+jpQ42lOhyYxN0/o=
 =xX0G
 -END PGP SIGNATURE-



Re: HWND is NULL but GetLastError returns 0x00

2009-10-17 Thread Zarathustra
Ok thanks, My fault ;p


Re: HWND is NULL but GetLastError returns 0x00

2009-10-16 Thread div0
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Zarathustra wrote:

 I have the problem with the following code in D2:
 CreateWindowEx returns NULL but I haven't got idea why?


snip

That's because your are not properly processing all of the messages that
are involved in window creation.

See:

http://msdn.microsoft.com/en-us/library/ms632635(VS.85).aspx

or better yet, have a look at my port of atlwin.h:

http://www.sstk.co.uk/atlWinD.php

all of this tedious shit has been done before.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFK2Pq7T9LetA9XoXwRAqHlAKDPIZwXCWSvjNNviUX4SEguGFA+awCgoV7j
VHP6w/x+jpQ42lOhyYxN0/o=
=xX0G
-END PGP SIGNATURE-


Re: delegate !is null

2009-09-10 Thread Steven Schveighoffer

On Tue, 08 Sep 2009 18:13:56 -0400, Saaa em...@needmail.com wrote:


Hope this one makes any sense :)

C c = new C;
C mouseOverObject = c;

int delegate() deleg = mouseOverObject.getSomeVariable;
mouseOverObject = null;

int value;
void write()
{
if(deleg !is null) //how do I make this check for (mouseOverObject !is
null)?


if(mouseOverObject !is null)

That's the only way.  A delegate does not magically become set to null  
when you set the original object to null or delete the original object.   
It is a separate pointer that is only set when you set it.


What you are asking is the equivalent of this:

int x = 5;
int y = x;

x = 0;

if(y != 0) // how do I check through y that x is now 0?

However, setting mouseOverObject to null does *not* destroy the object, as  
long as the delegate exists, it is still pointing to the object, so it  
will not be cleaned by the GC.


So your delegate is still valid as long as you don't delete  
mouseOverObject manually.


-Steve


Re: delegate !is null

2009-09-08 Thread Saaa

Steven Schveighoffer schvei...@yahoo.com wrote in message 
news:op.uzxs4wyreav...@localhost.localdomain...
 On Sun, 06 Sep 2009 18:54:47 -0400, Saaa em...@needmail.com wrote:

 I'd like to set D's delegate to a method which is not yet available (like
 c.method).
 I solved this by encapsulating the method within a function literal, but 
 I
 also need to know whether
 the method is available or not when calling the delegate.
 I could do this by making the function literal include the null-checking
 code, but is there maybe a better solution to this problem?
 The delegate is supposed to change a variable within the D class.
 Hope you understand it :)

 A delegate is a struct with a data pointer and a function pointer.  You 
 can access the individual parts via .ptr and .func (I believe).  You can 
 even change them via those properties.

 does that help?

 -Steve

I did read that part.
The problem lies more in that I'd like to point to something which is not 
there yet.
In the code 'c.method()' is not there yet, as c is null.
Maybe I should create a dummy object for c to point to in stead of null ?
That way I point the delegate to the dummy method and ignore it as long as 
it is pointing
to the dummy method :)
The only drawback to this is that all objects I want to point the delegate 
to,
need to somehow be convertable to the dummy type (interface/abstract class),
meaning it will be less flexible.





Re: delegate !is null

2009-09-08 Thread Steven Schveighoffer

On Tue, 08 Sep 2009 09:22:30 -0400, Saaa em...@needmail.com wrote:



Steven Schveighoffer schvei...@yahoo.com wrote in message
news:op.uzxs4wyreav...@localhost.localdomain...

On Sun, 06 Sep 2009 18:54:47 -0400, Saaa em...@needmail.com wrote:

I'd like to set D's delegate to a method which is not yet available  
(like

c.method).
I solved this by encapsulating the method within a function literal,  
but

I
also need to know whether
the method is available or not when calling the delegate.
I could do this by making the function literal include the  
null-checking

code, but is there maybe a better solution to this problem?
The delegate is supposed to change a variable within the D class.
Hope you understand it :)


A delegate is a struct with a data pointer and a function pointer.  You
can access the individual parts via .ptr and .func (I believe).  You can
even change them via those properties.

does that help?

-Steve


I did read that part.
The problem lies more in that I'd like to point to something which is not
there yet.
In the code 'c.method()' is not there yet, as c is null.
Maybe I should create a dummy object for c to point to in stead of null ?
That way I point the delegate to the dummy method and ignore it as long  
as

it is pointing
to the dummy method :)
The only drawback to this is that all objects I want to point the  
delegate

to,
need to somehow be convertable to the dummy type (interface/abstract  
class),

meaning it will be less flexible.


Hm... I'm still confused.  Why not just set the delegate to null?  Why do  
you need to have the delegate set to something?


There are ways to do it, without having a class instance, but it is messy.

-Steve


Re: delegate !is null

2009-09-08 Thread Saaa
 The problem lies more in that I'd like to point to something which is not
 there yet.
 In the code 'c.method()' is not there yet, as c is null.
 Maybe I should create a dummy object for c to point to in stead of null ?
 That way I point the delegate to the dummy method and ignore it as long 
 as
 it is pointing
 to the dummy method :)
 The only drawback to this is that all objects I want to point the 
 delegate
 to,
 need to somehow be convertable to the dummy type (interface/abstract 
 class),
 meaning it will be less flexible.

 Hm... I'm still confused.  Why not just set the delegate to null?  Why do 
 you need to have the delegate set to something?
It is for the gui. I give it a list of things to display.
And some of these things don't yet exist or can be deleted at any time.
I'd like it to display the last valid value.


 There are ways to do it, without having a class instance, but it is messy.
how messy? :D

Kind of related:
If you delete an object and later create a new object, what are the chances 
they are located on the
same place (deleted.ptr is new.ptr) ?
Does the garbage collector try to reuse locations or is it the opposite (or 
random) ? 




Re: delegate !is null

2009-09-08 Thread div0
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Saaa wrote:
 The problem lies more in that I'd like to point to something which is not
 there yet.
 In the code 'c.method()' is not there yet, as c is null.
 Maybe I should create a dummy object for c to point to in stead of null ?
 That way I point the delegate to the dummy method and ignore it as long 
 as
 it is pointing
 to the dummy method :)
 The only drawback to this is that all objects I want to point the 
 delegate
 to,
 need to somehow be convertable to the dummy type (interface/abstract 
 class),
 meaning it will be less flexible.
 Hm... I'm still confused.  Why not just set the delegate to null?  Why do 
 you need to have the delegate set to something?
 It is for the gui. I give it a list of things to display.
 And some of these things don't yet exist or can be deleted at any time.
 I'd like it to display the last valid value.
 
 There are ways to do it, without having a class instance, but it is messy.
 how messy? :D

The only way I've found so far to do static binding like you are talking
about is using string mixins. My port of Atl's window classes uses a MFC
like message map:

mixin messageMap!(
  msgRangeHdlr!(WM_MOUSEFIRST, WM_MOUSELAST, wmMouseEvents),
  msgHdlr!(WM_PAINT, wmPaint),
  msgHdlr!(WM_SIZE, wmSize),
  msgHdlr!(WM_KEYUP, wmKeyUp),
  msgHdlr!(WM_CHAR, wmChar),

  msgHdlr!(WM_SHOWWINDOW, wmShow),
  msgHdlr!(WM_CLOSE, wmClose),
  msgHdlr!(WM_CREATE, wmCreate)
);

Having to put the classes method in a string is a bit ugly, but without
c++ style pointer to members I don't see any other way of doing it.

If you want to have a look in more detail:

http://www.sstk.co.uk/atlWinD.php


 Kind of related:
 If you delete an object and later create a new object, what are the chances 
 they are located on the
 same place (deleted.ptr is new.ptr) ?
 Does the garbage collector try to reuse locations or is it the opposite (or 
 random) ? 

Dunno, but the full source code is in dmd zip @
src\druntime\src\gc\basic\gcx.d if you are really that interested.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFKppg7T9LetA9XoXwRApS4AJ0f+SI6MhonTiwLqC6LPD1lq4GzUACfU/oC
TY+3D6isjNKUt9zVxgoSDJ4=
=foxA
-END PGP SIGNATURE-


Re: delegate !is null

2009-09-08 Thread Steven Schveighoffer

On Tue, 08 Sep 2009 12:40:13 -0400, Saaa em...@needmail.com wrote:

Hm... I'm still confused.  Why not just set the delegate to null?  Why  
do

you need to have the delegate set to something?

It is for the gui. I give it a list of things to display.
And some of these things don't yet exist or can be deleted at any time.
I'd like it to display the last valid value.


That still doesn't explain why you need to have delegates set to something  
valid, yet have a null pointer value.  Either a) you are trying to subvert  
the type system, or b) you can simply set the whole delegate (function and  
pointer) once you get the class instance.


It's hard for me to say what is the best way to do it, but generally,  
things like this are difficult, and rightfully so.  Having a delegate  
without a valid pointer makes little sense.






There are ways to do it, without having a class instance, but it is  
messy.

how messy? :D


class C
{
  int val = 0;
  int method() {return val;}
}

int delegate() dg;

dg.func = C.method; // note the upper-case C

// dg is now a instanceless delegate to C.method.

dg.ptr = new C;

Now you can call dg() and it will call the C.method function on the newly  
created C object.


Note that this method of manipulating methods does NOT obey inheritance.   
For example:


class B : C
{
  int method() {return 555;}
}

dg.ptr = new B;

dg(); // will return 0, since it calls C's version of method.

I also don't know how well it will work on interfaces.


Kind of related:
If you delete an object and later create a new object, what are the  
chances

they are located on the
same place (deleted.ptr is new.ptr) ?
Does the garbage collector try to reuse locations or is it the opposite  
(or

random) ?


The chances are non-zero :)  I wouldn't depend on this behavior either way  
if I were you.


-Steve


Re: delegate !is null

2009-09-08 Thread Steven Schveighoffer

On Tue, 08 Sep 2009 16:15:49 -0400, Saaa em...@needmail.com wrote:




// dg is now a instanceless delegate to C.method.

dg.ptr = new C;

So, nothing special under the hood, this would also work?
C c= new C;
dg.ptr = c;


Yes, same thing.


I also don't know how well it will work on interfaces.

Very nice :)
Might be useful, thanks.


Just be cautious.  You can get into undefined territory real easily,  
because dg.ptr is a void * (i.e. goodbye type system, I'm on my own!)





Kind of related:
If you delete an object and later create a new object, what are the
chances
they are located on the
same place (deleted.ptr is new.ptr) ?
Does the garbage collector try to reuse locations or is it the opposite
(or
random) ?


The chances are non-zero :)

Are you quite sure there?


very quite.


I'm only asking a single 'new'.
I mean, there could be some heuristic which would prevent the a new  
object

to take the place of the latest deleted one.


There isn't.  Memory freed by the GC is able to be used in another  
allocation.  If this didn't happen, then it wouldn't take long to use up  
all the memory in the system.  I'm sure if you threw random shit at the GC  
long enough, it would do this :)


-Steve


Re: delegate !is null

2009-09-08 Thread Saaa

 Hm... I'm still confused.  Why not just set the delegate to null?  Why
 do
 you need to have the delegate set to something?
 It is for the gui. I give it a list of things to display.
 And some of these things don't yet exist or can be deleted at any time.
 I'd like it to display the last valid value.

 That still doesn't explain why you need to have delegates set to something 
 valid, yet have a null pointer value.
 Either a) you are trying to subvert  the type system, or b) you can simply 
 set the whole delegate (function and  pointer) once you get the class 
 instance.

 It's hard for me to say what is the best way to do it, but generally, 
 things like this are difficult, and rightfully so.  Having a delegate 
 without a valid pointer makes little sense.



 There are ways to do it, without having a class instance, but it is 
 messy.
 how messy? :D

 class C
 {
   int val = 0;
   int method() {return val;}
 }

 int delegate() dg;

 dg.func = C.method; // note the upper-case C
That's actually quite nice, I didn't know it worked like that :)


 // dg is now a instanceless delegate to C.method.

 dg.ptr = new C;
So, nothing special under the hood, this would also work?
C c= new C;
dg.ptr = c;


 Now you can call dg() and it will call the C.method function on the newly 
 created C object.

 Note that this method of manipulating methods does NOT obey inheritance. 
 For example:

 class B : C
 {
   int method() {return 555;}
 }

 dg.ptr = new B;

 dg(); // will return 0, since it calls C's version of method.

 I also don't know how well it will work on interfaces.
Very nice :)
Might be useful, thanks.


 Kind of related:
 If you delete an object and later create a new object, what are the 
 chances
 they are located on the
 same place (deleted.ptr is new.ptr) ?
 Does the garbage collector try to reuse locations or is it the opposite 
 (or
 random) ?

 The chances are non-zero :)
Are you quite sure there?
I'm only asking a single 'new'.
I mean, there could be some heuristic which would prevent the a new object 
to take the place of the latest deleted one.

 I wouldn't depend on this behavior either way  if I were you.
Yeah you're right, as long as something like this isn't part of the spec it 
wouldn't be safe.
Plus it shouldn't be part of the spec as it would be too stringent on the gc 
implementation in my opinion.
Just curious :)




Re: delegate !is null

2009-09-08 Thread div0
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Saaa wrote:
 The only way I've found so far to do static binding like you are talking
 about is using string mixins.
 I need to rethink stuff a bit, but mixins might be the solution.
 
 My port of Atl's window classes uses a MFC
 like message map:

 mixin messageMap!(
  msgRangeHdlr!(WM_MOUSEFIRST, WM_MOUSELAST, wmMouseEvents),
  msgHdlr!(WM_PAINT, wmPaint),
  msgHdlr!(WM_SIZE, wmSize),
  msgHdlr!(WM_KEYUP, wmKeyUp),
  msgHdlr!(WM_CHAR, wmChar),

  msgHdlr!(WM_SHOWWINDOW, wmShow),
  msgHdlr!(WM_CLOSE, wmClose),
  msgHdlr!(WM_CREATE, wmCreate)
 );

 Having to put the classes method in a string is a bit ugly, but without
 c++ style pointer to members I don't see any other way of doing it.

 If you want to have a look in more detail:

 http://www.sstk.co.uk/atlWinD.php
 The zip 404ed

Typeof. doh. fixed.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFKpuNnT9LetA9XoXwRAuutAKC0q9CzjrH37nOXhg5eJNaWMw1X8wCdFa0P
QNhCcfUJamdDdU2/n6LAg/U=
=B9aH
-END PGP SIGNATURE-


Re: delegate !is null

2009-09-06 Thread Saaa

Steven Schveighoffer schvei...@yahoo.com wrote in message 
news:op.uzqxxo1neav...@localhost.localdomain...
 On Fri, 04 Sep 2009 14:33:12 -0400, Saaa em...@needmail.com wrote:

 class C
 {
   private int i;
   int method()
   {
 return i;
   }
 }

 class D
 {
   private int delegate(void) _deleg;
   this(int delegate(void) d)
   {
 _deleg = d;
   }
   void write()
   {
 if(_deleg !is null)
 }
   writef(_deleg());
 }
   }
 }
 C c = null;
 D d = new d;
 d.function(c.method());
 //This fails, as method is not availible for null.
 d.function({return c.method();});
 //This works but now I can't check whether c is null or not.
 d.write(); //will fail.

 Any suggestions?

 Maybe you could rephrase your question in english.  I can't really 
 understand what you are trying to do with this code.

 i.e. I want to be able to tell whether a delegate is null or not, how do 
 I do that.  But you do that just like you said -- dg !is null.

 -Steve

(unexpected visit this weekend)
Erm, like this.. ?
I'd like to set D's delegate to a method which is not yet available (like 
c.method).
I solved this by encapsulating the method within a function literal, but I 
also need to know whether
the method is available or not when calling the delegate.
I could do this by making the function literal include the null-checking 
code, but is there maybe a better solution to this problem?
The delegate is supposed to change a variable within the D class.
Hope you understand it :)





Re: .dup is null

2009-05-04 Thread Qian Xu
Steven Schveighoffer wrote:

 I think you might have a bug?
 
 .dup is the same as s.dup, not sure why you would expect it to be
 not-null.
 
 -Steve

If I have not explained clearly. 
Here is the full code:

  char[] s;
  assert(s is null);
  assert(s.dup is null);

  assert( !is null); // OK
  assert(.dup !is null); // FAILED

At least the last two lines behave not consistent. 
Either both are failed, or both are passed. 



Re: .dup is null

2009-05-04 Thread Steven Schveighoffer
On Mon, 04 May 2009 10:22:49 -0400, Qian Xu quian...@stud.tu-ilmenau.de  
wrote:



Steven Schveighoffer wrote:


I think you might have a bug?

.dup is the same as s.dup, not sure why you would expect it to be
not-null.

-Steve


If I have not explained clearly.
Here is the full code:

  char[] s;
  assert(s is null);
  assert(s.dup is null);

  assert( !is null); // OK
  assert(.dup !is null); // FAILED

At least the last two lines behave not consistent.
Either both are failed, or both are passed.



OK, your original post was this:

  assert( s.dup  is null); // OK
  assert(.dup !is null); // FAILED


The compiler always returns a null array if you dup an empty array.  The  
reason being: why allocate memory for something that is zero length?


If anything, the oddity is this line:

assert( !is null);

But of course, no memory is allocated for literals, so at least needless  
memory allocation does not occur.


To be consistent, I think assert( is null); should pass, but it's a  
minor inconsistency at best.


I usually never compare arrays to null for this reason...

-Steve


Re: .dup is null

2009-05-04 Thread Steven Schveighoffer

On Mon, 04 May 2009 12:09:09 -0400, Georg Wrede georg.wr...@iki.fi wrote:

If I remember correctly, string literals are stored with a null  
appended, so as to make them easier to use with OS calls, etc. Could it  
be that  stores a 1-byte string, consisting with just this null? Then  
this behavior would be understandable.


Yes, that makes complete sense, thanks.

-Steve


Re: No segfault - null ==

2009-03-31 Thread Unknown W. Brackets

Please remember, strings are not objects.

Therefore, a comparison against null does not cause a segfault, as it 
might with an object.  In the case of arrays, test == null and test 
is null should be the same operation.


In contrast, if you had null == new Object(), you would've seen:

name.d(6): Error: use 'is' instead of '==' when comparing with null

-[Unknown]


Qian Xu wrote:

Hi All,

When I was trying to learn how char-array works, I found something
unexpected.

-- code --
module string_test;

void main()
{
  // test 1
  assert(null == , null is empty); // No segfault

  // test 2
  char[] test; // test = null;
  assert(test is null, undefined_string is null);
  assert(test == , undefined_string (null) is empty);
  assert(test.length == 0, undefined_string.length == 0);

  // test 3
  test = ;
  assert(test !is null, empty_string is NOT null);
  assert(test == , empty_string is empty);
  assert(test.length == 0, empty_string.length == 0);

  // test 4
  test = hello;
  assert(test !is null, non_empty_string is NOT empty);
  assert(test != , non_empty_string is NOT empty);
  assert(test.length  0, non_empty_string.length  0);
}
-- code --

I just wondered, why the first test does not lead to a segfault. Is this an
undocumented compiler feature? I have tested it with gdc and gdmd. Both no
segfault.


--Qian


Re: No segfault - null ==

2009-03-31 Thread Frits van Bommel

Unknown W. Brackets wrote:
In the case of arrays, test == null and test 
is null should be the same operation.


They're not the same operation. (Though there was quite a large debate on these 
newsgroups a while back because a lot of people thought they *should* be, as you 
said)


The difference:
'==', when used on arrays, compares first the length and then all elements (only 
if the lengths are equal, otherwise the result is already known).
Since 'null' contains no elements, only the length gets compared for (null == 
test). Either the other array is also empty and they're therefore equal, or the 
other array isn't and they aren't.
'is', when used on arrays, compares both the length and the pointer. It only 
returns true if both are equal. It never looks at the elements.


So (null == ), but (null !is ).


Re: No segfault - null ==

2009-03-31 Thread Sergey Gromov
Tue, 31 Mar 2009 16:29:30 +0200, Qian Xu wrote:

 When I was trying to learn how char-array works, I found something
 unexpected.
 
 -- code --
 module string_test;
 
 void main()
 {
   // test 1
   assert(null == , null is empty); // No segfault

When you compare null to an array, null is first converted into an empty
array, then a comparison takes place.  This happens because arguments to
comparison operator are first converted to a common type.