Il_totore
Il_totore

Reputation: 133

Creating pseudo-assignement using Scala macro

I'm trying to transform this:

@Property("test")  
private var testOpt: Option[String] = Option.empty

into

private var testOpt: Option[String] = Option.empty

def test: Option[String] = testOpt
def test_=(value: Option[String]) = testOpt = test

Using this code: https://hasteb.in/rawulipe.scala

Everything work fine except when using the _= suffix, getting

value test_= is not a member of <the class of the testOpt variable>

Work fine when adding this method manually. Any idea ?

Upvotes: 0

Views: 57

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51658

Replace

val setter = TermName(s"${defName}_=")

with

val setter = TermName(s"${defName}_=").encodedName.toTermName

or

val setter = TermName(s"${defName}_$$eq")

Usage:

@Property("test")
private var testOpt: Option[String] = Option.empty
  
//scalac: {
//  private var testOpt: Option[String] = Option.empty;
//  def test: Option[String] = testOpt;
//  def test_$eq(value: Option[String]): Unit = testOpt = value;
//  ()
//}

test = Some("a") // compiles

scala AST Select node can't find members inherited from parent

Upvotes: 2

Related Questions