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

Allow object expression without overrides #17387

Merged

Conversation

edgarfgp
Copy link
Contributor

@edgarfgp edgarfgp commented Jul 5, 2024

Description

Add support for object expression without overrides.

Before

To create an object expression without overrides, the user has to override a member, even if it is not necessary.

type IFirst = interface end

[<AbstractClass>]
type ClassEnd() = class end

// FS0738 Invalid object expression. Objects without overrides or interfaces should use the expression form 'new Type(args)' without braces.
let objExpr = { new ClassEnd() } 

// Workaround: override a member like ToString, Equals, GetHashCode, etc.
let objExpr = { new ClassEnd() with member this.ToString() = "ClassEnd" }

// Workaround: implement a marker interface
let objExpr = { new ClassEnd() interface IFirst }

type Class() = class end

//FS0738 Invalid object expression. Objects without overrides or interfaces should use the expression form 'new Type(args)' without braces.
let objExpr = { new Class() }

// Workaround: override a member like ToString, Equals, GetHashCode, etc.
let objExpr = { new Class() with member this.ToString() = "ClassEnd" }

// Workaround: implement a marker interface
let objExpr = { new Class() interface IFirst }

After

We won't need to use any workaround to use classes(abstract or non-abstract) in object expressions.

[<AbstractClass>]
type AbstractClass() = class end

// Ok
let objExpr = { new AbstractClass() }

type Class() = class end

// Ok
let objExpr = { new Class() }

Checklist

  • Test cases added
  • Release notes entry updated

Copy link
Contributor

github-actions bot commented Jul 5, 2024

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/9.0.100.md
LanguageFeatures.fsi docs/release-notes/.Language/preview.md

@edgarfgp edgarfgp force-pushed the allow-object-expression-without-overrides branch from e81303d to d08f0bb Compare July 6, 2024 21:41
@edgarfgp edgarfgp closed this Jul 15, 2024
@edgarfgp edgarfgp reopened this Jul 29, 2024
@edgarfgp edgarfgp force-pushed the allow-object-expression-without-overrides branch from d08f0bb to 860efd1 Compare July 29, 2024 19:00
@edgarfgp edgarfgp closed this Jul 30, 2024
@edgarfgp edgarfgp reopened this Jul 30, 2024
@edgarfgp edgarfgp changed the title Allow object expression without overrides Allow object expression without overrides when base class has protected constructor Jul 30, 2024
@edgarfgp edgarfgp force-pushed the allow-object-expression-without-overrides branch from 4446b57 to 1251e35 Compare July 30, 2024 14:59
@edgarfgp edgarfgp force-pushed the allow-object-expression-without-overrides branch from 743d695 to d8e0332 Compare August 7, 2024 16:40
@edgarfgp edgarfgp force-pushed the allow-object-expression-without-overrides branch from a78ad9b to 4a26619 Compare August 13, 2024 15:36
@edgarfgp edgarfgp force-pushed the allow-object-expression-without-overrides branch from 513f2ff to 2233c5f Compare August 14, 2024 08:02
@edgarfgp edgarfgp changed the title Allow object expression without overrides when base class has protected constructor Allow object expression without overrides Aug 15, 2024
@edgarfgp edgarfgp marked this pull request as ready for review August 16, 2024 06:14
@edgarfgp edgarfgp requested a review from a team as a code owner August 16, 2024 06:14
@vzarytovskii
Copy link
Member

Can be merged after RFC is approved by Don

…m:edgarfgp/fsharp into allow-object-expression-without-overrides
Copy link
Member

@psfinaki psfinaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fabulous work. Thanks for adding tests for the old versions as well. Great stuff Edgar!

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

Successfully merging this pull request may close these issues.

3 participants