Scala - value < is not a member of AnyVal
Here is my code. I got a compile error at second case clause:
Error:(92, 26) value < is not a member of AnyVal
case x if x._2 < 2 => "price under 2"
def tupleMatch: Unit = {
val donut = Tuple2("Donut", 2.5)
val plain = Tuple2("Plain", 1.0)
val tuples = List(donut, plain)
tuples.foreach { tuple =>
val toPrint = tuple match {
case ("Donut", price) => s"price of Donut is ${donut._2}"
case (_, price) if price < 2 => "price under 2"
case _ => "other"
}
println(toPrint)
}
}
After I change
val plain = Tuple2("Plain", 1)
to
val plain = Tuple2("Plain", 1.0)
it finally works.
It makes me confusing, I want to know why?
scala functional-programming
add a comment |
Here is my code. I got a compile error at second case clause:
Error:(92, 26) value < is not a member of AnyVal
case x if x._2 < 2 => "price under 2"
def tupleMatch: Unit = {
val donut = Tuple2("Donut", 2.5)
val plain = Tuple2("Plain", 1.0)
val tuples = List(donut, plain)
tuples.foreach { tuple =>
val toPrint = tuple match {
case ("Donut", price) => s"price of Donut is ${donut._2}"
case (_, price) if price < 2 => "price under 2"
case _ => "other"
}
println(toPrint)
}
}
After I change
val plain = Tuple2("Plain", 1)
to
val plain = Tuple2("Plain", 1.0)
it finally works.
It makes me confusing, I want to know why?
scala functional-programming
add a comment |
Here is my code. I got a compile error at second case clause:
Error:(92, 26) value < is not a member of AnyVal
case x if x._2 < 2 => "price under 2"
def tupleMatch: Unit = {
val donut = Tuple2("Donut", 2.5)
val plain = Tuple2("Plain", 1.0)
val tuples = List(donut, plain)
tuples.foreach { tuple =>
val toPrint = tuple match {
case ("Donut", price) => s"price of Donut is ${donut._2}"
case (_, price) if price < 2 => "price under 2"
case _ => "other"
}
println(toPrint)
}
}
After I change
val plain = Tuple2("Plain", 1)
to
val plain = Tuple2("Plain", 1.0)
it finally works.
It makes me confusing, I want to know why?
scala functional-programming
Here is my code. I got a compile error at second case clause:
Error:(92, 26) value < is not a member of AnyVal
case x if x._2 < 2 => "price under 2"
def tupleMatch: Unit = {
val donut = Tuple2("Donut", 2.5)
val plain = Tuple2("Plain", 1.0)
val tuples = List(donut, plain)
tuples.foreach { tuple =>
val toPrint = tuple match {
case ("Donut", price) => s"price of Donut is ${donut._2}"
case (_, price) if price < 2 => "price under 2"
case _ => "other"
}
println(toPrint)
}
}
After I change
val plain = Tuple2("Plain", 1)
to
val plain = Tuple2("Plain", 1.0)
it finally works.
It makes me confusing, I want to know why?
scala functional-programming
scala functional-programming
edited Nov 16 '18 at 16:36
Amit Prasad
573316
573316
asked Nov 16 '18 at 9:53
Felix JFelix J
314
314
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
That is because your tuples array is formed by two different types: Tuple2[String, Int]
and Tuple2[String, Double]
. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]
. When you put a Double representation the compiler is capable of create the Tuple2[String, Double]
.
add a comment |
Looking at the REPL:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
Because the two Tuples have the same type (String, Double)
, the List is of type List[(String, Double)]
. <
is a valid function for Double.
When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int
vs Double
) so the resulting List is List[(String, AnyVal)]
:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
... and <
is not a valid function for AnyVal
.
add a comment |
It because of we cannot perform <
(less than) operation on AnyVal
type.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
As you can see above. When you define Tuple2("Donut", 2.5)
, 2.5
is inferred to Double
and in Tuple2("Plain", 1)
, 1
is inferred as Int
since you have not provided type explicitly.
Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal)
.
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
Now, when you are performing case x if x._2 < 2
you are actually comparing AnyVal
type to number i.e. 2
, which is not possible because compiler thinks that x._2
is AnyVal
(not number) and won't compile. Therefore, you are getting exception at compile time.
<console>:22: error: value < is not a member of AnyVal
case x if (x._2 < 2) => "price under 2"
In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0)
, its number type is also inferred as Double
.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
And, when you create list of tuples, since both have same type i.e. (String, Double)
, list will have type List[(String, Double)]
.
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
And x._2 < 2
will compile correctly since you are invoking <
in double (number).
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335311%2fscala-value-is-not-a-member-of-anyval%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
That is because your tuples array is formed by two different types: Tuple2[String, Int]
and Tuple2[String, Double]
. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]
. When you put a Double representation the compiler is capable of create the Tuple2[String, Double]
.
add a comment |
That is because your tuples array is formed by two different types: Tuple2[String, Int]
and Tuple2[String, Double]
. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]
. When you put a Double representation the compiler is capable of create the Tuple2[String, Double]
.
add a comment |
That is because your tuples array is formed by two different types: Tuple2[String, Int]
and Tuple2[String, Double]
. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]
. When you put a Double representation the compiler is capable of create the Tuple2[String, Double]
.
That is because your tuples array is formed by two different types: Tuple2[String, Int]
and Tuple2[String, Double]
. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]
. When you put a Double representation the compiler is capable of create the Tuple2[String, Double]
.
edited Nov 16 '18 at 10:18
peterschrott
376219
376219
answered Nov 16 '18 at 10:12
EmiCareOfCell44EmiCareOfCell44
1,277415
1,277415
add a comment |
add a comment |
Looking at the REPL:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
Because the two Tuples have the same type (String, Double)
, the List is of type List[(String, Double)]
. <
is a valid function for Double.
When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int
vs Double
) so the resulting List is List[(String, AnyVal)]
:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
... and <
is not a valid function for AnyVal
.
add a comment |
Looking at the REPL:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
Because the two Tuples have the same type (String, Double)
, the List is of type List[(String, Double)]
. <
is a valid function for Double.
When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int
vs Double
) so the resulting List is List[(String, AnyVal)]
:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
... and <
is not a valid function for AnyVal
.
add a comment |
Looking at the REPL:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
Because the two Tuples have the same type (String, Double)
, the List is of type List[(String, Double)]
. <
is a valid function for Double.
When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int
vs Double
) so the resulting List is List[(String, AnyVal)]
:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
... and <
is not a valid function for AnyVal
.
Looking at the REPL:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
Because the two Tuples have the same type (String, Double)
, the List is of type List[(String, Double)]
. <
is a valid function for Double.
When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int
vs Double
) so the resulting List is List[(String, AnyVal)]
:
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
... and <
is not a valid function for AnyVal
.
answered Nov 16 '18 at 10:20
James WhiteleyJames Whiteley
2,246829
2,246829
add a comment |
add a comment |
It because of we cannot perform <
(less than) operation on AnyVal
type.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
As you can see above. When you define Tuple2("Donut", 2.5)
, 2.5
is inferred to Double
and in Tuple2("Plain", 1)
, 1
is inferred as Int
since you have not provided type explicitly.
Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal)
.
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
Now, when you are performing case x if x._2 < 2
you are actually comparing AnyVal
type to number i.e. 2
, which is not possible because compiler thinks that x._2
is AnyVal
(not number) and won't compile. Therefore, you are getting exception at compile time.
<console>:22: error: value < is not a member of AnyVal
case x if (x._2 < 2) => "price under 2"
In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0)
, its number type is also inferred as Double
.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
And, when you create list of tuples, since both have same type i.e. (String, Double)
, list will have type List[(String, Double)]
.
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
And x._2 < 2
will compile correctly since you are invoking <
in double (number).
add a comment |
It because of we cannot perform <
(less than) operation on AnyVal
type.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
As you can see above. When you define Tuple2("Donut", 2.5)
, 2.5
is inferred to Double
and in Tuple2("Plain", 1)
, 1
is inferred as Int
since you have not provided type explicitly.
Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal)
.
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
Now, when you are performing case x if x._2 < 2
you are actually comparing AnyVal
type to number i.e. 2
, which is not possible because compiler thinks that x._2
is AnyVal
(not number) and won't compile. Therefore, you are getting exception at compile time.
<console>:22: error: value < is not a member of AnyVal
case x if (x._2 < 2) => "price under 2"
In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0)
, its number type is also inferred as Double
.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
And, when you create list of tuples, since both have same type i.e. (String, Double)
, list will have type List[(String, Double)]
.
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
And x._2 < 2
will compile correctly since you are invoking <
in double (number).
add a comment |
It because of we cannot perform <
(less than) operation on AnyVal
type.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
As you can see above. When you define Tuple2("Donut", 2.5)
, 2.5
is inferred to Double
and in Tuple2("Plain", 1)
, 1
is inferred as Int
since you have not provided type explicitly.
Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal)
.
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
Now, when you are performing case x if x._2 < 2
you are actually comparing AnyVal
type to number i.e. 2
, which is not possible because compiler thinks that x._2
is AnyVal
(not number) and won't compile. Therefore, you are getting exception at compile time.
<console>:22: error: value < is not a member of AnyVal
case x if (x._2 < 2) => "price under 2"
In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0)
, its number type is also inferred as Double
.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
And, when you create list of tuples, since both have same type i.e. (String, Double)
, list will have type List[(String, Double)]
.
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
And x._2 < 2
will compile correctly since you are invoking <
in double (number).
It because of we cannot perform <
(less than) operation on AnyVal
type.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1)
plain: (String, Int) = (Plain,1)
As you can see above. When you define Tuple2("Donut", 2.5)
, 2.5
is inferred to Double
and in Tuple2("Plain", 1)
, 1
is inferred as Int
since you have not provided type explicitly.
Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal)
.
scala> val tuples = List(donut, plain)
tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))
Now, when you are performing case x if x._2 < 2
you are actually comparing AnyVal
type to number i.e. 2
, which is not possible because compiler thinks that x._2
is AnyVal
(not number) and won't compile. Therefore, you are getting exception at compile time.
<console>:22: error: value < is not a member of AnyVal
case x if (x._2 < 2) => "price under 2"
In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0)
, its number type is also inferred as Double
.
scala> val donut = Tuple2("Donut", 2.5)
donut: (String, Double) = (Donut,2.5)
scala> val plain = Tuple2("Plain", 1.0)
plain: (String, Double) = (Plain,1.0)
And, when you create list of tuples, since both have same type i.e. (String, Double)
, list will have type List[(String, Double)]
.
scala> val tuples = List(donut, plain)
tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))
And x._2 < 2
will compile correctly since you are invoking <
in double (number).
answered Nov 16 '18 at 10:20
Ra KaRa Ka
1,73011326
1,73011326
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335311%2fscala-value-is-not-a-member-of-anyval%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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