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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:14:59+00:00 2026-05-23T17:14:59+00:00

I have a list of type my_sum = {a : type_a} / {b :

  • 0

I have a list of

type my_sum = {a : type_a} / {b : type_b}
mylist = [{field_only_in_a = "test"} : type_a,{haha=3 ; fsd=4} : type_b]

I want to do that :

result = List.find( a -> match a with 
                                      | {a = _} -> true
                                      | _ -> false
                                     end,
                                mylist)
 if Option.is_some(result) then
     Option.some(Option.get(result).field_only_in_a)
 else
     Option.none

Like you can see, after the find I’m sure to get something of type_a but on compilation time I get that :

    Record has type
{ a : type_a } / { b : type_b }  but field access expected it to have type
{ field_only_in_a: 'a; 'r.a }
Hint:
  You tried to access a sum type with several cases as a record.

How could I say to the compilator, that I have extract only one type of the sum type and that I have the good type to acces the record… ?

  • 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-23T17:14:59+00:00Added an answer on May 23, 2026 at 5:14 pm

    Well, you cannot really inform the compiler that only a subtype will exist in the list… but you can explicitly create a list with this subtype only. Actually, what you are looking for is List.find_map which find a first element matching a certain criteria and maps it (you use this mapping to project from my_sum to its case type_a). Below is a fully working code (compiles on its own):

    type type_a = {fld_a : string}
    type type_b = {fld_b : int}
    type my_sum = {a : type_a} / {b : type_b}
    
    mylist = [{a = {fld_a = "test"}}, {b = {fld_b = 10}}] : list(my_sum)
    
    get_first_a(l : list(my_sum)) : option(type_a) =
      List.find_map(
        (el -> match el
               | ~{a} -> some(a)
               | _ -> none
        ), l)
    
    _ = prerr("First a-typed element of {mylist} is {get_first_a(mylist)}\n")
    

    If there was no List.find_map function in the stdlib there would still be a ton of ways to do it. Probably the simplest would be to use List.filter_map to obtain a list(type_a) and then get its head with List.head_opt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a list of regex patterns (stored in a list type) that I
I have IQueryable list of objects of type T which I want to transform
Hi all I have list of type string with some items. i want to
I want to subclass the list type and have slicing return an object of
I have List List<MyType> , my type contains Age and RandomID Now I want
Perhaps a stupid question: I have a List of type <Data> which I want
I have a list of type IList<Effort> . The model Effort contains a float
I have a list of input type radio buttons in formview (edit mode) and
I have list of words. I type in a word misspelled. Can I query
I have a List of a complex type - an object with a few

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.