0

I'm trying to convert a generic string to a number using scala

object h extends App {
  def castTo[T](s: String): T = {
    s.asInstanceOf[T]
  }


  print(castTo[Int]("20"))
  print(castTo[Double]("20.1"))
}

the data:

name | value a | "1" b | "2.123" c | "abd"

the usecase: riight now I'm exporting the data to the user a method for each conversion. getNameAsDouble, getNameAsInteger and so forth. I wish to do getNameT to save lot's of code and make it a bit more pretty and easy to read the doc.

so, in case a programmer does : getNameInt i want the program to print in this case: 1 getNameDouble i want the program to print in this case: 2.123

in cpp i could use dynamic_cast. there a way to do so in scala? ( i also tried to do so in java but couldn't find a way)

p.s. i've tried something like this, but i wandered if there is more generic way.

castTo[T] (s:String): T = {
...
case T instance of Integer => s.toInt
case T instance of Long => s.toLong
...
}
4
  • 3
    What you want to do is to parse the String into a generic Number, not to cast it. BTW, why do you want to do this? String already provides the toInt, toDouble & etc methods, why do you need to encapsulate all of them on a single method? Commented Dec 10, 2019 at 14:11
  • i'm exporiting an API to get data from source like this: name | value a | "1" b | "2.123" c | "abd" so the user of the api would do getName[Int](name) Commented Dec 10, 2019 at 14:17
  • It would be good if you add that to your question with a broader explanation, example of use, expected output and what should happen if the types do not match. Commented Dec 10, 2019 at 14:39
  • i've edited the question. thank you for your comments Commented Dec 10, 2019 at 14:52

2 Answers 2

3

I believe it would be better if you can expand more on your use case.
But, this should do what you want.

def readAs[T](str: String)(implicit num: Numeric[T]): Option[T] =
  num.parseString(str)

Which you can test like:

readAs[Int]("10")
// res: Option[Int] = Some(10)

readAs[Double]("10")
// res: Option[Double] = Some(10.0)

readAs[Double]("10.0d")
// res: Option[Double] = Some(10.0)

readAs[Int]("10.0d")
// res: Option[Int] = None

readAs[Int]("blah")
// res: Option[Int] = None
Sign up to request clarification or add additional context in comments.

Comments

1

Scala is not javascript. Scala is a real programming language with types. Strong types even. So, it treats conversions between types as what they really are: conversions. Not "casts". So, the string will have to be parsed into a number. And if you wrap this parsing in a function, it is utterly wrong to call this conversion a "cast".

And no, you cannot cast a string to a number in C++ either. Not with a dynamic cast, nor with any other kind of cast. You also have to parse it in C++, because C++ is also a real programming language.

As for simplifying your pattern matching expression, you might be able to first parse the string into a double, and then use a generic cast to convert that double into a number of lesser precision, but I do not have a Scala compiler at hand to prove the concept.

1 Comment

This could be condensed to a comment instead of an answer as it really doesn't answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.