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

Daniel Kozák via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 9 05:06:35 PDT 2015


On Thu, 09 Apr 2015 11:45:30 +0000
tcak via Digitalmars-d-learn <digitalmars-d-learn at 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 );
}


More information about the Digitalmars-d-learn mailing list