Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6231873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:00:47+00:00 2026-05-24T10:00:47+00:00

It seems the type of Nil is not polymorphic. How do I correct this

  • 0

It seems the type of Nil is not polymorphic. How do I correct this function:

scala> def last[A](a:List[A]) : A =   
     | a match {                      
     |   case _ :: tail  => last(tail)
     |   case Nil => Nil[A]           
     | }                              
<console>:8: error: object Nil does not take type parameters.
     case Nil => Nil[A]

UPDATE:

scala> def last[A](a : List[A] ) : Option[A] =
     | a match {                              
     |   case head :: Nil => Some(head)       
     |   case _ :: tail => last(tail)  
     |   case Nil => None                     
     | }                                      
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:00:48+00:00Added an answer on May 24, 2026 at 10:00 am

    Nil is an object, not a type. So Nil[A] doesn’t make any sense.

    An empty list does not have a last element. As such invoking last on Nil should throw an error.

    def last[A](l: List[A]): A = l match {                      
      case x :: Nil => x
      case _ :: tail => last(tail)
      case Nil => sys.error("An empty list")
    } 
    

    Alternatively you could have last return an Option[A] as shown below:

    def last[A](l: List[A]): Option[A] = l match {                      
      case x :: Nil => Some(x)
      case _ :: tail => last(tail)
      case Nil => None
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are the rules? OTOH the simple case seems to imply the new type
This seems like a bug in Rails to me, but there's probably not much
A using declaration does not seem to work with an enum type: class Sample{
Seems to me most of developers completely ignore this features. People prefer handling security
I've been getting this error in emacs whenever I type anything in certain buffers:
This is my code: NSError *error = nil; SBJsonParser *parserJson = [[SBJsonParser alloc] init];
I'm having a bit of trouble figuring out what's going wrong with this function.
I'm not sure whether this is an issue with my use of cl-who (specifically
I have the following classes/traits in Scala trait Write[-T] { def add(elem : T);
let's try some calls to the type function : user=> (type 10) java.lang.Integer user=>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.