An object expression for an abstract class without abstract elements

For a memberless interface

type IFoo = interface end 

it is possible to create an instance using an object expression

 let foo = { new IFoo } 

However, it is not possible to do the same with an abstract class that does not have abstract elements.

 [<AbstractClass>] type Foo() = class end let foo = { new Foo() } 

gives an error: Invalid object expression. Objects without overrides or interfaces should use the form expression 'new Type (args)' without curly braces. Well, we know that won't work either.

Here's a hacky workaround

 let foo = { new Foo() with member __.ToString() = base.ToString() } 

Is there a better way to create an object expression for an abstract type without abstract elements?

+6
source share
1 answer

I do not think the best way.

I also do not know why you would like to do this (an abstract class without participants), but I just assume that you have your own reasons and leave it to that :)

+1
source

Source: https://habr.com/ru/post/901631/


All Articles