question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circular reference and attributes.

See original GitHub issue

Moved 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kidcommented, Apr 15, 2016

As a workaround, you could define your types in a signature file first, this seems to work:

Foo.fsi

namespace Foo

open System

[<AttributeUsage(AttributeTargets.Property)>]
type MyAttribute =
    inherit System.Attribute

    new : unit -> MyAttribute

    member Foo : unit -> MyClass

and MyClass =
    new : someValue : int -> MyClass

    [<MyAttribute()>]
    member SomeValue : int

Foo.fs

namespace Foo

open System

[<AttributeUsage(AttributeTargets.Property)>]
type MyAttribute() =
    inherit Attribute()

    member this.Foo () =
        new MyClass(1)

and MyClass(someValue) =
    // [<MyAttribute()>] -> specified in the fsi, still appears in compiled code
    member this.SomeValue : int = someValue
1reaction
dsymecommented, Apr 14, 2016

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:

namespace rec Test

open System
open System.ComponentModel.DataAnnotations

[<AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)>]
type MyAttribute() =
    inherit ValidationAttribute ()

    override this.IsValid (value: Object, validationContext: ValidationContext) =
        match validationContext.ObjectInstance with
        | :? MyClass as item ->    ValidationResult.Success
        | _ ->     new ValidationResult("No no no")

type MyClass(someValue) =
    [<MyAttribute()>]
    member this.SomeValue : int = someValue
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found