what is difference between (1 to 10 by 3) toList and (1 to 10 by 3).toList in scala(or intellij problem?)
up vote
0
down vote
favorite
if this question is stupid, please understand.
I'm starting to learn scala with basic grammar.
But I don't understand why one is error other is not. I tested this with scala worksheet
val range = 1 to 10 by 3 toList
println(s"$range") //error
but the below is the List(actually Seq)
val range = (1 to 10 by 3).toList
println(s"$range") //no problem
I am using intellij 2018.2 ultimate edit.
Edit: Image was changed. I attached capture that is included error message not warning.
Edit: I thought it is intellij version problem, but still doesn't work
scala intellij-idea
|
show 3 more comments
up vote
0
down vote
favorite
if this question is stupid, please understand.
I'm starting to learn scala with basic grammar.
But I don't understand why one is error other is not. I tested this with scala worksheet
val range = 1 to 10 by 3 toList
println(s"$range") //error
but the below is the List(actually Seq)
val range = (1 to 10 by 3).toList
println(s"$range") //no problem
I am using intellij 2018.2 ultimate edit.
Edit: Image was changed. I attached capture that is included error message not warning.
Edit: I thought it is intellij version problem, but still doesn't work
scala intellij-idea
1
That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax likeval range = Range(1, 11, 3).toList
orRange.inclusive(1, 10, 3)
instead of(1 to 10 by 3).toList
which involves theimplicits
overInt
and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.
– Sarvesh Kumar Singh
Nov 22 at 8:50
it'a warning telling you should enable postfixOps if you use them (as you do)import scala.language.postfixOps
– Raf
Nov 22 at 8:56
you can go into scala-> general -> advance language feature (tick mark it)
– Raman Mishra
Nov 22 at 8:57
It's worth noting that theList
object has a.range()
method:List.range(1,10,3)
No.toList
conversion needed.
– jwvh
Nov 22 at 8:58
1
@HackingJ you need to end the line with post-fix operator either with;
or new line or defining aval
,def
after the post-fix operator.
– prayagupd
Nov 22 at 9:43
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
if this question is stupid, please understand.
I'm starting to learn scala with basic grammar.
But I don't understand why one is error other is not. I tested this with scala worksheet
val range = 1 to 10 by 3 toList
println(s"$range") //error
but the below is the List(actually Seq)
val range = (1 to 10 by 3).toList
println(s"$range") //no problem
I am using intellij 2018.2 ultimate edit.
Edit: Image was changed. I attached capture that is included error message not warning.
Edit: I thought it is intellij version problem, but still doesn't work
scala intellij-idea
if this question is stupid, please understand.
I'm starting to learn scala with basic grammar.
But I don't understand why one is error other is not. I tested this with scala worksheet
val range = 1 to 10 by 3 toList
println(s"$range") //error
but the below is the List(actually Seq)
val range = (1 to 10 by 3).toList
println(s"$range") //no problem
I am using intellij 2018.2 ultimate edit.
Edit: Image was changed. I attached capture that is included error message not warning.
Edit: I thought it is intellij version problem, but still doesn't work
scala intellij-idea
scala intellij-idea
edited Nov 22 at 12:30
asked Nov 22 at 8:40
Hacking J
1199
1199
1
That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax likeval range = Range(1, 11, 3).toList
orRange.inclusive(1, 10, 3)
instead of(1 to 10 by 3).toList
which involves theimplicits
overInt
and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.
– Sarvesh Kumar Singh
Nov 22 at 8:50
it'a warning telling you should enable postfixOps if you use them (as you do)import scala.language.postfixOps
– Raf
Nov 22 at 8:56
you can go into scala-> general -> advance language feature (tick mark it)
– Raman Mishra
Nov 22 at 8:57
It's worth noting that theList
object has a.range()
method:List.range(1,10,3)
No.toList
conversion needed.
– jwvh
Nov 22 at 8:58
1
@HackingJ you need to end the line with post-fix operator either with;
or new line or defining aval
,def
after the post-fix operator.
– prayagupd
Nov 22 at 9:43
|
show 3 more comments
1
That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax likeval range = Range(1, 11, 3).toList
orRange.inclusive(1, 10, 3)
instead of(1 to 10 by 3).toList
which involves theimplicits
overInt
and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.
– Sarvesh Kumar Singh
Nov 22 at 8:50
it'a warning telling you should enable postfixOps if you use them (as you do)import scala.language.postfixOps
– Raf
Nov 22 at 8:56
you can go into scala-> general -> advance language feature (tick mark it)
– Raman Mishra
Nov 22 at 8:57
It's worth noting that theList
object has a.range()
method:List.range(1,10,3)
No.toList
conversion needed.
– jwvh
Nov 22 at 8:58
1
@HackingJ you need to end the line with post-fix operator either with;
or new line or defining aval
,def
after the post-fix operator.
– prayagupd
Nov 22 at 9:43
1
1
That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like
val range = Range(1, 11, 3).toList
or Range.inclusive(1, 10, 3)
instead of (1 to 10 by 3).toList
which involves the implicits
over Int
and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.– Sarvesh Kumar Singh
Nov 22 at 8:50
That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like
val range = Range(1, 11, 3).toList
or Range.inclusive(1, 10, 3)
instead of (1 to 10 by 3).toList
which involves the implicits
over Int
and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.– Sarvesh Kumar Singh
Nov 22 at 8:50
it'a warning telling you should enable postfixOps if you use them (as you do)
import scala.language.postfixOps
– Raf
Nov 22 at 8:56
it'a warning telling you should enable postfixOps if you use them (as you do)
import scala.language.postfixOps
– Raf
Nov 22 at 8:56
you can go into scala-> general -> advance language feature (tick mark it)
– Raman Mishra
Nov 22 at 8:57
you can go into scala-> general -> advance language feature (tick mark it)
– Raman Mishra
Nov 22 at 8:57
It's worth noting that the
List
object has a .range()
method: List.range(1,10,3)
No .toList
conversion needed.– jwvh
Nov 22 at 8:58
It's worth noting that the
List
object has a .range()
method: List.range(1,10,3)
No .toList
conversion needed.– jwvh
Nov 22 at 8:58
1
1
@HackingJ you need to end the line with post-fix operator either with
;
or new line or defining a val
, def
after the post-fix operator.– prayagupd
Nov 22 at 9:43
@HackingJ you need to end the line with post-fix operator either with
;
or new line or defining a val
, def
after the post-fix operator.– prayagupd
Nov 22 at 9:43
|
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
If you're getting this error ...
Error:(2, 13) recursive lazy value range needs type
... it's because you're using infix (dot-less) notation where you shouldn't.
Put a blank line before the println()
, or a semicolon after the toList
and it will work.
The reason for the error is that instance.method(argument)
can be turned into ...
instance method argument
... but the compiler is often confused if you try to turn instance.method
into ...
instance method
The compiler will look for the missing argument
until it hits a semicolon or a blank line.
add a comment |
up vote
1
down vote
Either should work. In scala you can omit .
with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION
Example:
scala> val data = "guitar"
data: String = guitar
scala> data.toUpperCase
res8: String = GUITAR
you can use space instead of .
,
scala> data toUpperCase
<console>:13: warning: postfix operator toUpperCase should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
data toUpperCase
^
res9: String = GUITAR
Since .toUpperCase
is used after the data-structure without .
(it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,
scala> import scala.language.postfixOps
import scala.language.postfixOps
scala> data toUpperCase
res10: String = GUITAR
Same thing applies to your example. From readability perspective dot makes more readable in your example.
scala> (1 to 10 by 3).toList
res9: List[Int] = List(1, 4, 7, 10)
ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt
apply the toInt
at the end of the computation but maybe what I wanted was 1 + "11".toInt
ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList
with ;
or a new line after post-fix operator. Or you can use val
, def
after post-fix operator that way compiler knows it is different context now.
scala> :paste
import scala.language.postfixOps
val range = 1 to 10 by 3 toList
println(s"$range")
// Exiting paste mode, now interpreting.
List(1, 4, 7, 10)
import scala.language.postfixOps
range: List[Int] = List(1, 4, 7, 10)
Also read: Let’s drop postfix operators
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you're getting this error ...
Error:(2, 13) recursive lazy value range needs type
... it's because you're using infix (dot-less) notation where you shouldn't.
Put a blank line before the println()
, or a semicolon after the toList
and it will work.
The reason for the error is that instance.method(argument)
can be turned into ...
instance method argument
... but the compiler is often confused if you try to turn instance.method
into ...
instance method
The compiler will look for the missing argument
until it hits a semicolon or a blank line.
add a comment |
up vote
1
down vote
If you're getting this error ...
Error:(2, 13) recursive lazy value range needs type
... it's because you're using infix (dot-less) notation where you shouldn't.
Put a blank line before the println()
, or a semicolon after the toList
and it will work.
The reason for the error is that instance.method(argument)
can be turned into ...
instance method argument
... but the compiler is often confused if you try to turn instance.method
into ...
instance method
The compiler will look for the missing argument
until it hits a semicolon or a blank line.
add a comment |
up vote
1
down vote
up vote
1
down vote
If you're getting this error ...
Error:(2, 13) recursive lazy value range needs type
... it's because you're using infix (dot-less) notation where you shouldn't.
Put a blank line before the println()
, or a semicolon after the toList
and it will work.
The reason for the error is that instance.method(argument)
can be turned into ...
instance method argument
... but the compiler is often confused if you try to turn instance.method
into ...
instance method
The compiler will look for the missing argument
until it hits a semicolon or a blank line.
If you're getting this error ...
Error:(2, 13) recursive lazy value range needs type
... it's because you're using infix (dot-less) notation where you shouldn't.
Put a blank line before the println()
, or a semicolon after the toList
and it will work.
The reason for the error is that instance.method(argument)
can be turned into ...
instance method argument
... but the compiler is often confused if you try to turn instance.method
into ...
instance method
The compiler will look for the missing argument
until it hits a semicolon or a blank line.
answered Nov 22 at 9:52
jwvh
25k52038
25k52038
add a comment |
add a comment |
up vote
1
down vote
Either should work. In scala you can omit .
with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION
Example:
scala> val data = "guitar"
data: String = guitar
scala> data.toUpperCase
res8: String = GUITAR
you can use space instead of .
,
scala> data toUpperCase
<console>:13: warning: postfix operator toUpperCase should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
data toUpperCase
^
res9: String = GUITAR
Since .toUpperCase
is used after the data-structure without .
(it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,
scala> import scala.language.postfixOps
import scala.language.postfixOps
scala> data toUpperCase
res10: String = GUITAR
Same thing applies to your example. From readability perspective dot makes more readable in your example.
scala> (1 to 10 by 3).toList
res9: List[Int] = List(1, 4, 7, 10)
ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt
apply the toInt
at the end of the computation but maybe what I wanted was 1 + "11".toInt
ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList
with ;
or a new line after post-fix operator. Or you can use val
, def
after post-fix operator that way compiler knows it is different context now.
scala> :paste
import scala.language.postfixOps
val range = 1 to 10 by 3 toList
println(s"$range")
// Exiting paste mode, now interpreting.
List(1, 4, 7, 10)
import scala.language.postfixOps
range: List[Int] = List(1, 4, 7, 10)
Also read: Let’s drop postfix operators
add a comment |
up vote
1
down vote
Either should work. In scala you can omit .
with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION
Example:
scala> val data = "guitar"
data: String = guitar
scala> data.toUpperCase
res8: String = GUITAR
you can use space instead of .
,
scala> data toUpperCase
<console>:13: warning: postfix operator toUpperCase should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
data toUpperCase
^
res9: String = GUITAR
Since .toUpperCase
is used after the data-structure without .
(it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,
scala> import scala.language.postfixOps
import scala.language.postfixOps
scala> data toUpperCase
res10: String = GUITAR
Same thing applies to your example. From readability perspective dot makes more readable in your example.
scala> (1 to 10 by 3).toList
res9: List[Int] = List(1, 4, 7, 10)
ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt
apply the toInt
at the end of the computation but maybe what I wanted was 1 + "11".toInt
ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList
with ;
or a new line after post-fix operator. Or you can use val
, def
after post-fix operator that way compiler knows it is different context now.
scala> :paste
import scala.language.postfixOps
val range = 1 to 10 by 3 toList
println(s"$range")
// Exiting paste mode, now interpreting.
List(1, 4, 7, 10)
import scala.language.postfixOps
range: List[Int] = List(1, 4, 7, 10)
Also read: Let’s drop postfix operators
add a comment |
up vote
1
down vote
up vote
1
down vote
Either should work. In scala you can omit .
with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION
Example:
scala> val data = "guitar"
data: String = guitar
scala> data.toUpperCase
res8: String = GUITAR
you can use space instead of .
,
scala> data toUpperCase
<console>:13: warning: postfix operator toUpperCase should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
data toUpperCase
^
res9: String = GUITAR
Since .toUpperCase
is used after the data-structure without .
(it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,
scala> import scala.language.postfixOps
import scala.language.postfixOps
scala> data toUpperCase
res10: String = GUITAR
Same thing applies to your example. From readability perspective dot makes more readable in your example.
scala> (1 to 10 by 3).toList
res9: List[Int] = List(1, 4, 7, 10)
ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt
apply the toInt
at the end of the computation but maybe what I wanted was 1 + "11".toInt
ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList
with ;
or a new line after post-fix operator. Or you can use val
, def
after post-fix operator that way compiler knows it is different context now.
scala> :paste
import scala.language.postfixOps
val range = 1 to 10 by 3 toList
println(s"$range")
// Exiting paste mode, now interpreting.
List(1, 4, 7, 10)
import scala.language.postfixOps
range: List[Int] = List(1, 4, 7, 10)
Also read: Let’s drop postfix operators
Either should work. In scala you can omit .
with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION
Example:
scala> val data = "guitar"
data: String = guitar
scala> data.toUpperCase
res8: String = GUITAR
you can use space instead of .
,
scala> data toUpperCase
<console>:13: warning: postfix operator toUpperCase should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scaladoc for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
data toUpperCase
^
res9: String = GUITAR
Since .toUpperCase
is used after the data-structure without .
(it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,
scala> import scala.language.postfixOps
import scala.language.postfixOps
scala> data toUpperCase
res10: String = GUITAR
Same thing applies to your example. From readability perspective dot makes more readable in your example.
scala> (1 to 10 by 3).toList
res9: List[Int] = List(1, 4, 7, 10)
ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt
apply the toInt
at the end of the computation but maybe what I wanted was 1 + "11".toInt
ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList
with ;
or a new line after post-fix operator. Or you can use val
, def
after post-fix operator that way compiler knows it is different context now.
scala> :paste
import scala.language.postfixOps
val range = 1 to 10 by 3 toList
println(s"$range")
// Exiting paste mode, now interpreting.
List(1, 4, 7, 10)
import scala.language.postfixOps
range: List[Int] = List(1, 4, 7, 10)
Also read: Let’s drop postfix operators
edited Nov 22 at 10:07
answered Nov 22 at 8:53
prayagupd
19.3k888136
19.3k888136
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53426876%2fwhat-is-difference-between-1-to-10-by-3-tolist-and-1-to-10-by-3-tolist-in-sc%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
1
That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like
val range = Range(1, 11, 3).toList
orRange.inclusive(1, 10, 3)
instead of(1 to 10 by 3).toList
which involves theimplicits
overInt
and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.– Sarvesh Kumar Singh
Nov 22 at 8:50
it'a warning telling you should enable postfixOps if you use them (as you do)
import scala.language.postfixOps
– Raf
Nov 22 at 8:56
you can go into scala-> general -> advance language feature (tick mark it)
– Raman Mishra
Nov 22 at 8:57
It's worth noting that the
List
object has a.range()
method:List.range(1,10,3)
No.toList
conversion needed.– jwvh
Nov 22 at 8:58
1
@HackingJ you need to end the line with post-fix operator either with
;
or new line or defining aval
,def
after the post-fix operator.– prayagupd
Nov 22 at 9:43