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 attributes before the and keyword #733

Closed
5 tasks done
Tarmil opened this issue Apr 9, 2019 · 7 comments
Closed
5 tasks done

Allow attributes before the and keyword #733

Tarmil opened this issue Apr 9, 2019 · 7 comments

Comments

@Tarmil
Copy link

Tarmil commented Apr 9, 2019

Allow attributes before the and keyword

Right now the only way to add attributes to recursively defined types or values is to put the attribute after the and keyword:

[<Struct>]
type Foo = { x : option<Bar> }

and [<Struct>] Bar = { y : option<Foo> }

I propose that we allow putting the attributes before and, making this more consistent:

[<Struct>]
type Foo = { x : option<Bar> }

[<Struct>]
and Bar = { y : option<Foo> }

and similarly for values:

[<ReflectedDefinition>]
let rec isOdd x =
    x = 1 || isEven (x - 1)

[<ReflectedDefinition>]
and isEven x =
    x = 0 || isOdd (x - 1)

Pros and Cons

The advantages of making this adjustment to F# are:

  • More consistent syntax between recursive and non-recursive types and value

The disadvantages of making this adjustment to F# are:

  • More complex parsing maybe? I doubt it.

Extra information

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

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • 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:

  • 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
@7sharp9
Copy link
Member

7sharp9 commented Apr 9, 2019

Yeah this caught me out last week I was perplexed why it didnt work that way.

@abelbraaksma
Copy link
Member

10 upvotes if I could. When you have large or more than one attribute, this quickly becomes unreadable presently. I thought I reported this a while ago too, but can't find it back.

@charlesroddie
Copy link

charlesroddie commented May 12, 2019

The root cause of this is that and replaces type or let in subsequent definitions. How about this syntax: more regular?

[<Struct>]
type Foo = { x : option<Bar> }
and
[<Struct>]
type Bar = { y : option<Foo> }

[<ReflectedDefinition>]
let rec isOdd x =
    x = 1 || isEven (x - 1)
and
[<ReflectedDefinition>]
let isEven x =
    x = 0 || isOdd (x - 1)

This syntax might suggest adding recursive type/let combinations. Not sure if this is a good idea.

module AndTest =
    type Foo() =
        static member f = Bar()
    and
    let Bar() = Foo.f()

@dsyme
Copy link
Collaborator

dsyme commented Apr 12, 2023

Just use a module rec or namespace rec please

@dsyme dsyme closed this as completed Apr 12, 2023
@auduchinok
Copy link

@dsyme It seems this one is quite easy to add, and it would make it much easier to use attributes on types with and.

It's possible to parse additional attributes in tyconDefnList and to add them the same way it's done for xmldocs. See https://github.com/dotnet/fsharp/blob/b66de358fd257d742faf603e2301182d176e7bb6/src/Compiler/pars.fsy#L1516-L1517

@auduchinok
Copy link

Just use a module rec or namespace rec please

We've tried to use it, but found that there're issues in analysis (e.g. dotnet/fsharp#7931) and some things are checked differently with rec added, so we try to avoid it unless really needed.

@dsyme
Copy link
Collaborator

dsyme commented May 2, 2023

We've tried to use it, but found that there're issues in analysis (e.g. dotnet/fsharp#7931) and some things are checked differently with rec added, so we try to avoid it unless really needed.

Yes, we should fix that issue for sure....

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

7 participants