Skip to content

Commit

Permalink
Added more detail to Iterable<> bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuckJonas authored Oct 10, 2019
1 parent 0eb3a62 commit 6ea0fe2
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,34 @@ Worth noting that Java won't even allow you to declare a block scoped variable i

Source: [Kevin Jones](https://twitter.com/nawforce/status/1180936132491657224)

### Phantom Interface
### Broken type inference for `Iterable<>`

A Set can be iterated in a for loop:
Lets look at the standard `Set` class for example...

It can be iterated in a foreach loop:

``` java
Set<String> mySet = new Set<String>{'a', 'b'};
for(String s : mySet){}
```

But it doesn't implement the Iterable interface:
But, according to Salesforce (compiler & runtime), it does not actually implement the `Iterable` interface:

``` java
String.join(mySet, ',');
/// fails because Set doesn't implement Iterable
String.join((Iterable<String>) mySet, ',');
/// or does it? This cast succeeds!
String.join(mySet, ','); // Doesn't compile! "Method does not exist or incorrect signature: void join(Set<String>, String)..."

// Just to make sure, lets check at runtime..
System.assert(mySet instanceof Iterable<String>); // Yup, this fails! I guess Set really isn't an Iterable...
```

Except... It actually does:

``` java
String.join((Iterable<String>) mySet, ','); // this works!?
```

You'll find the same behavior with "Custom Iterables".

### Fun with Hashcodes

[Enums in batch](https://salesforce.stackexchange.com/questions/158557/enums-as-map-keys-dont-work-in-batchable)
Expand Down

0 comments on commit 6ea0fe2

Please sign in to comment.