Replies: 1 comment 5 replies
-
@gusty Yes, it's an important question that was largely glossed over in F# 2.0. I believe what's there is under test in FSharp.Core.UnitTests, so will be hard to change either way. Practically speaking most people consider the I think the normative guidance would thus be to fail fast on null inputs, with the String functions being an exception for legacy reasons. That is we would not encourage attributing any semantic meaning to "null". Do you have a specific proposal on a mod to the design-guidelines to capture this better? |
Beta Was this translation helpful? Give feedback.
-
I wanted to start the discussion on "How F# functions are expected to deal with null values?"
It turns out that in FSharp.Core the null handling for String functions is completely different and I would say completely opposite to what it does with say Arrays.
It's important to note that both types have null as a proper value, I mean
let x:'T = null
compiles fine when 'T is a string or an array, whereas it fails when 'T is something liketype MyType = MyType
with anerror FS0043: The type 'MyType' does not have 'null' as a proper value
message.Which could have resulted in
false
as it's clear thatnull
doesn't contains1
.With Strings, what FSharp.Core does instead of checking nulls is it converts them to empty strings, which IMHO is going too far, sitting on the other extreme, and let me tell you why I think so:
If we have a set of string functions that convert nulls to empty string, the user might eventually rely on some string conversions, say
toUpper
as it would take care of nulls, but suddenly the business requirement to convert those strings to Uppercase is removed. Months later they find out some null pointer exceptions in some specific cases in production, which generates a lot of research.Some companies may have a business rule which includes null string, like for instance this value was not supplied at all, which differs from an empty string. Whereas this is a good idea or not is subject of debate, but the fact is that using those functions would break their logic.
Apart from those points, stuff like
toUpper null = ""
simply feels wrong to me.Finally, the function
toObj
suggests that an isomorphism may arise between options and nullable types. We can actually doand then the question is, if we're fighting back nulls, by silently converting them to empty strings, why do we generate them here?
Note:
toUpper
is not in FSharp.Core but this discussion is broader than FSharp.Core the idea is to define how it should behave in any F# library.My feeling is that this was done at early stages without thinking too much. Probably having
nullAsEmpty
as a separate function would have been a better idea, so user can freely combine it with the rest of the functions.Both behaviors can be surprising to users, but specially the one with strings. We can't do breaking changes to FSharp.Core but still we can decide on what's the ideal criteria and apply it to most F# library functions and eventually to new functions in FSharp.Core and add it to the Design Guidelines, which btw currently they only mention null checks when interoperating with other languages.
@dsyme what do you think it should be the standard expectation when designing F# libraries?
Beta Was this translation helpful? Give feedback.
All reactions