String variables can have content, be empty, or be null
It is important to understand that the value
null represents the absence
of an object reference and it is distinct from the empty String value "". The two are not interchangeable
values. A variable declared to type String
can hold the value null,
the empty String (""), or a non-empty String.
To test for a populated String object versus a null or empty String object, use the HasContent method. When you combine
it with the null-tolerant property access in Gosu, HasContent returns false if either the value is null or an empty String object.
Compare the behavior of properties HasContent and Empty:
var s1 : String = null
var s2 : String = ""
var s3 : String = "hello"
print("s1 has content = " + s1.HasContent)
print("s2 has content = " + s2.HasContent)
print("s3 has content = " + s3.HasContent)
print("s1 is empty = " + s1.Empty)
print("s2 is empty = " + s2.Empty)
print("s3 is empty = " + s3.Empty)
This code prints:
s1 has content = false
s2 has content = false
s3 has content = true
s1 is empty = false
s2 is empty = true
s3 is empty = false
If the variable holds an empty String or null, the HasContent method returns false. Using the HasContent method is more intuitive
than using the Empty property
in typical cases where null
represents absence of data.
