aspose file tools*
The moose likes Scala and the fly likes Found: String / Required: List[Any]? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Scala
Bookmark "Found: String / Required: List[Any]?" Watch "Found: String / Required: List[Any]?" New topic
Author

Found: String / Required: List[Any]?

Hugh Angle
Greenhorn

Joined: Apr 15, 2009
Posts: 4
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.


Hugh V. Angle

notecard.org
notecard.org/myart
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

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

Joined: Apr 15, 2009
Posts: 4

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

Joined: Aug 15, 2010
Posts: 230
    
    2

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

Joined: Jan 17, 2006
Posts: 1296
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]


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Found: String / Required: List[Any]?
 
Similar Threads
Interview Questions
String object creation
need help:dos based .clp to java gui
Scala exercises for beginners (in FP)
Trouble reading the regex value