convert String to Option[String]












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.










share|improve this question




















  • 1





    For what input do you get "1" as a response?

    – Cyrille Corpet
    May 22 '17 at 9:25
















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.










share|improve this question




















  • 1





    For what input do you get "1" as a response?

    – Cyrille Corpet
    May 22 '17 at 9:25














0












0








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.










share|improve this question
















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.







scala playframework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 22 '17 at 9:24









Jasper-M

9,09211329




9,09211329










asked May 22 '17 at 8:57









b1399877b1399877

125




125








  • 1





    For what input do you get "1" as a response?

    – Cyrille Corpet
    May 22 '17 at 9:25














  • 1





    For what input do you get "1" as a response?

    – Cyrille Corpet
    May 22 '17 at 9:25








1




1





For what input do you get "1" as a response?

– Cyrille Corpet
May 22 '17 at 9:25





For what input do you get "1" as a response?

– Cyrille Corpet
May 22 '17 at 9:25












3 Answers
3






active

oldest

votes


















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.






share|improve this answer





















  • 1





    see, also, stackoverflow.com/questions/7078022/…

    – 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






share|improve this answer































    -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.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44108671%2fconvert-string-to-optionstring%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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.






      share|improve this answer





















      • 1





        see, also, stackoverflow.com/questions/7078022/…

        – 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


















      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.






      share|improve this answer





















      • 1





        see, also, stackoverflow.com/questions/7078022/…

        – 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
















      1












      1








      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.






      share|improve this answer















      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 22 '17 at 9:35

























      answered May 22 '17 at 9:30









      Jasper-MJasper-M

      9,09211329




      9,09211329








      • 1





        see, also, stackoverflow.com/questions/7078022/…

        – 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
















      • 1





        see, also, stackoverflow.com/questions/7078022/…

        – 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










      1




      1





      see, also, stackoverflow.com/questions/7078022/…

      – Yaneeve
      May 22 '17 at 10:24





      see, also, stackoverflow.com/questions/7078022/…

      – 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







      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






      share|improve this answer




























        0














        I think you have to add toString()

        here
        case Some(oldName) => empInfo.copy(empType =
        Some(newName)).toString()
        case _ => empInfo






        share|improve this answer


























          0












          0








          0







          I think you have to add toString()

          here
          case Some(oldName) => empInfo.copy(empType =
          Some(newName)).toString()
          case _ => empInfo






          share|improve this answer













          I think you have to add toString()

          here
          case Some(oldName) => empInfo.copy(empType =
          Some(newName)).toString()
          case _ => empInfo







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 22 '17 at 9:02









          manikandanmanikandan

          12




          12























              -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.






              share|improve this answer






























                -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.






                share|improve this answer




























                  -1












                  -1








                  -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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 17:00









                  Alonso Dominguez

                  6,28311936




                  6,28311936










                  answered Nov 23 '18 at 12:38









                  RaviRavi

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44108671%2fconvert-string-to-optionstring%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin