question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better Support For C# Records

See original GitHub issue

Hello.

Currently, the library has support for C# records, at least it claims to have one. But that doesn’t seem to fully work (or I may be missing something 🤦‍♂️).

I’ve seen the entities in FsCheck/Records.cs at master · fscheck/FsCheck · GitHub and tests that use them FsCheck/Arbitrary.fs at master · fscheck/FsCheck · GitHub

At first glance since the tests pass it seems that random records are being generated. But they are rather “randomly empty” all the time 😢.

Checked versions of the packages:

  • FsCheck.Xunit -> 2.16.4
    • FsCheck -> 2.16.4

Example

F# project that references a C# project with a Person record:

namespace CSharp {
    public record Person {
        public string FirstName { get; init; }
        public string LastName { get; init; }
    }
}

and contains a similar F# record too:

type FsPerson = { FirstName: string; LastName: string }

[<EntryPoint>]
let main args =
    let csharpPersons = Arb.generate<CSharp.Person> |> sample 10 10
    let fsharpPersons = Arb.generate<FsPerson> |> sample 10 10
    42

Values in the program are:

image



The tricky part is that the library fails for “init-only C# records” (as above), but indeed generates the data if at least one property in the C# record is mutable (has a public “set”).

namespace CSharp {
    public record Person {
        public string FirstName { get; init; }
        public string LastName { get; set; }
    }
}
type FsPerson = { FirstName: string; LastName: string }

[<EntryPoint>]
let main args =
    let csharpPersons = Arb.generate<CSharp.Person> |> sample 10 10
    let fsharpPersons = Arb.generate<FsPerson> |> sample 10 10
    42

Values in the program are:

image



So, I wonder whether the library has support for init-only C# records since in my case I:

  • don’t want to make any properties mutable in my domain

  • must stick with C# for my domain because of other developers in the team who aren’t familiar with F#

  • don’t want to use an awkward hack like this one (which won’t work if the base type is sealed):

namespace CSharp {
    public record Person
    {
        public string FirstName { get; init; }
        public string LastName { get; init; }
    }

    public record ChildForTestOnly: Person {
        public int DummyMutableProp { get; set; }
    }
}
type FsPerson = { FirstName: string; LastName: string }

[<EntryPoint>]
let main args =
    let csharpPersons = Arb.generate<CSharp.Person> |> sample 10 10
    let fsharpPersons = Arb.generate<FsPerson> |> sample 10 10
    let childPersons = Arb.generate<CSharp.ChildForTestOnly> |> sample 10 10
    // ... upcast the "child persons" to "persons" and test them ...
    42

Values in the program are:

image


Any ideas or suggestions?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:19 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
kurtschelfthoutcommented, Jun 2, 2022

overall size is distributed to properties, so the more properties you have the smaller the size will be for those properties. Same for tuples: https://github.com/fscheck/FsCheck/blob/master/src/FsCheck/ReflectArbitrary.fs#L83

so many properties => smaller size for each of them. Try the above example with a record with one or two properties and at size 10 you’ll see some non-zero values.

1reaction
kurtschelfthoutcommented, Jun 1, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

CNAME Record - How it Works, Alternatives & Advanced Use ...
A Canonical Name (CNAME) Record is used in the Domain Name System (DNS) ... In addition, ALIAS has better performance than CNAME because...
Read more >
Differences Between A and CNAME records
The A and CNAME records are the two common ways to map a host name (“name”) to one or more IP addresses. There...
Read more >
Differences Among A, CNAME, ALIAS, and URL records
These are the main differences: The A record maps a name to one or more IP addresses when the IP are known and...
Read more >
About CNAME records - Google Workspace Admin Help
A Canonical Name or CNAME record is a type of DNS record that maps an alias name to a true or ... See...
Read more >
Add a CNAME record to your domain's DNS records
Professional email, online storage, shared calendars, video meetings and more. Start your free Google Workspace trial today.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found