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 6000629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:43:16+00:00 2026-05-23T00:43:16+00:00

If I have a function defined as let test = function | [] ->

  • 0

If I have a function defined as

let test = function
    | [] -> None
    | head::tail -> Some(head)

fsi will allow me to define this and the compile will compile it; but it will fall over should I ever actually try to do test [].

Now I know the reasoning, when I give it an empty set it can’t infer the type and so the generic function fails, but can it not do something a bit cleverer? (along the lines of, “I don’t know the type of 'a but in this case I’m not using 'a so I’m going to allow this.”)

Anyway, is there any way I can avoid this problem?

  • 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-23T00:43:17+00:00Added an answer on May 23, 2026 at 12:43 am

    There is a great article about value restriction on MSDN and additional notes about tricky aspects by Brian that explains this problem in details.

    When you write test [] the result has a type option<'a>, so the compiler needs to know the type to be used in place of 'a. You’re not actually using values of type 'a, but the compiler needs to compile the code that uses it. F# doesn’t allow working with generic values (in general) so the value needs to have concrete type.

    You can write something like:

    let foo () = test []
    

    This is a standard generic function of type unit -> option<'a>, so this is a perfectly valid construct. You can also use type annotations to specify the type of the result explicitly:

    (test []:option<obj>)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this: test = function() { this.init = function() { $(#someElement).html(Hey!).mousedown(function()
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
I have a simple f# quick sort function defined as: let rec qsort(xs:List<int>) =
I have a function defined in a module called DataAccess like this module DataAccess
Let's say I have a normalize function defined as: Vec3f Vec3f::getNormalized() const { return
I have defined a function like the following: let ff (f1: a_function) (f2: a_function)
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>

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.