value - is not a member of type parameter T | Scala Error - scala

I am using Scala 2.13.
I wrote the following code representing a 2D Point which works on only scala.math.Numeric types:
class Point[T: Numeric](val x: T, val y: T) {
def getDistance(otherPoint: Point[T]): Double = {
math.sqrt(math.pow(otherPoint.x - x, 2) + math.pow(otherPoint.y - y, 2))
}
override def toString = "(" + x + "," + y + ")"
}
in the 4th line, I get the error "value - is not a member of type parameter T", click to see the location where error occurs:
click to see error location
Where am I going wrong here? (I am new to generics, type bounds in scala)

Problem 1:
To use mathematical operators given a Numeric bound, you need to
import Numeric.Implicits._
(and Fractional/Integral.Implicits._ to get division operations). Without it you need to do something like
Numeric[T].minus(otherPoint.x, x)
or to give a name to the implicit Numeric parameter instead of using a context bound. With the import you can just write
otherPoint.x - x
The import can be declared at any scope: usually at the top of the file, but it can go inside the method if you only need it once.
Problem 2:
math.pow works only on Double, and there's no equivalent function for all Numerics in the standard library. So convert to Double using toDouble method (you could define a generic squaring function, but not really square root).

First, you need an extra import to enable the convenient symbol ops on a Numeric.
After that, feed pow() a parameter type that it understands.
class Point[T: Numeric](val x: T, val y: T) {
import Numeric.Implicits._
def getDistance(otherPoint: Point[T]): Double = {
math.sqrt(math.pow((otherPoint.x - x).toDouble, 2) +
math.pow((otherPoint.y - y).toDouble, 2))
}
override def toString = "(" + x + "," + y + ")"
}

Related

Scala add value to a char variable

I am creating a simple function in Scala
def addOne(m: Int): Int = m + 1
Using it with integers works normally, using double it throws a type mismatch error.
addOne(2) = 3
addOne(2.1) = error: type mismatch
When I use it with a character in double quotes, it throws a type mismatch as expected.
addOne("z") = error: type mismatch
However, when using a single quotes character it returns a value for that letter.
addOne('z') = 123
What is happening here and why is it like this?
The reason you can use a Char as an argument to a function taking an Int is because Scala performs an implicit conversion from Char to Int. This specific conversion is defined in the companion object of the Char class. See here:
http://www.scala-lang.org/api/2.12.1/scala/Char$.html (It seems like SO breaks this link at the $ character. Copy-paste it instead)
The function perfoming the conversion is called char2int. It converts the Char into its corresponding Unicode value as an Int.
When the Scala compiler sees that the types Char and Int don't match, it will first check if there are any available implicit conversions. It only gives a compile error if it didn't find any. If it finds an implicit conversion, it will insert that function call into your code. Your code is therefore transformed to this:
addOne(Char.char2int('z'))
If you want to make your own implicit conversion to, for example, let your function accept String, you can define this:
// Enable implicit conversions.
import scala.language.implicitConversions
// The "implicit" modifier is the important part here, not the name of the function.
implicit def string2int(s: String) = s.toInt
Now this compiles:
// This returns 6
addOne("5")
/*
* This throws a NumberFormatException due to my implementation of string2int.
* Create your own implementation of string2int if you want it to work properly.
*/
addOne("a")
Finally: Beware that implicit conversions are very powerful and therefore can be dangerous! See TheArchetypalPaul's comment for an explanation.
It is because addOne(m: Int) [The part after colon (:) ] tells Scala you will pass Int to it, not Double, not anything else.
if you want it to work for Double, try this, but then you will always get Double as Result.
def addone (m : Double ) = m+1
addone: (m: Double)Double
scala> addone(1)
res0: Double = 2.0
scala> addone(1.1)
res1: Double = 2.1
Scala map the char type using the ASCII Table. So, 'z' is mapped to 122, which is an integer. In the method, addOne('z'), the input parameter been cast to an integer (i.e. 122).
However, the input parameter in addOne(2.1) is 2.1, which is a double and in addOne("z") is a string. They cannot be cast to an integer automatically.
def addOne(m: Int): Int = m + 1 only accept an integer for m
It also work with a single quoted character (z) because it's translated into its ASCII value. The value for 'z' is 122 and you add 1.
scala> val foo: Int = 'z'
foo: Int = 122
scala> val bar = foo + 1
bar: Int = 123
If you want to make this working with double you can specify def addOne(m: Double): Double = m + 1

Scala Implicit Conversion Function Name Clashes

I am working with a simple complex number case class in Scala and would like to create an add function that works between complex numbers, doubles and ints. Below is a simple example of a working solution:
case class Complex(re: Double, im: Double)
implicit def toComplex[A](n: A)(implicit f: A => Double): Complex = Complex(n, 0)
implicit class NumberWithAdd[A](n: A)(implicit f: A => Complex) {
def add(m: Complex) = Complex(n.re + m.re, n.im + m.im)
}
Note I am deliberately not including the add function in the complex case class. Using the above I can do all of this:
scala> val z = Complex(1, 2); val w = Complex(2, 3)
z: Complex = Complex(1.0,2.0)
w: Complex = Complex(2.0,3.0)
scala> z add w
res5: Complex = Complex(3.0,5.0)
scala> z add 1
res6: Complex = Complex(2.0,2.0)
scala> 1 add z
res7: Complex = Complex(2.0,2.0)
I'd like to use '+' instead of 'add, but however this does not work. I get the following error:
Error:(14, 4) value + is not a member of A$A288.this.Complex
z + 1
^
Both z + w and 1 + z still work however.
What I'd like to know is why does changing the function name from 'add' to '+' break this? Is there an alternate route to getting this functionality (without simply putting the add function in the complex case class)? Any help would be appreciated.
Edit - Motivation
I'm playing around with monoids and other algebraic structures. I would like to be able to generalise the '...WithAdd' function to automatically work for any class that has a corresponding monoid:
trait Monoid[A] {
val identity: A
def op(x: A, y: A): A
}
implicit class withOp[A](n: A)(implicit val monoid: Monoid[A]) {
def +(m: A): A = monoid.op(n, m)
}
case class Complex(re: Double, im: Double) {
override def toString: String = re + " + " + im + "i"
}
class ComplexMonoid extends Monoid[Complex] {
val identity = Complex(0, 0)
def op(z: Complex, w: Complex): Complex = {
Complex(z.re + w.re, z.im + w.im)
}
}
implicit val complexMonoid = new ComplexMonoid
Using the above I can now do Complex(1, 2) + Complex(3, 1) giving Complex = 4.0 + 3.0i. This is great for code reuse as I could now add extra functions to the Monoid and withAdd function (such as appling op n times to an element, giving the power function for multiplication) and it would work for any case class that has a corresponding monoid. It is only with complex numbers and trying to incorporate doubles, ints, etc., that I then run into the problem above.
I would use a regular class, not a case class. Then it would be easy to create methods to add or subtract these Complex numbers, like:
class Complex(val real : Double, val imag : Double) {
def +(that: Complex) =
new Complex(this.real + that.real, this.imag + that.imag)
def -(that: Complex) =
new Complex(this.real - that.real, this.imag - that.imag)
override def toString = real + " + " + imag + "i"
}
As the source page shows, it will now support something that looks like operator overloading (it's not, because + and - are functions and not operators).
The problem with implicit class NumberWithAdd and its method + is that the same method also exist in number classes such as Int and Double. The + method of NumberWithAdd basically allows you to start with a number that can be casted to Complex and add a Complex object to that first item. That is, the left hand value can be anything (as long as it can be converted) and the right hand value must be Complex.
That works great for w + z (no need to convert w) and 1 + z (implicit conversion for Int to Complex is available). It fails for z + 1 because + is not available in the class Complex .
Since z + 1 is actually z.+(1), Scala will look for other possible matches for +(i: Int) in classes that Complex can be converted into. It also checks NumberWithAdd, which does have a + function but that one required a Complex as right hand value. (It would match a function that requires an Int as right hand value.) There are other functions named + that do accept Int, but there's no conversion from Complex to what those functions want as left hand values.
The same definition of + does work when it's in the (case) class Complex. In that case, both w + z and z + 1 simply use that definition. The case 1 + z is now a little more complicated. Since Int does not have a function + that accepts a Complex value, Scala will find the one that does (in Complex) and determines whether or not it is possible to convert Int into Complex. That is possible using the implicit functions, the conversion takes place and the function is executed.
When the function + in the class NumberWithAdd is renamed add, there's no confusion with functions in Int because Int does not have a function +. So Scala will try harder to apply the function add and it will do the Int to Complex conversion. It will even do that conversion when you try 1 add 2.
Note: My explanations may not fully describe the actual inner workings.

Can't use a negative number in named parameters in Scala

I'm using Scala 2.11.2.
If I have this Fraction class:
case class Fraction(numerator: Int, denominator: Int) {}
Then this gives an error:
val f = new Fraction(numerator=-1, denominator=2)
But this is not:
val f = new Fraction(-1, denominator=2)
The error message is:
Multiple markers at this line
- not found: value
numerator
- not found: value
numerator
I tried to use negative numbers in other snippets with the same result, but the documentation doesn't mentions that this is not possible.
Am I doing something wrong?
Thanks
You need a space between the = and the -, or you can wrap the -1 in parentheses, otherwise the compiler gets confused. This is because =- is a valid method name, so the compiler cannot tell whether you are assigning a value to a named parameter, or making a method call.
so this gives an error:
val f = Fraction(numerator=-1, denominator=2)
but this is OK:
val f = Fraction(numerator = -1, denominator = 2)
and so is this:
val f = Fraction(numerator=(-1), denominator=2)

Data Abstraction in Scala

We were given a problem in scala to define a procedure that that takes a line segment as an argument and returns its midpoint(the point whose coordinates are the average of the coordinates of the end points.) When I try to compile the program it gives me two errors, namely type mismatch errors in my midpointSegment method. I don't get why it requires a String. Can anyone point out my mistake? Below is my code.
class Point(x: Int, y: Int) {
def xCoord = x
def yCoord = y
def makeString(m: Point) = "Point" + "(" + x + "," + y + ")"
}
class LineSegment(x: Point, y: Point) {
def startSeg = x
def endSeg = y
def midpointSegment(m: LineSegment) = ((startSeg + m.startSeg) / 2,
(endSeg + m.endSeg) / 2)
def makeString(m: LineSegment) =
"LineSegment" + "(" + x.makeString(x) + "," + y.makeString(y) + ")"
}
object Mp5 {
def main(args: Array[String]): Unit = {
val aLine1 = new Point(1, 2)
val aLine2 = new Point(5, 4)
val aLineSegment1 = new LineSegment(aLine1, aLine2)
val aLineSegment2 = new LineSegment(new Point(-3, 5), new Point(8, -1))
println(aLine1.makeString(aLine1))
println(aLine2.makeString(aLine2))
println(aLineSegment1.makeString(aLineSegment1))
println(aLineSegment2.makeString(aLineSegment2))
println(aLineSegment1.midpointSegment(aLineSegment2))
}
}
You are trying to add two points, since startSeg and m.startSeg are points. You haven't defined how to do this, so the compiler seems to think you are adding strings (since anything can be added to a string, as in definition of toString). To be honest, I wouldn't expect this error if that's the entire code, and instead something about a missing + method.
For future reference: 1. provide the actual error message and stack trace; 2. you don't need to define methods like def xCoord = x in Scala, just write val x instead of simply x in class parameters; 3. read about case classes.

Having trouble adding update method to Symbol

I am attempting to add an update method to the Symbol class.
class SymbolUpdate(s: Symbol) {
def update(i: Int) = s.name + i
}
implicit def toSymbolUpdate(s: Symbol) = new SymbolUpdate(s)
But when I run the code I get the following
scala> 's = 1
<console>:327: error: value update is not a member of object Symbol
's = 1
^
But it does work when I call the method directly.
scala> 's.update(1)
res41: java.lang.String = s1
Or if I explicitly put an empty argument array.
scala> 's() = 1
res42: java.lang.String = s1
Not sure what the problem is with my code?
According to the Scala Language Spec:
An assignment f(args) = e with a function application to the left of the ‘=’ operator is interpreted as f.update(args, e), i.e. the invocation of an update function
defined by f.
It's especially clear if you read the corresponding section in Programming in Scala:
Similarly, when an assignment is made to a variable to which parenthesis and one or more arguments have been applied, the compiler will transform that into an invocation of an update methods that takes the arguments in parenthesis as well as the object to the right of the equals sign.
Together, I take it to mean that the parenthesis are required.