functional programming - F# pass by reference -
i'm trying pass reference in f#. in c#, pretty easy using ref , out keywords, seems in f#, not simple. read this: http://davefancher.com/2014/03/24/passing-arguments-by-reference-in-f/, suggests using reference cell. (i suppose means there no analog out keyword in c# don't have initialize variable pass it.)
but anyway, i'm wondering if best/only way pass reference in f#, , if idea in functional language.
edit:
i interested because realized exceptions slowing down code. i'm on crusade eliminate them. basically, plan this: function returns double or throws exception if there error. instead, i'll pass in double , errorcode type reference bit mask , pass values , errors down. i'm guessing i'll have make these variables mutable.
your question has number of parts try answer:
which suggests using reference cell
- yes, reference cells (or ref cells) idiomatic way pass mutable data function
i suppose means there no analog out keyword in c# don't have initialize variable pass it.
- c# uses
out
parameters signify additional return values. idomatic way in f# tuples. example c# functionint tryparse(string s, out bool succeeded)
tryparse(s : string) -> (int, bool)
- c# uses
if idea in functional language.
- this not consider normal, idiomatic f# code. way represent discriminated union representing possible results:
type myfunctionresult = | success of double | errorcase1 | errorcase2 of addditionaldatatype
etc
if have number of functions use style, it's very easy compose them bind
function.
Comments
Post a Comment