Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give a constructorless type a private/internal constructor #906

Open
dsyme opened this issue Aug 31, 2020 · 1 comment
Open

Give a constructorless type a private/internal constructor #906

dsyme opened this issue Aug 31, 2020 · 1 comment

Comments

@dsyme
Copy link
Collaborator

dsyme commented Aug 31, 2020

Transferring dotnet/fsharp#8093

Shouldn't a constructorless type have a private/internal constructor in IL?

Looking at these two types in sharplab https://sharplab.io/#v2:DYLgZgzgNAJiDUAfALgTwA4FMAEAxA9vgIzYC8AsAFDY3YTICGyAlgMbYC2mHARpgE7ZmMbAA8yYqlTRY8hAEzZ0/ZgDcmOABQBKMlVp1GLdl14ChI8aTFA=

type Foo1 =
    static member id x = x

type Foo2 private () =
    static member id x = x

Foo1 can't be constructed in F# similar to Foo2 but only Foo2 has an internal constructor in IL, leaving Foo1 to be constructed from C# without issue, is this by design?

@abelbraaksma
Copy link
Member

abelbraaksma commented Aug 31, 2020

is this by design?

Since it'll be relevant to this discussion, let me answer that last question (copied from dotnet/fsharp#8093 (comment)):

Do note that the F# Spec specifically says that if the default constructors are omitted that default constructors are not created. So technically this seems to me to be a change to the spec.

image

If I understand the issue at hand correctly, the absence of a default ctor allows C# to create an instance. But adding a default (private) ctor, while fixing the C# issue, will allow creating instances in F#, wouldn't it (though only in private scope)?

It seems perhaps more reasonable that if there's no constructor at all that we mark it AbstractClass? We might even warn and/or disallow creating instance methods, as there's no way to call them, not even through inheritance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants