Circular reference and attributes.
See original GitHub issueMoved from https://github.com/fsharp/fsharp/issues/565
Repro steps
Paste the following code in FSI
open System
[<AttributeUsage(AttributeTargets.Property)>]
type MyAttribute() =
inherit Attribute()
member this.Foo () =
new MyClass(1)
and MyClass(someValue) =
[<MyAttribute()>]
member this.SomeValue : int = someValue
;;
Expected behavior
It would compile to
type MyAttribute =
class
new : unit -> MyAttribute
member Foo : unit -> MyClass
end
and MyClass =
class
new : someValue:int -> MyClass
member SomeValue : int
end
Actual behavior
[<MyAttribute()>]
------^^^^^^^^^^^
stdin(90,7): error FS0848: A custom attribute must invoke an object constructor
Known workarounds
Do it in C#.
Related information
Ran in fsi
Microsoft (R) F# Interactive version 14.0.23413.0
And in Visual Studio 2015 Update 2 with using a DLL with .NET 4.6.1, and the 4.4.0.0 F# Runtime.
This is done on a Windows 8.1 machine, but can reproduce on a Windows 10 (latest patches, until the one from 4/12/2016, non-Insider branch).
Use case:
namespace Test
open System
open System.ComponentModel.DataAnnotations
[<AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)>]
type MyAttribute() =
class
inherit ValidationAttribute ()
override this.IsValid (value: Object, validationContext: ValidationContext) =
match validationContext.ObjectInstance with
| :? MyClass as item ->
// TODO more validation
ValidationResult.Success
| _ ->
new ValidationResult("No no no")
end
and MyClass(someValue) =
[<MyAttribute>]
member this.SomeValue : int = someValue
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Circular reference in Excel: how to find, enable, use, or ...
Learn the basics of Excel circular reference - how to check workbooks for circular formulas, and how to enable, find and remove circular ......
Read more >Circular Reference Error in JavaScript – Meaning and How ...
This object has properties containing values of the string, boolean, array, and number data type. ... Here, we add a languages property, which ......
Read more >What Is the Best Algorithm for Circular Reference Check in ...
Understanding Circular References Each node contains a set of attributes and references other nodes in the network. A circular reference occurs ...
Read more >Circular reference attributed grammars — their evaluation ...
This paper presents a combination of Reference Attributed Grammars (RAGs) and Circular Attribute Grammars (CAGs). While.
Read more >code quality - What's wrong with circular references?
There are a great many things wrong with circular references: Circular class references create high coupling; both classes must be ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a workaround, you could define your types in a signature file first, this seems to work:
Foo.fsi
Foo.fs
Note this is a good test case for https://github.com/fsharp/FSharpLangDesign/blob/master/RFCs/FS-1009-mutually-referential-types-and-modules-single-scope.md
The code would become the more pleasant: