# Last function

**URL:** https://discourse.julialang.org/t/last-function/86741
**Category:** General Usage
**Created:** [September 3, 2022, 7:50pm UTC](https://discourse.julialang.org/t/last-function/86741 "2022-09-03T19:50:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![reewa\_malik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reewa_malik/32/37743_2.png) [@reewa\_malik](https://discourse.julialang.org/u/reewa_malik)
#### Post date: [September 3, 2022, 7:50pm UTC](https://discourse.julialang.org/t/last-function/86741/1 "2022-09-03T19:50:53Z")

</div>

x=“abcd”  
last(x)==“d”  
This is giving me false as output, can anybody explain why??

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [September 3, 2022, 7:59pm UTC](https://discourse.julialang.org/t/last-function/86741/2 "2022-09-03T19:59:35Z")

</div>

Unfortunately `==` allows comparing objects with different types, so you are accidentally comparing the Char `'d'` to the String `"d"`.

```nohighlight
julia> last("abcd") == 'd'
true

```

You can get the string version by passing `1` to `last`.

```julia
julia> last("abcd",1) == "d"
true

```

---

<div class="post-metadata">

### Author: ![reewa\_malik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reewa_malik/32/37743_2.png) [@reewa\_malik](https://discourse.julialang.org/u/reewa_malik)
#### Post date: [September 3, 2022, 8:00pm UTC](https://discourse.julialang.org/t/last-function/86741/3 "2022-09-03T20:00:32Z")

</div>

Thanks 🙂

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 3, 2022, 8:02pm UTC](https://discourse.julialang.org/t/last-function/86741/4 "2022-09-03T20:02:36Z")

</div>

> [@jar1](#):
>
> Unfortunately `==` allows comparing objects with different types

To explain why this happens: `==` falls back to `===` for comparisons, which has a default fallback of `false` for objects of different types (they’re differently typed objects after all and as such cannot be the exact same thing).

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [September 3, 2022, 11:21pm UTC](https://discourse.julialang.org/t/last-function/86741/5 "2022-09-03T23:21:20Z")

</div>

Is it a mistake to define it as such? Should it be defined as:

```julia
julia> import Base.==
julia> ==(C::AbstractChar, S::AbstractString) = S == string(C)
julia> ==(S::AbstractString, C::AbstractChar) = S == string(C)

julia> last(x)=="d"
true

And note, in contrast, this will allocate for the String returned:
julia> @time last("abcd",1) == "d"
  0.000004 seconds (1 allocation: 32 bytes)
true

```

I thought my version would also allocate because of `string`, and I was going to amend it, but actually it doesn’t, at least on 1.7.3. I guess the compiler is just clever.
