-
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
Computation expressions in function declaration #1147
Comments
I feel it would be more natural to swap around the keywords, so that, with the given example, it would be
Not only does it feel more natural when comparing with the English language, but also the (edit: Note that I am not arguing pro here, but rather how I think the syntax should be if implemented. Hopefully thumbs up or down reflect that.) |
It will be very logical to also treat async in the same way. Otherwise people would really wonder what's been going on. (edit: While checking my posts for sanity, I see now that this post was unneeded, because the proposal is about computation expressions, and not just |
Yes, I considered this, but it would be a breaking change if the name of the function name is the same as the CE I am not confident with the syntax I proposed, would be nice to think of a simpler way to define this type of computation block, even if it would be only for async and task which are really common cases |
In the lisp world, they have |
What about an attribute?
and my favorite way to place such attributes, in order to avoid code drift, and a little bit in order to save lines
edit, add: But again, the suggestion I believe is about computation expressions in general, isn't it? So perhaps something like this is needed instead, if attributes are to be used:
Again, I'm not arguing pro yet, but just thinking about how it might be done. |
CE are already the one of the more magical things in F#. I don't know if making them less obvious, or having more ways of creating them is in the benefit of clarity. |
Keeping them separate and clear keeps the language simple and IMO this would confuse new comers if not old devs as well, also this would alter the ML simple syntax andin troduce unnecessarily complexity. |
It seems to me that functions don't really have much to do with this suggestion, they're just a special case of 'CEs should use the same scoping syntax as the rest of the language, i.e. indentation instead of braces'. Something like let answer =
async!
do! something()
return 42
// or with undentation support
let answer = async!
do! something()
return 42
|
Personally, I don't see how is it different (let alone better) than existing syntax with |
I never liked the braces that much, and with piaste's suggestion I'm turning my vote in favor. To me, the braces seem like an odd syntax borrowed from C/C++/C#/Java and mixed into the otherwise elegant F# syntax. To me, the braces are slightly annoying to edit and read. Btw, that's one reason why we like F# over C#. Yes, it'll be another way of doing the same thing, just as F#'s light syntax is also another way of doing the same thing. In this case I think we'd actually be doing away with an odd way of doing a thing, and doing it in a more normal way instead. |
It would get rid of the ugly curly braces surrounding the code, especially the trailing one at the end. How about: let {async} answer =
do! something()
return 42 The curly braces say "this is a CE" and the use of them should preclude confusion with existing code binding to a name that matches a CE name. |
@realparadyne wouldn't that clash or be confused with record deconstruction for parameters? |
Brackets look odd and clunky, but I think that's the point. Significant whitespace is more elegant for scoping than explicit delimiters within the same syntactical structure, but CEs use significantly different syntax from regular F#, even implementing a |
This feature is trying to add some surface behaviour of higher kinded types, like `` let CE f : T`` might as well be ``let f :CE<T>`` , IMHO the language should stay simple the curly braces aren't bad, or we go in the direction of the do notation in haskell
Get Outlook for Android<https://aka.ms/AAb9ysg>
…________________________________
From: Lyndon Gingerich ***@***.***>
Sent: Tuesday, May 31, 2022 3:22:37 PM
To: fsharp/fslang-suggestions ***@***.***>
Cc: Ayman Bouchareb ***@***.***>; Comment ***@***.***>
Subject: Re: [fsharp/fslang-suggestions] Computation expressions in function declaration (Issue #1147)
Brackets look odd and clunky, but I think that's the point. Significant whitespace is more elegant for scoping than explicit delimiters within the same language, but CEs use significantly different syntax from regular F#, even implementing a return keyword. Just as F# uses explicit delimiters for code quotations, so it should also for snippets from other syntaxes, including computation expressions. Imagine the confusion if F# were to implement quotations from, say, Python without explicit delimiters!
—
Reply to this email directly, view it on GitHub<#1147 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AOF3HZEHVRUDODEOQ5PDRC3VMYOC3ANCNFSM5WTANUBA>.
You are receiving this because you commented.Message ID: ***@***.***>
|
There's already the let isEven = function
| x when x % 2 = 0 -> true
| _ -> false What if the abbreviated CE keyword was in the same location? let asyncId value = task
let! x = Task.FromResult value
return x |
So what @piaste said, but without the exclamation mark? |
CE builders aren't keywords though, they're normal values. So this is very ambiguous. |
if it were to be done, and while I am passionate about removing braces/brackets/parens, I don't think it should... but if it were to be done it would need a new keyword to indicate that a CE were starting and then the ce builder. let asyncID x = Compute task
let! y = Task.FromResult x
return y now if it implicitly bound/returned like let asyncID = Compute task
Task.FromResult especially if you could do more than one line with implicit let! between each let asyncID = Compute task
Task.FromResult
someOtherFunction
//could be equivalent to
let asyncID x = task {
let! y = Task.FromResult x
let! z = someOtherFunction y
return z
}
While to me this provides much more value because now you have a brief notation that has a specific use akin to |
I like the idea... mainly the implicit return, in this context makes a lot of sense I dislike the implicit something like this for the same purpose: let! task ayncID x =
Task.FromResult x
!> someOtherFunction
let asyncID' = fun! task x -> Task.FromResult x !> someOtherFunction Maybe it could infer the CE from usage or context, (even if it was only for the native CE) let! ayncID x =
Task.FromResult x
!> someOtherFunction
let asyncID' = fun! x -> Task.FromResult x !> someOtherFunction For me looks more Another option is to throw the task! ayncID x =
Task.FromResult x
!> someOtherFunction
let asyncID' = task! x -> Task.FromResult x !> someOtherFunction other ideas: compute task ayncID x = // ...
process task ayncID x = // ...
with task ayncID x = // ...
begin task ayncID x = // ...
do task ayncID x = // ... |
If you were okay with operators you can just make a local bind operator, so no CE needed. let (>>=) = TaskBuilder.bind |
Yes I am, maybe this should be a new discussion because it is not correlated with the CE function declaration, but with expressiveness inside it defining and using bind |
@lucasteles have you tried out F#+ yet? |
I will close this for reasons like this I do get the suggestion - TypeScript has |
Computed expressions in function declaration
I propose a way to set a computation expression as default in a function declarations:
Pros and Cons
The advantages of making this adjustment to F# is a simple way to define function scoped in computation expressions
The disadvantages of making this adjustment to F# are more than one way to write the same thing
Extra information
Estimated cost (XS, S, M, L, XL, XXL):
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick this 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: