This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Found: String / Required: List[Any]?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
please buy my thing and then I'll have more money:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic