-
Notifications
You must be signed in to change notification settings - Fork 21
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
Default Type Annotation #1374
Comments
Feels like a shortcut on the highway to obfuscation :) Honestly: My opinion in general is that when information becomes relevant for program understanding, it should be as local as possible, and type annotations fulfil that. This proposal would introduce a delocalisation that contradicts this. In addition, I think it is very unusual for identifier names to have a meaning beyond their documentary character 1. It could be a hard and annoying process getting one's brain trained on such a concept. Also, what should happen when refactoring the name of that identifier? Further, it seems that this syntax addition adresses a problem that might not occur often, since inference works very well for non-overloaded and non-dot-notated stuff. Footnotes
|
For the specific 'problem' of You could do that but name it Semi-related blog article re new C# 12 features in this direction https://devblogs.microsoft.com/dotnet/refactor-your-code-using-alias-any-type/ |
I don't think it "removes the clutter", it adds a completely new (global) language construct, which uses existing one (type) in a different unfamiliar way. As well as adds a "new" source for the type inference, which has a special treatment. I personally dislike the proposal, and don't think we should be adding it in the language, will add the "probably not", @dsyme, @cartermp, @abelbraaksma if you feel we should do it, please remove. Besides, you can already do that in the signature file. |
This would probably not be allowed on the basis that:
Furthermore, by using type abbreviations it's possible to reduce the clutter like @bartelink suggests: open System.Threading
type CTS = CancellationTokenSource
let f (cts: CTS) = cts.Token |
I considered this feature for F# 1.0. The context was that in an earlier project (a theorem prover) I had been really surprised what a huge difference to readability and succinctness it had made to be able to declare that particular patterns of identifiers had particular types. It requires discipline to use such a feature well, and it makes more sense in the context of "mathematical" style of development - specifications, model implementations, proofs, tensor programming etc - but I think it's an under-explored are of typed-programming language design. That said, I don't think it makes sense for F# as a general purpose programming language that needs to consider software engineering needs in teams and large codebases. It is the kind of feature which can be very powerful for the individual developer. So I agree this is a "no" for F#, though I appreciate the suggestion |
I also have thought of this feature in the past. By the way I have used dummy inline functions for more concise type annotations. |
I propose we allow default type annotation
Consider this classic example:
The type has to be annotated somehow:
This is clutter, and F# doesn’t like clutter.
I propose a default type annotation syntax, which allows extracting type annotation clutter from code, moving it out of the way, to top of file perhaps, or to a module. For example:
A default type annotation
Default type annotation is a convenience feature. Implicit shadowing
Alternatives
Another name for the feature might be “type annotation hint”.
The overloading of the
type
keyword does not seem to me a problematic addition to the grammar. I chosetype
because it is already a keyword directly related to the feature, and because it is short and monosyllabic. Conceivable alternatives that I can think of areannotation
orannotate
orhint
, which suffer from various problems. Using any of those keywords seems infeasible at this late stage.Perhaps there is some other concise syntax or modification to the proposed syntax that I have not thought of.
There is no existing way of approaching this problem in F#
Also, as far as I can tell, it does not exist in other languages.
Pros and Cons
Advantages
Significant reduction in clutter. It seems to me that the impossibility of inferencing the type of
a
ina.B
is the most common source of type annotation clutter and is solved by the proposal.Disadvantages
Reading and understanding code without the benefit of a smart interactive editor (which shows the type of a name with little effort) can be more difficult where a default type annotation name does not match the intended type, but I observe that
Extra information
Estimated cost (XS, S, M, L, XL, XXL):
XS?
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: