Skip to content

Instantly share code, notes, and snippets.

@anoopelias
Created December 28, 2012 14:37
Show Gist options
  • Save anoopelias/4398362 to your computer and use it in GitHub Desktop.
Save anoopelias/4398362 to your computer and use it in GitHub Desktop.
Is there a name for this kind of assignment? See how 'n' is assigned below.
scala> case class Name(name:String)
defined class Name
scala> val a = Name("Anoop")
a: Name = Name(Anoop)
scala> val Name(n) = a
n: String = Anoop
scala>
@ktoso
Copy link

ktoso commented Dec 28, 2012

I don't think it has a name for the assignment itself, it's just "using the Name extractor".
So an idea would be "Extractor assignment" but that sounds weird IMHO.

@anoopelias
Copy link
Author

Either ways, this is very interesting. I noticed this is possible and got excited!.

@ktoso
Copy link

ktoso commented Dec 28, 2012

:-)

worth mentioning is that it's the same thing which happens here:
a match { case Name(n) => ... }

@anoopelias
Copy link
Author

Right. It makes sense. I was half expecting this to work,

    scala> val x*x = 16
    <console>:21: error: not found: value *
           val x*x = 16
                ^

    scala>

but then, now I know!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment