News for hackers. Stuff that matters.
Re:String.intern() by Terr_ on Monday May 15, 2017 @09:10PM (#14346644)
Every time I see String.intern() my mind leaps to the problem of new Java programmers who are misled into this:

    String a = "hello";
    String b = "world";
    assert a != b;
    b = "hello";
    assert a == b;
    // OH NICE I'LL USE == FOR STRING COMPARISONS NOW
It works cause source-code literals are intern'ed down into identical objects by the compiler, but that's a special case that won't apply to strings created at runtime.
Re:String.intern() by userbinator on Monday May 15, 2017 @10:00PM (#14346837)
new Java programmers

Or perhaps interns? ;-P

In fact, I've wondered about the origins of the term "intern" since I first heard of it; the technique was already known to me and likely others as "tokenising" before that, however, since compilers would often have to compare identifiers and doing this to them made it much faster.

Re:String.intern() by lispm on Tuesday May 16, 2017 @02:52AM (#14347836)
'intern'ing in the Lisp world usually means to create a new object and put it under its name into some datastructure for later retrieval, unless the object already exists -> then the object is just retrieved.

The typical example is to intern a symbol into a symbol table (list, array, or in Common Lisp into a package). Here the symbol table is searched for a symbol with the required name. If it exists, the symbol will be returned. If it does not exist, the symbol will be created and registered in the symbol table.

But one might also use the word 'intern' for an operation to retrieve/put a user into a group, retrieve/put a resource into a pool, or similar operations.

The function INTERN dates back at least to Lisp 1.5 from 1962. See the Lisp 1.5 Programmer's Manual.

Re:String.intern() by TeMPOraL on Tuesday May 16, 2017 @04:14AM (#14348122)
It's from Lisp, and where did Lisp take it from in the sixties, I don't know. But I'll bet that this particular Java method comes straight from Lisp, as there were some Lisp folks involved in making Java (in particular, Guy Steele worked on both Common Lisp ANSI standard and Java language specification).
Re:String.intern() by pjmlp on Tuesday May 16, 2017 @07:00AM (#14348683)
And was one of the Scheme creators.
Re:String.intern() by wtetzner on Monday May 15, 2017 @10:21PM (#14346928)
> In fact, I've wondered about the origins of the term "intern" since I first heard of it

I always assumed it came from internalize, or something like that.

Re:String.intern() by derefr on Monday May 15, 2017 @11:14PM (#14347117)
I was curious, but a good amount of Googling didn't turn up any etymology.

I'll take a guess, though, that it's from Common Lisp, because CL's intern function (http://clhs.lisp.se/Body/f_intern.htm) is clearly part of a suite of names (intern, unintern, :internal, :external) where, in context, "intern" makes complete sense as the name for the function.

Re:String.intern() by smarks on Monday May 15, 2017 @11:36PM (#14347193)
Usage of "intern" in this sense is much older than Common Lisp. The earliest reference I could find in a few minutes of searching is from 1974, from the MACLISP Reference Manual:

http://www.softwarepreservation.org/projects/LISP/MIT/Moon-M...

See sections 6.3 and 6.4 and the glossary for definitions and uses of the word.

Unfortunately they don't shed any light on where the term "intern" comes from. Typical usage is that a string is «"interned" or registered in an obarray» but it doesn't explain much more than that. Interning occurs most often at read time, when a string read from the input is turned into an atom, so I'd guess that "intern" is short for "internalize" or similar.

Re:String.intern() by derefr on Monday May 15, 2017 @11:51PM (#14347256)
Well, I was about to say, the Common Lisp version makes enough sense (and the MacLisp version might be equivalent, for all I know):

In CL, you don't really have a global "symbol table" or "atom table"; instead, interning is about the runtime lexical scopes under which code compilation happens, which consist of stacks of imported namespaces ("packages"), where each namespace exists as a mutable object holding identifier "slots".

(intern foo) is a mutation on the current namespace that creates a local variable "slot" for the identifier foo, shadowing any other foo that might have been in lexical scope from an import. That is, (intern foo) makes foo resolve internally to the current namespace.

(unintern foo) is the complementary mutation; it drops the foo identifier from the current namespace, allowing other symbols (slots in packages) that were previously shadowed by it to re-appear.

Re:String.intern() by jsmthrowaway on Tuesday May 16, 2017 @01:40AM (#14347607)
Internment, perhaps?
Re:String.intern() by brudgers on Monday May 15, 2017 @11:38PM (#14347204)
OED:

†1. intr. To enter or pass in; to be come incorporated or united with another being. Obs.

2. trans. To confine within the limits of a country, district, or place; to oblige to reside within prescribed limits without permission to leave them.

Re:String.intern() by tyingq on Monday May 15, 2017 @11:03PM (#14347080)
I believe the original term was for recent medical grads working at a hospital before they had their license. It was "okay" because they were internal, and supervised. Once they had the license, they could work unsupervised or externally.
Re:String.intern() by josteink on Tuesday May 16, 2017 @01:32AM (#14347579)
> In fact, I've wondered about the origins of the term "intern" since I first heard of it; the technique was already known to me and likely others as "tokenising" before that, however, since compilers would often have to compare identifiers and doing this to them made it much faster.

My first hearing of it was in conjunction with LISP macros, or more specifically creating hygenic LISP macros. I'm pretty sure it goes way back.

You can see an example of this here: http://stackoverflow.com/a/30016344

Re:String.intern() by cousin_it on Tuesday May 16, 2017 @04:03AM (#14348066)
I think the problem is that == compares pointers, when it should've been sugar for .equals() instead.
Re:String.intern() by logicallee on Tuesday May 16, 2017 @06:12AM (#14348512)
it's tough. If a language has pointers, then you would expect == to compare whether the two sides have the same value (an address). I don't see a way around that.

How would you expect a programmer to ask "do these two pointers hold the same address?" .holdsSameAddressAs()?

Re:String.intern() by tveita on Tuesday May 16, 2017 @06:32AM (#14348573)
D and Python use the "is" operator to compare reference identity. It is the lesser used operator and the more advanced concept, so using == for reference comparison is just a newbie trap, doubly so since you have to use == to compare equality for primitives.
Re:String.intern() by logicallee on Tuesday May 16, 2017 @06:50AM (#14348640)
But it's not true. Mary is a sign-holder. She's holding a sign that says "32 Crestview Road". John is also a sign-holder. Today his sign also says "32 Crestview Road".

Question: is Mary John?

You want the answer to be "yes" (Mary is John) since the is operator asks whether they hold addresses which have the same value.

But of course it's a broken terminology, because Mary and John are two different people, with their own physical locations etc. So you're overloading a word "is". I get that it works, but so does anything else you make programmers learn.

Re:String.intern() by cousin_it on Tuesday May 16, 2017 @07:05AM (#14348700)
I think Haskell gets it right. All values are immutable. Calling == for two values of some type invokes the Eq implementation for that type, e.g. for strings it's string equality. For the type "IORef a" (box that can contain different a's at different points in time) it compares the identity of the box, not the contents. In fact it can't compare contents, because the type of == stops it from using impure operations like accessing current contents.

The same approach could work in imperative or OO languages. For immutable types == should be value equality, while mutable types are analogous to IORef or IOArray, so == should be reference equality. It comes down to observational equality, different instances of the number 2 shouldn't be distinguishable, but different mutable containers with the same contents should be distinguishable because you can make their contents different. I think observational equality is a good fit for pretty much all uses of equality in programming.

Re:String.intern() by Larrikin on Tuesday May 16, 2017 @12:42PM (#14350985)
But in the ideal situation it would be

    assertTrue(mary.getSign == john.getSign)
    assertFalse(mary.getSign is john.getSign)
    assertFalse(mary == john)
    assertFalse(mary is john)
Re:String.intern() by logicallee on Tuesday May 16, 2017 @02:50PM (#14352210)
right, you and I are agreed and your third sentence is exactly what I was arguing for - it goes against the OP's request that that third sentence evaluate to true.

this was the OP in this thread:

https://news.ycombinator.com/item?id=14348066

Re:String.intern() by styfle on Tuesday May 16, 2017 @08:32AM (#14349029)
> How would you expect a programmer to ask "do these two pointers hold the same address?"

In Java and C#, you rarely need to ask this question.

In fact, the .NET Framework overloads String, DateTime[0], etc so you can use == for almost all comparisons.

[0]: http://stackoverflow.com/a/1205462/266535

Re:String.intern() by ben-schaaf on Tuesday May 16, 2017 @06:45AM (#14348613)
Except java doesn't have pointers, it has references. You can't do pointer arithmetic on references, nor can you take addresses and dereference.

I'd expect a programmer to ask: "is this object the same as this other object" and have the language support that by an operator like `is`. Instead of overloading `==` to mean value and reference equality depending on the context.

Re:String.intern() by gpderetta on Tuesday May 16, 2017 @06:58AM (#14348672)
java references are pointers; the fact that you can't do math on them is immaterial. In java, pointers and primitive types are first class (can be assigned, compared, passed and returned from functions, be members of other objects) while the object themselves are not (they only live in the heap and can't be aggregated).
Re:String.intern() by ajanuary on Tuesday May 16, 2017 @01:35PM (#14351520)
I think the distinction between "this value is a reference that can be derefenced to get another value that is stored somewhere else" and "this value is an address into some contiguous storage where another value may be stored" is a useful one. Personally I like "reference" and "pointer" to make that distinction, where a pointer is a specific implementation of a reference.

However, that terminology is by no means universal. While, from what I can tell, "reference" as defined above is favoured in the Java and C# community, "pointer" is still used plenty. For example, the Java spec says "The reference values (often just references) are pointers to these objects"

All that being said, I do agree with what I think is the spirit of your comment, which is that when your language doesn't allow pointer manipulation it is less useful to ask whether two values point to/reference the same value.

Re:String.intern() by _pmf_ on Tuesday May 16, 2017 @08:16AM (#14348942)
> How would you expect a programmer to ask "do these two pointers hold the same address?" .holdsSameAddressAs()?

In C++, yes, you do

    (&a == &b)
and the plain == is used for the common case of actually doing what you mean (what in Java would be .equals()).
Re:String.intern() by sherincall on Wednesday May 17, 2017 @03:42AM (#14356385)
Actually, that's not always correct in C++ (it is in C). It is possible that either type has the operator& overloaded, so instead the proper way to do it is:

    (std::addressof(a) == std::addressof(b))
http://en.cppreference.com/w/cpp/memory/addressof
Re:String.intern() by _pmf_ on Wednesday May 17, 2017 @05:23AM (#14356814)
Thanks (I didn't even know this operator was overloadable)!
Re:String.intern() by HappyTypist on Tuesday May 16, 2017 @06:18AM (#14348534)
==& ?

I use .equals far more than ==.

Re:String.intern() by swsieber on Monday May 15, 2017 @11:24PM (#14347152)
I ran into this once when I accidentally used == for a comparison to an empty string... and the condition went through part of the time because sometimes the empty string it was being compared through was also a constant....
Re:String.intern() by bbcbasic on Tuesday May 16, 2017 @12:29AM (#14347367)
new Java programmers, especially those coming from C# where == compares the strings.