fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Allow object expression without overrides

Open dmitry-a-morozov opened this issue 8 years ago • 6 comments

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 avatar Dec 07 '17 23:12 dmitry-a-morozov

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

dsyme avatar Jan 12 '18 12:01 dsyme

Does this need an RFC?

7sharp9 avatar Jul 17 '18 09:07 7sharp9

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

dmitry-a-morozov avatar Aug 01 '18 19:08 dmitry-a-morozov

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 https://github.com/dotnet/fsharp/pull/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 }

edgarfgp avatar Aug 15 '24 10:08 edgarfgp

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.

vzarytovskii avatar Aug 16 '24 15:08 vzarytovskii

This can be closed. I implemented it in https://github.com/dotnet/fsharp/pull/17387

edgarfgp avatar Sep 08 '24 18:09 edgarfgp