www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Parameter is null by default. No value is given. Code says it is not

reply "tcak" <tcak gmail.com> writes:
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?
Apr 09 2015
next sibling parent "tcak" <tcak gmail.com> writes:
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.
Apr 09 2015
prev sibling next sibling parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Thu, 09 Apr 2015 11:45:30 +0000
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 ); }
Apr 09 2015
parent "tcak" <tcak gmail.com> writes:
On Thursday, 9 April 2015 at 12:06:49 UTC, Daniel Kozák wrote:
 On Thu, 09 Apr 2015 11:45:30 +0000
 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" ); }
Apr 09 2015
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Apr 09 2015
next sibling parent reply "tcak" <tcak gmail.com> writes:
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.
Apr 09 2015
parent reply "tcak" <tcak gmail.com> writes:
By the way, I am using "DMD64 D Compiler v2.067.0" on Ubuntu 
14.04.
Apr 09 2015
parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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.
Apr 09 2015
parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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
Apr 09 2015
parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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
Apr 09 2015
parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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
Apr 09 2015
parent "tcak" <tcak gmail.com> writes:
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
Apr 09 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Apr 09 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Apr 09 2015
prev sibling next sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
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 :(
Apr 09 2015
prev sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
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
Apr 09 2015