Strings in Gosu

723 views
Skip to first unread message

Hertz

unread,
Aug 6, 2014, 12:44:28 PM8/6/14
to gosu...@googlegroups.com
I have a few questions about strings in Gosu. Using the program below as a guide here are my questions

Does strA and strB reference the same object?

Does the == operator on strings find out if the values are equal or if they reference the same object?


var strA = "gosu"
var strB = "gosu"
var strC = strA
var strD = strB

print( strA == strB ) //True
print( strA == strD ) //Since D is equal to B, and A == B is true, it should be true

print( strB == strC ) //Since C is A, and A == B, then it should be true
print( strC == strD ) //Since C is A, and D is B, and A == B, then it C == D is true

Alan Keefer

unread,
Aug 6, 2014, 1:02:17 PM8/6/14
to gosu...@googlegroups.com
Gosu String constants work like Java String constants:  as an implementation detail, it happens that two identical String constants will actually be the same reference since the Strings are part of the constant pool, but that's an implementation detail that shouldn't be relied upon for program correctness. That means that not all Strings with the same value will be the same object: var x = "go" + "su" will not be the same object as var y = "gosu" since in this case x has been heap allocated and is not coming from the constant pool.

In Gosu, the == operator compares value equality (equivalent to a null-safe call to .equals()), while the === operator compares reference equality.  So in the example I gave, x == y will be true, while x === y will be false.

In your example, all four variables will return true for both == and ===.

-Alan


--
You received this message because you are subscribed to the Google Groups "gosu-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gosu-lang+...@googlegroups.com.
To post to this group, send email to gosu...@googlegroups.com.
Visit this group at http://groups.google.com/group/gosu-lang.
For more options, visit https://groups.google.com/d/optout.

Hertz

unread,
Aug 6, 2014, 1:33:44 PM8/6/14
to gosu...@googlegroups.com
Thanks for the clarification.

Hertz

unread,
Aug 7, 2014, 12:12:33 PM8/7/14
to gosu...@googlegroups.com
I also have a question about the size and length properties of Strings. At first I thought the size property returned the memory amount size of the string and the length returned the actual character amount until I looked at the source for the two and found out they both return the same thing( length as in the amount of characters. )

Why are there to properties that give you the same value? Is is a functional reason, language design, legacy code?

Carson Gross

unread,
Aug 7, 2014, 12:49:20 PM8/7/14
to gosu...@googlegroups.com
I think the idea was to standardize a single property name across Strings and Collections.

Cheers,
Carson

On Thursday, August 7, 2014 at 9:12 AM, Hertz wrote:

I also have a question about the size and length properties of Strings. At first I thought the size property returned the memory amount size of the string and the length returned the actual character amount until I looked at the source for the two and found out they both return the same thing( length as in the amount of characters. )

Why are there to properties that give you the same value? Is is a functional reason, language design, legacy code?

--
Reply all
Reply to author
Forward
0 new messages