# Should functions that take AbstractStrings also work for Symbols?

**URL:** <https://discourse.julialang.org/t/should-functions-that-take-abstractstrings-also-work-for-symbols/97687>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [April 20, 2023, 5:28am UTC](https://discourse.julialang.org/t/should-functions-that-take-abstractstrings-also-work-for-symbols/97687 "2023-04-20T05:28:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [April 20, 2023, 5:28am UTC](https://discourse.julialang.org/t/should-functions-that-take-abstractstrings-also-work-for-symbols/97687/1 "2023-04-20T05:28:08Z")

</div>

I often use Symbols for better efficiency in large DataFrames, but sometimes would still like to filter by calling functions on the value of the Symbols like `contains`, `startswith`, `endswith`, etc. or even `length`.

Is there a reason why they shouldn’t also be defined for Symbols? Or should AbstractString be extended to include Symbol, since isn’t a Symbol just an interned String?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 20, 2023, 3:56pm UTC](https://discourse.julialang.org/t/should-functions-that-take-abstractstrings-also-work-for-symbols/97687/2 "2023-04-20T15:56:39Z")

</div>

I think the two are pretty separate concepts, see here for an explanation: [Strings vs symbols in DataFrames.jl column indexing | Blog by Bogumił Kamiński](https://bkamins.github.io/julialang/2022/08/05/symbol.html)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 20, 2023, 8:37pm UTC](https://discourse.julialang.org/t/should-functions-that-take-abstractstrings-also-work-for-symbols/97687/3 "2023-04-20T20:37:28Z")

</div>

I’m wondering if InlineStrings.jl or StaticStrings.jl would be of use here.

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [April 21, 2023, 9:03am UTC](https://discourse.julialang.org/t/should-functions-that-take-abstractstrings-also-work-for-symbols/97687/4 "2023-04-21T09:03:39Z")

</div>

> [@gdalle](#):
>
> I think the two are pretty separate concepts,

I agree that they often have distinct use-cases, but I don’t see any downside to extending the functionality of Symbols to allow operations individual characters of the label. Or am I missing something?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 21, 2023, 9:34am UTC](https://discourse.julialang.org/t/should-functions-that-take-abstractstrings-also-work-for-symbols/97687/5 "2023-04-21T09:34:03Z")

</div>

They are very different indeed, I think an even more helpful discussion is Stefan’s answer on SO from ages ago:

> <https://stackoverflow.com/questions/23480722/what-is-a-symbol-in-julia>

Although I guess one might say your point still stands - who cares whether Symbols are also used to represent expressions, couldn’t we still just treat them as Strings?

I’m not a computer scientist and don’t know much about the internals of Symbol and String implementations, but I do know this much:

```julia
julia> x = :Something
:Something

julia> x[1]
ERROR: MethodError: no method matching getindex(::Symbol, ::Int64)
Stacktrace:
 [1] top-level scope

julia> y = "Something"
"Something"

julia> y[1]
'S': ASCII/Unicode U+0053 (category Lu: Letter, uppercase)

```

Fundamentally, Strings are a collection of characters, while Symbols are not, which is why there isn’t even a `getindex` method for them, which presumably would be the minimum requirement for things like `startswith`. My gut says that enabling this sort of stuff for Symbols would be a pretty massive change to the language and isn’t going to happen.

I also think Mark is right, you are probably looking for InlineStrings (or even something like CategoricalArrays if you have lots of repeated strings).
