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 #632

Closed
1 task
dmitry-a-morozov opened this issue Dec 7, 2017 · 6 comments
Closed
1 task

Allow object expression without overrides #632

dmitry-a-morozov opened this issue Dec 7, 2017 · 6 comments

Comments

@dmitry-a-morozov
Copy link

dmitry-a-morozov commented Dec 7, 2017

Described here

https://stackoverflow.com/questions/8154730/object-expression-for-abstract-class-without-abstract-members

I propose we allow it because it's necessary when base class has protected constructor.

The existing way of approaching this problem in F# is ...

Define top level class that only invokes base constructor.

Pros and Cons

The advantages of making this adjustment to F# are ...
Better inter-op with existing NET libraries.

The disadvantages of making this adjustment to F# are ...
Extra work.

Extra information

Estimated cost (XS, S, M, L, XL, XXL):
No idea.

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • [x ] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • [ x] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [ x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • [ x] This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@dmitry-a-morozov dmitry-a-morozov changed the title Allow object expression for abstract class without abstract members Allow object expression without overrides Dec 7, 2017
@dsyme
Copy link
Collaborator

dsyme commented Jan 12, 2018

This makes total sense, there is no reason why we shouldn't allow this. Marking as approved-in-principle

@7sharp9
Copy link
Member

7sharp9 commented Jul 17, 2018

Does this need an RFC?

@dmitry-a-morozov
Copy link
Author

@7sharp9 It probably does. Can you please create one?

@edgarfgp
Copy link
Collaborator

edgarfgp commented Aug 15, 2024

Im looking into writing the RFC but I think it should be possible to remove this limitation for abstract classes and not abstract classes as it already possible for interfaces. cc @dsyme @vzarytovskii

See WIP implementation dotnet/fsharp#17387

type IFoo = interface end

type Class() =
    do printfn "Hello"
    member this.Method() = ()

let a = { new Class() with member this.ToString() = ""  } // We are force to override ToString to make it work
let b = { new Class() interface IFoo } // Or implement a marker interface

[<AbstractClass>]
type AbstractClass() =
    do printfn "Hello"
    
let c = { new AbstractClass() with member this.ToString() = "" } // We are force to override ToString to make it work
let d = { new AbstractClass() interface IFoo } // Or implement a marker interface

// It should be possible to remove this limitation for abstract classes and not abstract classes as it already possible for interfaces

let e = { new IFoo }

@vzarytovskii
Copy link

vzarytovskii commented Aug 16, 2024

I am fine with it, as long as @dsyme is (he may have specific reason why it is explicitly disallowed currently), but it feels more uniform (however confusing, as it's "do the same thing in two different ways").

dotnet fsi

Microsoft (R) F# Interactive version 12.8.400.0 for F# 8.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> type ClassEnd() = class end
- ;;
type ClassEnd =
  new: unit -> ClassEnd

> { new ClassEnd() };;

  { new ClassEnd() };;
  ^^^^^^^^^^^^^^^^^^

/Users/u/code/fsharp/stdin(6,1): error FS0738: Invalid object expression. Objects without overrides or interfaces should use the expression form 'new Type(args)' without braces.

@edgarfgp
Copy link
Collaborator

edgarfgp commented Sep 8, 2024

This can be closed. I implemented it in dotnet/fsharp#17387

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

5 participants