A Story of ‘Symbol

A Story of ‘Symbol

A somewhat secret and powerful construct is Scala’s Symbol class. Symbols are much alike Strings but have a few key differences. Let’s start with instantiation.

I want one. Gimme, Gimme, Gimme!

You want a Symbol, but you don’t know how. Well, there are two ways. It’s deprecated, and no longer supported in Scala 3, but you can use the Symbol literal syntax, like this:

scala> 'symbol
res0: Symbol = 'symbol        

Why do we have this? It’s short. That’s really it. It’s limited in the characters you can use, and it can’t be instantiated dynamically. How can we do this more idiomatically? Good ol' class construction:

scala> Symbol("A Symbol w/ Spaces :)")
res1: Symbol = 'A Symbol w/ Spaces :)        

Now, we have our Symbols.

I Thought Strings were Enough?

They are, in most cases, but Symbols are special. We need to talk about String interning. String interning is designed to help you save memory if have many equivalent Strings in your application

If you intern a String, it will be placed in the intern pool on the JVM where it. A reference to that String will be used instead of a new instance every time it’s called. This is particularly useful for fast equality checking, as it only needs to check if the references are equal. Note: String interning always occurs for compile-time Strings.

That brings us back to Symbols. Symbols are interned are Strings. The difference between Symbols and normal Strings is that Symbols have faster equality checking.

But, when is it Worth It?

Whenever you’re comparing the same set of Strings over and over again, for example, a map with String keys.

Perhaps instead of:

Map[String, Any]        

It might be time for:

Map[Symbol, Any]        

There are lots of use-cases. Keep an eye out, but not before reading the next section.

When not to use Symbol:

Symbols do come with disadvantages. For one, interning is an expensive process, so only utilize it when you really have to. Secondly, interned Strings live in the PermGen space. If you’re not careful you can run out of memory, even if you have plenty of heap space.

Be careful, be smart, and use Symbols when they improve the quality of your programs. Now, go build something awesome.

Regards,

Bradly O

Bradly Ovitt

Functional Scala Programmer

2 年

Make sure to follow The Scala Developer.

回复

要查看或添加评论,请登录

Bradly Ovitt的更多文章

  • Concerns of Domain Driven Design

    Concerns of Domain Driven Design

    Let's talk about what problems domain driven design solves. Domain driven design is a methodology to model several…

  • The Actor Model & Reactive Systems

    The Actor Model & Reactive Systems

    Let's talk about the actor model and building reactive systems. The actor model is a programming paradigm for building…

    1 条评论
  • Concerns of Reactive Systems

    Concerns of Reactive Systems

    Let's talk about why we build reactive systems, as software engineers/developers. As professionals, we have to balance…

    1 条评论
  • Software Transactional Memory

    Software Transactional Memory

    Software transactional memory (STM) is a programming paradigm that allows multiple threads to share access to shared…

  • Understanding the Elm Architecture

    Understanding the Elm Architecture

    Elm is a functional programming language that is well-known for its robust architecture and its ability to build highly…

  • Binding Propagation in Prolog

    Binding Propagation in Prolog

    Prolog is a programming language that is well known for its ability to perform logical reasoning and search through…

  • Effect Rotation in ZIO

    Effect Rotation in ZIO

    ZIO is a popular functional programming library in Scala that has gained a lot of traction in recent years. One of the…

  • What Scala Can't Do

    What Scala Can't Do

    Today, Scala is a programming language that can use any Javascript library, any Java library, and any C/C++ library…

  • An Introduction to Shardcake

    An Introduction to Shardcake

    I recently found myself developing backend microservices with Scala and ZIO. I'm using ZIO-gRPC to implement the…

  • Transparency in Distributed Systems

    Transparency in Distributed Systems

    One of the most important theoretical goals of distributed systems is hiding the fact that resources and processes are…

社区洞察

其他会员也浏览了