News for hackers. Stuff that matters.
Re:Fantasy World OCaml (2013) by rwmj on Sunday August 03, 2014 @06:30AM (#8127590)
I would like to see a 'return' statement added to the language, so that:

    fun () ->
      if foo then return r;
      more code ...
is the equivalent of:

    fun () ->
      if foo then r
      else more code ...
Note that type safety would obviously have to be preserved. The returned type would have to match the return type of the overall function.
Re:Fantasy World OCaml (2013) by tel on Sunday August 03, 2014 @11:08AM (#8128108)
I think this is pretty intentionally avoided most of the time since it clutters up the continuation-passing semantics of the code... With `return` you have a much more complex base language. Especially since you can achieve it using monads.
Re:Fantasy World OCaml (2013) by rwmj on Sunday August 03, 2014 @01:00PM (#8128418)
Monads don't (quite) short-circuit the later computations unlike a return statement. Anyway, I'd like to see an example of writing this using monads, since it makes the idea more concrete.
Re:Fantasy World OCaml (2013) by tel on Sunday August 03, 2014 @03:01PM (#8128782)
I can write a continuation monad in Haskell which would. Something like

    {-# LANGUAGE DeriveFunctor #-}

    module Return where

    import Control.Monad

    newtype Cont r a = Cont { runCont :: (a -> r) -> r } deriving Functor

    instance Monad (Cont r) where
      return a = Cont (\k -> k a)
      m >>= f  = Cont (\k -> runCont m (\a -> runCont (f a) k))

    early :: r -> Cont r a
    early a = Cont (\_ -> a)

    ex :: Cont Integer [Integer]
    ex = do
      forM [1..] $ \i -> do
        when (i == 33) (early i)
        return i
Re:Fantasy World OCaml (2013) by Peaker on Sunday August 03, 2014 @03:26PM (#8128860)
Why Cont when Either would do?
Re:Fantasy World OCaml (2013) by tel on Sunday August 03, 2014 @03:36PM (#8128888)
True, but I mentioned continuation-passing semantics! Peaker's right though—and Either might be a little easier to understand.
Re:Fantasy World OCaml (2013) by theaeolist on Sunday August 03, 2014 @08:07AM (#8127719)
Why?
Re:Fantasy World OCaml (2013) by rwmj on Sunday August 03, 2014 @08:47AM (#8127779)
Because it would make a lot of code less nested and simpler to understand. The most common case is:

    if some_error_condition then
      None
    else (
      // everything else in the function is nested
    )
which would be replaced with:

    if some_error_condition then
      return None;
    // everything else is not nested
The situation is particularly bad when you have to do a lot of small tests at the beginning of your function. Think permissions checks:

    let user_id =
      match user with Some id -> id
      | None ->
        printf "you need to specify a user\n";
        return None in
    if not (permitted user_id) then
      printf "this user is not permitted\n";
      return None;
    // go ahead and return Some object
The alternative is to use exceptions, either hidden within the function (requiring a nested function), or demanding that the caller is careful about catching exceptions. Exceptions are an unsafe back door, when what's really needed is a return statement.
Re:Fantasy World OCaml (2013) by Peaker on Sunday August 03, 2014 @03:28PM (#8128865)
Original code:

  if some_error_condition then
        None
      else (
        // everything else in the function is nested
      )
Why not use a Maybe/Either monad like:

  x <- some_maybe_value
  // everything else in the function isn't nested, and "x" is available (if some_maybe_value was Just)
Kind of like exceptions, but a bit more explicit.
Re:Fantasy World OCaml (2013) by rwmj on Sunday August 03, 2014 @04:47PM (#8129134)
I don't understand your example. I don't want to run the "// everything else" bit if the error condition was true.
Re:Fantasy World OCaml (2013) by Peaker on Monday August 04, 2014 @01:38AM (#8130503)
The error condition is represented by Nothing ("None" in OCaml?) and the rest of the code only runs if it was Just ("Some" in OCaml?).
Re:Fantasy World OCaml (2013) by talex5 on Sunday August 03, 2014 @11:10AM (#8128117)
A style I sometimes see is people not indenting the normal code path of the match. So your example above would be written:

    match user with
    | None -> printf "you need to specify a user\n"; None
    | Some user_id when not (permitted user_id) ->
	printf "this user is not permitted\n"; None
    | Some user_id ->
    // go ahead and return Some object
Re:Fantasy World OCaml (2013) by rwmj on Sunday August 03, 2014 @12:58PM (#8128414)
Stay classy, downvoters. You know I've been a working OCaml programmer for the last 12 years, using the language most days, so I do know a bit about the pain points.
Re:Fantasy World OCaml (2013) by renox on Monday August 04, 2014 @10:47AM (#8131953)
Some people hates the 'return' keyword.. Suggest a '~' operator (like the ^ operator of Smalltalk) and your proposal will be better received ;-)