[Haskell-cafe] (SPAM 3)Re: handling NULL value in database querywith Maybe (or other ...)

Paul aquagnu at gmail.com
Tue Dec 18 19:28:53 UTC 2018


➢ The advantage of Maybe is...
This is not advantage of Maybe, but advantage of pattern-matching which allows you to “extract” value only from “Just a” branch and no way to get value from Nothing. But wait, I can get the same exception as in C/C++: fromJust Nothing without “if isJust...”! So, Damien is right here – no difference with C and Haskell is not more safe than C here – and you CAN omit checking and to get run time exception. I’m not sure about SML, may be it does not allow to get value in so dangerous way, but Haskell allows, like C. You will not get warning at compile time even 😊 But in C# 8 you will get warning with such dangerous using of Nullable.
Common rule is to use pattern-matching. The same technique, sure, is possible in C – you need to wrap value in some structure and to use special functions/macros to extract value, modern C++ libraries already have “option” types.


From: Ian Denhardt
Sent: 18 декабря 2018 г. 21:01
To: Damien Mattei; haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] (SPAM 3)Re: handling NULL value in database querywith Maybe (or other ...)

Quoting Damien Mattei (2018-12-18 10:34:15)

> if (x == NULL)
>    printf("NULL\n");
> else
>    printf("%s",*x);

C will also allow you to just write:

    printf("%s", *x);

Without the if, which will silently compile without complaint, and then
segfault (at best) or corrupt memory at runtime. With Maybe there's no
way to try to access the value if it isn't actually there.

As others have pointed out, your database has a concept of null (to
which the article's complaints are spot on), so somehow you have to
model the fact that it may not actually return a string.

There are more ergonomic ways of dealing with it than having the same
case expression everywhere; other replies suggested using the `maybe`
function to supply a default, or modifying the database schema to say
NOT NULL -- but you have to deal with it somehow. The advantage of Maybe
is:

* It makes the possibility of no-value explicit, and not something you
  can forget to handle.
* It makes it possible to express that no-value is *not* a possibility,
  when that is the case (by just not using a Maybe).

-Ian
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20181218/3af6873a/attachment.html>


More information about the Haskell-Cafe mailing list