Skip to content

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 closed this Jul 15, 2024
@edgarfgp edgarfgp reopened this Jul 29, 2024
@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 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
Contributor

@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