Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Found: String / Required: List[Any]?

 
Hugh Angle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following prints "now" and "is":


I wanted to recursively collect any's strings into a list, so i
generalized from the above and wrote:


The following compile error occurred:
found : String
required: List[Any]
case s:String=> s :: recurs(a.tail)
^
I am not sure what this error message is telling me. It is the
basic question I am putting forward to this forum.

I corrected the compilation problem with the following; but I am unsure
as to why this worked:


Of course, I now get a runtime exception 'ExceptionInInitializerError'.
When the 'any' list is empty, then the execution of 'a.head'
throws the exception. Easily fixed.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hugh Angle wrote:

The following compile error occurred:
found : String
required: List[Any]
case s:String=> s :: recurs(a.tail)
^

a is of type List[Any] and it is no way becomes String
a.head would work because Any is a super type of String

would the following code simpler?

if you prefer to use match
 
Hugh Angle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your code help to clear up the problem I was having:


The problem I was having was in the difference between
the two sets of codes. Why did the error occur in one
and not the other?



I have to believe that the comprehension made the
difference. In you code example, the expression:



was probably doing what the comprehension was
accomplishing.

I hope I am on the right track



-- INSERT --
1,15 Top
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hugh Angle wrote:
The problem I was having was in the difference between
the two sets of codes. Why did the error occur in one
and not the other?

Check again the beginning part of my previous post. (comment in code)
The problem due to List[Any] can't match to a String.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternate way to do what you're attempting:



A side benefit of doing it this way, besides being idiomatic, is that the resulting collection is correctly typed as a List[String]
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett's way is definitely the best, but in answer to the original question:

You get the error because you're trying to get the head of an empty List. You can tackle this firstly by explicitly checking for an empty List, viz: or, more idiomatically, matching on the list itself:
Notice I've put the return type as List[String]. You could leave it as List[Any], but why not make it a List of Strings since you can.

Contrary to what Raymond Tong says, there is no problem in matching a String by type in a List[Any]. This is the point of matching by type.

BTW I highly recommend 99 Scala Problems for practice with this kind of construct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic