8

I know the difference between @+id and @id (see this accepted answer). However, I always get the feeling that I'm doing the job of the AAPT compiler when I write the '+' in @+id.

Is there a reason the resource compiler cannot infer by itself if an identifier must be created or just reused? An underlying hashtable structure would do the job: every resource with the same id go into the same bucket, and if the key does not exist, just create it.

Community
  • 1
  • 1
Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
  • http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android - refer this – N20084753 Oct 23 '13 at 13:20
  • @N20084753 He already mentioned it in the OP. – Piovezan Oct 23 '13 at 13:21
  • @N20084753 That's the link I gave in the first sentence. It explains the difference but does not answer why the '+' cannot be automatically infered by AAPT. – Aurelien Ribon Oct 23 '13 at 13:21
  • @AurélienRibon : sorry forgot the see the link and misinterpreted the question. This is really nice question, but to my understanding the plus symbol tells the XML parser to generate an integer, which along with the ID is used to create a new entry in the R.java file. Without it, the parser will instead look for an existing entry with the same name. Absence of such mapping will trigger the error “No resource found that matches the given name”, why making your parser to check for each ID when you are sure/know that this is the new ID. – N20084753 Oct 23 '13 at 14:16

3 Answers3

1

Probably the compiler would not be able to differentiate between a 'right' and a 'wrong' id. If it finds a new id (i.e. one that is not in the underlying hashtable), it would always assume it to be a right, new id. It would not be able to differentiate between an actual new id and a mistyped id.

Piovezan
  • 3,215
  • 1
  • 28
  • 45
  • Maybe, but the result is that many devs use @+id everywhere, since there is no error if the id is already defined, and everything works just fine. That means the compiler tests if the id already exist, but not if it does not exist, that's crazy. – Aurelien Ribon Oct 23 '13 at 13:26
1

I could imagine it is to help the programmer.

If you did not need the @+id construction then all @id references/constructions would be valid, then it would be difficult to track down an error, as the compiler would not fail on incorrect references (as it would simply construct the typo id).

Put differently, all id reference errors would have to be discovered at runtime.

Edit:

Just noticed the similar answer by Piovezan, regarding your comment:

Maybe, but the result is that many devs use @+id everywhere, since there is no error if the id is already defined, and everything works just fine. That means the compiler tests if the id already exist, but not if it does not exist, that's crazy

Then those developers are misusing the @+id construction imo.

It is still much better to have the option to distinguish between @+id and @id, since (for those who does not misuse the @+id) the compiler has a chance of giving a compile time error on wrong references.

Edit2

And to address the comment:

That's the link I gave in the first sentence. It explains the difference but does not answer why the '+' cannot be automatically infered by AAPT

I believe it can, it just does not due to the argumentation above (i believe).

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • Doing that on purpose to help the devs is indeed a possibility. However, try to drag and drop some UI elements in the eclipse designer, and you'll see that it uses @+id EVERYWHERE! Since it's an official Android tool, I find that behavior strange, since most new users will use that designer, and look at the code to learn. – Aurelien Ribon Oct 23 '13 at 14:18
  • I can see your point but as it is a tool, it will generate valid references for sure and may have a reason to use the @+id (simpler for them I guess). Bottom line, they do not need the compile time help that @id brings. – cYrixmorten Oct 23 '13 at 14:57
0

with @+id you are adding id to R.java which allows you to reference it from Java classes while with @id you are not. You can use @id only if specific id is already created and you are referencing it in another view e.g. android:layout_below="@id/some_id"

kjurkovic
  • 4,044
  • 2
  • 23
  • 28
  • 1
    Thanks, but as I said, I know all of that. The question is why the plus-symbol is necessary. It should be super easy for the compiler to know if the identifier already exist or not, so I do not understand why I must give it this piece of information. – Aurelien Ribon Oct 23 '13 at 13:22