Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there in scala the equivalent of the c# nameof?

Tags:

c#

scala

In c# I can write code such as this (I ran it Linqpad 5):

void Main()
{
    new Test { Property1 = 100 }.Dump();
    Console.WriteLine(nameof(Test.Property1));
}

class Test
{
    public int Property1 { get; set; }
    override public String ToString()
    {
        return $"{nameof(Property1)}={Property1}";
    }
}

Is there a scala equivalent of nameof?

Thanks

like image 621
costa Avatar asked Mar 09 '23 17:03

costa


2 Answers

In Scala it can be done by a (macro) library instead of needing special language-level support. There is one at https://github.com/dwickern/scala-nameof.

like image 75
Alexey Romanov Avatar answered Mar 11 '23 06:03

Alexey Romanov


There is no any similar methods in scala as of C#, If you really want the function than you can use scala-reflection.

like image 30
koiralo Avatar answered Mar 11 '23 06:03

koiralo