0

I am new to scala programming and am facing difficulty to convert from String to Option[String]. I am not sure if the approach I am following is correct or not. Here is the code:

EmpInfo.scala:

package model
case class EmpInfo(empType: Option[String]) extends Named {
  override def name: String = clientName
}

EmpReconciliator.scala

import scalaz._
import Scalaz._
import scala.concurrent.Future
import play.api.mvc.Action
import play.api.mvc.AnyContent
import model.EmpInfo

trait EmpReconciliator[A] extends BaseController[A] {

  val oldName  = "abc"
  val newName = "xyz"
  def reconcileGeneric(genericResource: A) = {
    (genericResource match {         
      case empInfo : EmpInfo => reconcileEmpInfo(empInfo)
      case other@_ => other
    }).asInstanceOf[A]
  }

  def reconcileEmpInfo(empInfo: EmpInfo) = {
    empInfo.empTypeType match {
      case Some(oldName) => empInfo.copy(empType = Some(newName))
      case _ => empInfo
    }
  }
}

Here, I need to replace all instances of oldName with newName. But instead, I am getting "1" in the response. I cannot change type of empType: Option[String] to String (need it that way). I know it's a very minor change needed but cannot place what. Please help.

Jasper-M
  • 14,966
  • 2
  • 26
  • 37
b1399877
  • 27
  • 2
  • 6
  • 1
    For what input do you get `"1"` as a response? – Cyrille Corpet May 22 '17 at 09:25
  • `empInfo.copy(empType = Some(newName))` never use Some in this case. Use `empInfo.copy(empType = Option(newName))` but and answer is instead of `case Some(oldName) => empInfo.copy(empType = Some(newName))` use `case Some(oldName) => empInfo.empType.map(newName)` – Roman Kazanovskyi Mar 28 '23 at 13:31

4 Answers4

1

I don't understand your question. But if you want to use oldName as a pattern, you have to capitalize it.

val OldName  = "abc"
val newName = "xyz"

empInfo.empType match {
  case Some(OldName) => empInfo.copy(empType = Some(newName))
  case _ => empInfo
}

If that doesn't solve the problem you're having, please state your problem more clearly and precisely.

Jasper-M
  • 14,966
  • 2
  • 26
  • 37
  • 1
    see, also, http://stackoverflow.com/questions/7078022/why-does-pattern-matching-in-scala-not-work-with-variables – Yaneeve May 22 '17 at 10:24
  • Type of `Some("a string")`: `Option[String]`. Get `String` Value of a `Option[String]` type: `str.get` – Seraf Aug 24 '17 at 17:29
0

I think you have to add toString()
here case Some(oldName) => empInfo.copy(empType = Some(newName)).toString() case _ => empInfo

0

Not clear what you want to achieve but maybe this helps you:

def reconcileEmpInfo(empInfo: EmpInfo) : EmpInfo  = {
    EmpInfo(Option(newName))
}

think that you should not update EmpInfo. if you need newName inside just create a new one and use the new one. The garbage collector will clean the old one.

Roman Kazanovskyi
  • 3,370
  • 1
  • 21
  • 22
-1

As I understand, the problem that you are facing is to convert a String type to Option[String] type.

Then try wrapping it like this: Option(<variablename>), as in Option(newvalue) in your example.

Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
Ravi
  • 1