Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. More F# neat features

More F# neat features

Scheduled Pinned Locked Moved The Lounge
csharpc++dotnetvisual-studiocom
20 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nemanja Trifunovic

    It is safer and harder to ignore. If a method returns an option - you get an option type as a result and then match cases for 'Some' and 'None'. If a method returns i.e. a string, nothing tells you that Null is a possible result. Sure, you can check each and every return value for Null, but with options it is easier and safer: if a "not exists" is a valid value, you get an option type; if it isn't, there is nothing to check - the reference points to a valid object. Haskell Maybe monad[^] serves a similar purpose and in C++ there is Boost.Optional[^] library,

    Programming Blog utf8-cpp

    J Offline
    J Offline
    Judah Gabriel Himango
    wrote on last edited by
    #5

    Would introducing a Maybe<T> struct into the framework accomplish the same thing? Obviously, there would have to be widespread support throughout the APIs, and tons of things would break, but I'm just thinking aloud here.

    Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

    N R T 3 Replies Last reply
    0
    • J Joe Woodbury

      That still makes no sense; you still have to either always check for None or know that None may be a result of an operation. To use the cliche, it's six one way, half-dozen the other. In other words, there is still no free lunch, just overhead.

      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #6

      Joe Woodbury wrote:

      you still have to either always check for None or know that None may be a result of an operation

      No, you don't. If a method can return None, the return type will be an option. If it cannot return None, the return type will be whatever the type is and you don't need to (or can, for that matter) check for None. An example in a pseudo-language:

      // declarations
      option(string) find_string();

      string get_string();

      ...
      // usage

      string s = get_string(); can't be None - no need to check

      option(string) os = find_string(); // can be None, we'd better check
      if (os.is_None())
      report_not_found();
      else
      s = os.get_value();

      Programming Blog utf8-cpp

      J 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Would introducing a Maybe<T> struct into the framework accomplish the same thing? Obviously, there would have to be widespread support throughout the APIs, and tons of things would break, but I'm just thinking aloud here.

        Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #7

        Judah Himango wrote:

        Would introducing a Maybe struct into the framework accomplish the same thing?

        I don't think it can be introduced at this point, but that's what should have been done, IMHO.

        Programming Blog utf8-cpp

        J 1 Reply Last reply
        0
        • N Nemanja Trifunovic

          Joe Woodbury wrote:

          you still have to either always check for None or know that None may be a result of an operation

          No, you don't. If a method can return None, the return type will be an option. If it cannot return None, the return type will be whatever the type is and you don't need to (or can, for that matter) check for None. An example in a pseudo-language:

          // declarations
          option(string) find_string();

          string get_string();

          ...
          // usage

          string s = get_string(); can't be None - no need to check

          option(string) os = find_string(); // can be None, we'd better check
          if (os.is_None())
          report_not_found();
          else
          s = os.get_value();

          Programming Blog utf8-cpp

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #8

          Your example is bogus. You still HAVE to know if a return value can be None.

          string s = get_string(); can't be null - no need to check

          string os = find_string(); // can be null, we'd better check
          if (os == null)
          report_not_found();
          else
          s = os;

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          N 1 Reply Last reply
          0
          • J Joe Woodbury

            Your example is bogus. You still HAVE to know if a return value can be None.

            string s = get_string(); can't be null - no need to check

            string os = find_string(); // can be null, we'd better check
            if (os == null)
            report_not_found();
            else
            s = os;

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #9

            Joe Woodbury wrote:

            You still HAVE to know if a return value can be None.

            The return type tells me if a return value can be None.

            string s = find_string(); // compile error - the return type is option, not string

            Programming Blog utf8-cpp

            B 1 Reply Last reply
            0
            • N Nemanja Trifunovic

              Judah Himango wrote:

              Would introducing a Maybe struct into the framework accomplish the same thing?

              I don't think it can be introduced at this point, but that's what should have been done, IMHO.

              Programming Blog utf8-cpp

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #10

              Interesting. At least for future projects, that may be something to keep in mind -- introduce a Maybe<T> and use that when there might be nulls.

              Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

              1 Reply Last reply
              0
              • N Nemanja Trifunovic

                Option types[^] In ideal world that would eliminate the use of Null (which was a billion dollar mistake[^] as admitted by its inventor Tony Hoare). Unfortunatelly, the world is not ideal, and when you use types from .NET framework, Null still needs to be taken into account[^]. Backwards compatibility at its best :)

                Programming Blog utf8-cpp

                R Offline
                R Offline
                Rama Krishna Vavilala
                wrote on last edited by
                #11

                My language works better:-

                NSString* x = nil;

                [x upperCaseString]; // Never crashes

                Yes in Objective C it is OK to call methods (send messages) to nil object(s). ;P

                J 1 Reply Last reply
                0
                • N Nemanja Trifunovic

                  Option types[^] In ideal world that would eliminate the use of Null (which was a billion dollar mistake[^] as admitted by its inventor Tony Hoare). Unfortunatelly, the world is not ideal, and when you use types from .NET framework, Null still needs to be taken into account[^]. Backwards compatibility at its best :)

                  Programming Blog utf8-cpp

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #12

                  Also a C++ feature if you use Boost[^] :-) I prefer the Haskell implementation[^], though - mainly because (unlike F# and OCaml) it's not a special case builtin type, although it feels like one, because it can utilise Haskell's type classes to fit in liek a builtin type.

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    Joe Woodbury wrote:

                    You still HAVE to know if a return value can be None.

                    The return type tells me if a return value can be None.

                    string s = find_string(); // compile error - the return type is option, not string

                    Programming Blog utf8-cpp

                    B Offline
                    B Offline
                    bulg
                    wrote on last edited by
                    #13

                    so instead of looking up the return values for a function that returns a pointer, which could be null & which you should check, you state that it would be better to do what exactly?

                    N 1 Reply Last reply
                    0
                    • B bulg

                      so instead of looking up the return values for a function that returns a pointer, which could be null & which you should check, you state that it would be better to do what exactly?

                      N Offline
                      N Offline
                      Nemanja Trifunovic
                      wrote on last edited by
                      #14

                      bulg wrote:

                      so instead of looking up the return values for a function that returns a pointer, which could be null & which you should check, you state that it would be better to do what exactly?

                      Declare the function to return an option type and then if you try to assign the return value directly to a pointer without checking for null, you get a compile error.

                      Programming Blog utf8-cpp

                      1 Reply Last reply
                      0
                      • J Judah Gabriel Himango

                        Would introducing a Maybe<T> struct into the framework accomplish the same thing? Obviously, there would have to be widespread support throughout the APIs, and tons of things would break, but I'm just thinking aloud here.

                        Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                        R Offline
                        R Offline
                        Roger Wright
                        wrote on last edited by
                        #15

                        That would violate basic principles of Boolean logic, developed by George Boole, in which everything either is or isn't. Fortunately his wife, Mary, extended his work to incorporate a third state in logic, "maybe" (some texts refer to this value as "that depends"). I say 'fortunately' because, without this logical extension, it would have been impossible for women to become programmers. Limiting them to simple choices between "true" and "false" is cruel, and ultimately crippling. Programming would have been consigned to pale, nervous, socially backward, and very lonely men if George had got the final word. But what were the odds of that happenning?

                        "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                        T 1 Reply Last reply
                        0
                        • R Rama Krishna Vavilala

                          My language works better:-

                          NSString* x = nil;

                          [x upperCaseString]; // Never crashes

                          Yes in Objective C it is OK to call methods (send messages) to nil object(s). ;P

                          J Offline
                          J Offline
                          Judah Gabriel Himango
                          wrote on last edited by
                          #16

                          Heh, interesting. Do you also send messages to ask for data, e.g. var result = Foo.Bar? What happens when you ask for a result from a nil object?

                          Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                          1 Reply Last reply
                          0
                          • J Judah Gabriel Himango

                            Would introducing a Maybe<T> struct into the framework accomplish the same thing? Obviously, there would have to be widespread support throughout the APIs, and tons of things would break, but I'm just thinking aloud here.

                            Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                            T Offline
                            T Offline
                            TheGreatAndPowerfulOz
                            wrote on last edited by
                            #17

                            it's called Nullable<T> which itself is a value type and therefore cannot be null.

                            J 1 Reply Last reply
                            0
                            • R Roger Wright

                              That would violate basic principles of Boolean logic, developed by George Boole, in which everything either is or isn't. Fortunately his wife, Mary, extended his work to incorporate a third state in logic, "maybe" (some texts refer to this value as "that depends"). I say 'fortunately' because, without this logical extension, it would have been impossible for women to become programmers. Limiting them to simple choices between "true" and "false" is cruel, and ultimately crippling. Programming would have been consigned to pale, nervous, socially backward, and very lonely men if George had got the final word. But what were the odds of that happenning?

                              "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                              T Offline
                              T Offline
                              TheGreatAndPowerfulOz
                              wrote on last edited by
                              #18

                              Roger Wright wrote:

                              But what were the odds of that happenning?

                              one would say 50%, given the possible boolean logic results. obviously it was 0%.

                              R 1 Reply Last reply
                              0
                              • T TheGreatAndPowerfulOz

                                it's called Nullable<T> which itself is a value type and therefore cannot be null.

                                J Offline
                                J Offline
                                Judah Gabriel Himango
                                wrote on last edited by
                                #19

                                Doesn't work for this situation. Nullable<T> only works with value types; the only reason we'd introduce Maybe<T> is for reference types.

                                Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                                1 Reply Last reply
                                0
                                • T TheGreatAndPowerfulOz

                                  Roger Wright wrote:

                                  But what were the odds of that happenning?

                                  one would say 50%, given the possible boolean logic results. obviously it was 0%.

                                  R Offline
                                  R Offline
                                  Roger Wright
                                  wrote on last edited by
                                  #20

                                  That depends... ;P

                                  "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups