Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:44:55+00:00 2026-06-02T00:44:55+00:00

UPDATE All the answers are good here, but @senia’s does so the most directly,

  • 0

UPDATE
All the answers are good here, but @senia’s does so the most directly, without need for additional steps. Will this lead to bugs, possibly, but when using Map[Symbol, T] convention in hundreds of methods, a 1-step implicit conversion prior to map creation is preferred (avoids Symbol Map key permgen storage). At any rate, here’s the pimp:

class SymbolProvidesPair(i: Symbol) { def ->[T](s: T) = (i.toString.tail, s) }
@inline implicit def symbol2String(i: Symbol) = new SymbolProvidesPair(i)

Original
It bothers me a bit using string keys in Maps, just slows me down and is, IMO, not as syntactically easy on the eyes as symbol keys.

val map: Map[String, Int] = Map("strings" -> 1, "blow" -> 2)
val map: Map[String, Int] = Map('symbols -> 1, 'rock -> 2)

So, I created an implicit to scratch my itch:

implicit def symbolKey2String[A <: Symbol, B](x:(A,B)) = (x._1.toString, x._2)

Couple things:
1) is this the correct signature? The above works, but A <: Symbol I take to mean, something that derives from Symbol vs. something that equals Symbol.

2) I’ll be using this when I manually type out Maps; i.e. just for convenience. Am I going to hit any snags with this implicit? It seems edge case enough to not cause issues (like string2Int, for example), but not sure if I’m missing something.

Thanks

EDIT
Ok, well #1 I can just actually say what I mean, [Symbol, B] instead of [A <: Symbol, B]

But now I find myself with another issue, the symbol-to-string implicit boxes me into a corner of sorts as I then have to explicitly define Map[String, Type] for all new Maps (i.e. lose the nice compiler type inference) in order to be able to use symbol keys.

How then to get the best of both worlds, Map symbol keys, but with inferred [String, Type] when not specifying the type signature? i.e. have the compiler infer Map[String, Int] when I do:

val map = Map('foo -> 1)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:44:56+00:00Added an answer on June 2, 2026 at 12:44 am

    You don’t need to specify map’s type explicitly:

    scala> class SymbolToPait(i: Symbol) { def ->[T](s: T) = (i.toString().tail, s)}
    defined class SymbolToPait
    
    scala> implicit def symbolToPair(i: Symbol) = new SymbolToPait(i)
    symbolToPair: (i: Symbol)SymbolToPait
    
    scala> 'Symbol -> "String"
    res0: (String, String) = (Symbol,String)
    
    scala> Map('Symbol -> "String")
    res1: scala.collection.immutable.Map[String,String] = Map(Symbol -> String)
    
    scala> Map('Symbol -> 1)
    res2: scala.collection.immutable.Map[String,Int] = Map(Symbol -> 1)
    

    This kind of behavior could surprise other developers. Maybe it would be better to replace -> with some other word? For example :-> or ~>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Brief description of requirements (Lots of good answers here, thanks to all, I'll update
I have a table that I need to update where all columns are optionally
Update: Oh good grief, it was all a red herring following a bad merge.
Yes, this is one more question about good old div inside td but without
I'm trying to calculate all the correct answer a user gets and update it
i want to update all records in my database(mongodb),I tried to use command below
I am trying to update all records in a column so that they start
I'd like to update all records with an empty string in all columns of
What I want: Update all new commits from server with my local repository in
I just pulled https://github.com/kolanos/kohana-universe and now I'm trying to update all the modules running:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.