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 opt-in (by attribute?) warning if indexing is slow #589

Open
5 tasks done
rmunn opened this issue Jul 1, 2017 · 2 comments
Open
5 tasks done

Allow opt-in (by attribute?) warning if indexing is slow #589

rmunn opened this issue Jul 1, 2017 · 2 comments

Comments

@rmunn
Copy link

rmunn commented Jul 1, 2017

In the discussion of #588, the idea of a compiler warning on indexing operations was briefly floated. (Because they could potentially throw exceptions and are thus unsafe). Don Syme mentioned performance issues on indexing operations, which suggests another possible compiler warning. If an indexing operation on a given data structure is "slow" (precisely what "slow" means might vary from context to context), maybe the designers of the data structure could tag that operation with an attribute that would generate a compiler warning, text definable by the user. For example, if the index operator on Lists was tagged with that attribute, the text might look like: "This operation is included for API compatibility with Array. But while indexing into an array is O(1), indexing into a list is O(N). Unless you know that N will be small, it is recommended that you avoid index operations on a list."

The name of this attribute could be InefficientOperationWarningAttribute, or just InefficientOperationAttribute since gluing together more than two or three CamelCase words can become unreadable very fast.

The ObsoleteAttribute is similar in how it generates a compiler warning with user-definable text, but its semantics are not what I'm suggesting here. ObsoleteAttribute suggests that the function might be removed from future iterations of the API, whereas I'm suggesting an attribute that would communicate the warning, "This function is in the API to stay, but should be avoided unless you know what you're doing. There are other functions that will do this operation more efficiently."

Pros and Cons

The advantages of making this adjustment to F# are that we can warn people away from operations that they may not know are inefficient (.[n] on a list or seq, Seq.last...).

The disadvantages of making this adjustment to F# are yet another attribute, slower compilation, and larger code size (unless there's a way to make the attribute available at compile-time only, but not bloat the runtime code with it — and I don't know of any way to do that).

Extra information

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

Related suggestions:

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
@Rickasaurus
Copy link

I love this line of thought. We can make F# the most beginner friendly language around!

@dsyme
Copy link
Collaborator

dsyme commented Jan 10, 2023

I agree with this, it fits with various other suggestions by @charlesroddie for more opt-in warnings. Marking as approved-in-principle. Please consider submitting an RFC and PR for this.

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

3 participants