Computer Science Canada

Things I miss since learning O'Caml

Author:  wtd [ Tue Aug 22, 2006 7:35 pm ]
Post subject:  Things I miss since learning O'Caml

I learned O'Caml, but I miss some things about Java/C++...


  • I miss braces.

    I miss having to enclose all of my functions and method bodies in braces, even if they're only composed of single expressions.

    code:
    void foo(int bar)
    {
       printf("%d\n", bar);
    }

    ------------------------------

    let foo bar =
       printf "%d\n" bar


  • I miss "return."

    I miss having to use return, even with really simple functions and methods.

    code:
    int foo(int bar)
    {
       return bar + 1;
    }

    ------------------------------

    let foo bar = bar + 1

  • I miss preprocessor guards.

    I miss having to use the preprocessor to protect against multiple inclusion of header files. O'Caml's module system is no replacement at all.

    code:
    #ifndef __FOO_H
    #define __FOO_H

    void say_hello();

    #endif

    ------------------------------

    module Foo = struct
       let say_hello = print_endline "Hello!"
    end

  • I miss control statements.

    I liked having to create a variable, then assign to it within a control structure. Having control structures that return values just messes with my mind, and I don't like my programming languages trying to outsmart me. I should be the one in control.

    code:
    public class Test {
       public static void main(String[] args) {
          int a;
              
              if (args.length > 0) {
                 a = Integer.parseInt(args[0]);
              } else {
                 a = 0;
              }
              
              System.out.println(a);
       }
    }

    ------------------------------

    let a = if Array.length Sys.argv > 0 then
       int_of_string Sys.argv.(0)
    else 0;;
    Printf.printf "%d\n" a

  • I miss getting away with incomplete conditionals.

    O'Caml actually refuses to compile my code if I've not handled some possibility in my conditionals. It's incredibly frustrating. I just want my code to compile. I don't care if I've missed an obvious source of error.

    I'll find out about it when my code produces weird runtime errors. They make for interesting challenges.

    code:
    public class Test {
       public static void main(String[] args) {
          int a;
              
              if (args.length > 0) {
                 a = Integer.parseInt(args[0]);
              }
              
              System.out.println(a);
       }
    }

    ------------------------------

    (* Doesn't compile. *)
    let a = if Array.length Sys.argv > 0 then
       int_of_string Sys.argv.(0);;
    Printf.printf "%d\n" a

  • I miss explicit types.

    I miss writing out the types of variables, return values, and parameters. It gave me the chance to essentially document my code. And no, I do not buy the explanation that you can load up a module in the interpreter and quickly find out the types involved.
  • I miss non-named parameters.

    I liked having to remember the order function parameters occur in. It was a great sense of achievement. Yes, O'Caml has positional parameters, but it doesn't force me to use them, sadly.
  • I miss contructors.

    I miss having to explicitly set up the initial state of objects. O'Caml's method of doing this is just too easy for the amount of work being done. I dislike it when the power of code, and the difficulty in writing it are not proportional.

    code:
    class Foo {
       private String a, b;
       
       public Foo(String a, String b) {
          this.a = a;
              this.b = b;
       }
       
       public String toString() {
          return a + " " + b;
       }
    }

    ------------------------------

    class foo a b = object
       val mutable a = a
       val mutable b = b
       method to_string = a ^ " " ^ b
    end

  • I miss null.

    I miss having to wonder if a parameter is actually an object at all.

Author:  [Gandalf] [ Tue Aug 22, 2006 8:16 pm ]
Post subject: 

Lies, I say!

No, but it's an interesting comparison. If only O'Caml wasn't so brain melting. Sad

Author:  wtd [ Tue Aug 22, 2006 9:22 pm ]
Post subject: 

[Gandalf] wrote:
Lies, I say!

No, but it's an interesting comparison. If only O'Caml wasn't so brain melting. Sad


How so?

Author:  OneOffDriveByPoster [ Wed Aug 23, 2006 8:30 pm ]
Post subject:  Re: Things I miss since learning O'Caml

wtd wrote:
I miss control statements.

I liked having to create a variable, then assign to it within a control structure. Having control structures that return values just messes with my mind, and I don't like my programming languages trying to outsmart me. I should be the one in control.

code:
public class Test {
   public static void main(String[] args) {
      int a;
          
          if (args.length > 0) {
             a = Integer.parseInt(args[0]);
          } else {
             a = 0;
          }
          
          System.out.println(a);
   }
}

------------------------------

let a = if Array.length Sys.argv > 0 then
   int_of_string Sys.argv.(0)
else 0;;
Printf.printf "%d\n" a


Let's have something else...

Java:
public class Test {
    public static void main(String[] args) {
        int a = args.length > 0 ? Integer.parseInt(args[0]) : 0;
        System.out.println(a);
    }
}

Author:  md [ Wed Aug 23, 2006 9:42 pm ]
Post subject: 

Methinks people are missing the point... this was sarcastic...

Though I do like the idea of return. Explicitly returning something is nice... what if I want to add one to bar, and do nothing else, but not return bar+1? This could be the case where code is commented out to test something else...

Author:  [Gandalf] [ Wed Aug 23, 2006 11:17 pm ]
Post subject: 

Cornflake wrote:
this was sarcastic...

Yes...

wtd wrote:
How so?

Mostly because my brain is frozen in imperative programming mode. It's a slow process to change.

Author:  wtd [ Thu Aug 24, 2006 12:44 am ]
Post subject: 

Cornflake wrote:
what if I want to add one to bar, and do nothing else, but not return bar+1? This could be the case where code is commented out to test something else...


You have two problems there.

In one case, you're returning void. In another, you're returning int. O'Caml is statically-typed, so this is no good.

The other problem is that you're counting on bar being mutable. Values in O'Caml are by default immutable. This leads to more optimization, and not less, by the way. Smile

Author:  wtd [ Thu Aug 24, 2006 12:46 am ]
Post subject:  Re: Things I miss since learning O'Caml

OneOffDriveByPoster wrote:
Let's have something else...

Java:
public class Test {
    public static void main(String[] args) {
        int a = args.length > 0 ? Integer.parseInt(args[0]) : 0;
        System.out.println(a);
    }
}


There's that, but it doesn't scale well. And why have two different constructs for the same concept? Java is supposed to be about not reinventing the wheel, right? Wink

Author:  Cervantes [ Thu Aug 24, 2006 7:50 am ]
Post subject: 

It seems odd that if the ternary operator can return a value, why can't an if-construct?

Author:  OneOffDriveByPoster [ Fri Aug 25, 2006 9:10 am ]
Post subject:  Re: Things I miss since learning O'Caml

wtd wrote:
I miss getting away with incomplete conditionals.

O'Caml actually refuses to compile my code if I've not handled some possibility in my conditionals. It's incredibly frustrating. I just want my code to compile. I don't care if I've missed an obvious source of error.

I'll find out about it when my code produces weird runtime errors. They make for interesting challenges.

code:
public class Test {
   public static void main(String[] args) {
      int a;
          
          if (args.length > 0) {
             a = Integer.parseInt(args[0]);
          }
          
          System.out.println(a);
   }
}

------------------------------

(* Doesn't compile. *)
let a = if Array.length Sys.argv > 0 then
   int_of_string Sys.argv.(0);;
Printf.printf "%d\n" a


code:
Test.java:9: variable a might not have been initialized
        System.out.println(a);
                           ^
1 error

Author:  wtd [ Fri Aug 25, 2006 10:14 am ]
Post subject: 

Thanks for pointing that out.

Author:  rizzix [ Fri Aug 25, 2006 3:31 pm ]
Post subject: 

Cervantes wrote:
It seems odd that if the ternary operator can return a value, why can't an if-construct?


If constructs are statements in Java, whereas the ternary operator is an expression.

Unlike ruby blocks/statements in Java can not return values, unless they are defined to do so (and if they are, you must explictly return a value).

Author:  wtd [ Fri Aug 25, 2006 3:39 pm ]
Post subject: 

rizzix wrote:
Cervantes wrote:
It seems odd that if the ternary operator can return a value, why can't an if-construct?


If constructs are statements in Java, whereas the ternary operator is an expression.

Unlike ruby blocks/statements in Java can not return values, unless they are defined to do so (and if they are, you must explictly return a value).


Oh, I think we're all pretty well aware that is how Java works. Cervantes' question was not "how" but rather "why?"

Author:  Cervantes [ Fri Aug 25, 2006 3:43 pm ]
Post subject: 

Indeed. I understand that Java's if constructs are statements and not expressions. I'm just saying, y'know, it sucks. Wink

Author:  rizzix [ Fri Aug 25, 2006 3:43 pm ]
Post subject: 

Java likes to make a clear distinction between expressions and statements? gee..

Author:  wtd [ Fri Aug 25, 2006 3:46 pm ]
Post subject: 

An unnecessary dichotomy.

Why is there a need for "statements" at all? Certainly expressions that return "unit" or something comparable provide all the same functionality.

Author:  rizzix [ Fri Aug 25, 2006 3:52 pm ]
Post subject: 

It's more intuitive.
In a natural language, you have words which compose clauses/phrases which compose statements which compose paragraphs, which finally composes an article.
Similarly, in a strucured language (like C/Java) we have expressions which compose statements which compose blocks, which finally composes a program.

Author:  Cervantes [ Fri Aug 25, 2006 4:04 pm ]
Post subject: 

Granted, statements might be a little more intuitive. But I think it's a pretty poor argument when you consider the power and flexibility that comes with embracing expressions over statements.


: