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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:44:15+00:00 2026-05-30T02:44:15+00:00

I just uncovered this confusion and would like a confirmation that it is what

  • 0

I just uncovered this confusion and would like a confirmation that it is what it is. Unless, of course, I am just missing something.

Say, I have these data declarations:

data VmInfo = VmInfo {name, index, id :: String} deriving (Show)
data HostInfo = HostInfo {name, index, id :: String} deriving (Show)

vm = VmInfo "vm1" "01" "74653"
host = HostInfo "host1" "02" "98732"

What I always thought and what seems to be so natural and logical is this:

vmName = vm.name
hostName = host.name

But this, obviously, does not work. I got this.


Questions

So my questions are.

  • When I create a data type with record syntax, do I have to make sure that all the fields have unique names? If yes – why?

  • Is there a clean way or something similar to a “scope resolution operator”, like :: or ., etc., so that Haskell distinguishes which data type the name (or any other none unique fields) belongs to and returns the correct result?

  • What is the correct way to deal with this if I have several declarations with the same field names?


As a side note.

In general, I need to return data types similar to the above example.
First I returned them as tuples (seemed to me the correct way at the time). But tuples are hard to work with as it is impossible to extract individual parts of a complex type as easy as with the lists using “!!”. So next thing I thought of the dictionaries/hashes.
When I tried using dictionaries I thought what is the point of having own data types then?
Playing/learning data types I encountered the fact that led me to the above question.
So it looks like it is easier for me to use dictionaries instead of own data types as I can use the same fields for different objects.


Can you please elaborate on this and tell me how it is done in real world?

  • 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-30T02:44:16+00:00Added an answer on May 30, 2026 at 2:44 am

    Haskell record syntax is a bit of a hack, but the record name emerges as a function, and that function has to have a unique type. So you can share record-field names among constructors of a single datatype but not among distinct datatypes.

    What is the correct way to deal with this if I have several declarations with the same field names?

    You can’t. You have to use distinct field names. If you want an overloaded name to select from a record, you can try using a type class. But basically, field names in Haskell don’t work the way they do in say, C or Pascal. Calling it “record syntax” might have been a mistake.

    But tuples are hard to work with as it is impossible to extract individual parts of a complex type

    Actually, this can be quite easy using pattern matching. Example

    smallId :: VmInfo -> Bool
    smallId (VmInfo { vmId = n }) = n < 10
    

    As to how this is done in the “real world”, Haskell programmers tend to rely heavily on knowing what type each field is at compile time. If you want the type of a field to vary, a Haskell programmer introduces a type parameter to carry varying information. Example

    data VmInfo a = VmInfo { vmId :: Int, vmName :: String, vmInfo :: a }
    

    Now you can have VmInfo String, VmInfo Dictionary, VmInfo Node, or whatever you want.

    Summary: each field name must belong to a unique type, and experienced Haskell programmers work with the static type system instead of trying to work around it. And you definitely want to learn about pattern matching.

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

Sidebar

Related Questions

Just a knowledge question which I would like to ask: For example, I have
I just uncovered this little gem of an IE9 bug. It seems that IE9
Just learning the world of jquery, and all my googling gives examples like this:
Just a quick check really. I have an XML file that I will be
Just a quick question. Say I have updated my app to iOS6 and changed
I have a file called new.js.erb that contains this line: if( $('#user_password').val().length < 6
Just wondering. I have a page in Jquery Mobile which uses a popup that
Just like this question with no answer , I am trying to hook up
Just checking my JS and I have an error, but I cannot see where.
Just see this: SELECT clientid,clientname,startdate,enddate,age FROM clients WHERE clientid IN (1,2,3,4,5) AND CASE WHEN

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.