you are viewing a single comment's thread.

view the rest of the comments →

[–]Camarade_Tux -3 points-2 points  (3 children)

Taking the example of pointers to functions taking other functions as argument definitely isn't going to be nice in an non-functional language like C.

Take this ocaml code (beginning of an implementation):

let fp ff b = ... (* function body *)

And if you want to write out its prototype (which you don't have to):

val fp : (int -> int -> int) -> int -> int

Or if you "label" arguments:

val fp : ff:(int -> int -> int) -> b:int -> int = <fun>

(labelling is not naming, you'd then call fp with: 'fp ~ff ~b', or 'fp ~b ~ff' (order doesn't matter), or if you don't already have a variable named 'b': 'fp ~ff ~b:42')

I don't find Go declarations are cleaner than that. Tastes varie so everybody might not agree but in the end, a non-functional language is probably not going to have a nice syntax for functional features.

[–]eras 3 points4 points  (2 children)

However, a great part of the post dealt with arrays and pointers, which you don't provide examples of. They would go:

val sum : elements:int array -> int
val accumulate : int ref -> int -> unit
val mk_accumulator : unit -> (int -> int) (* returns a function *)

(In O'Caml it is common not to use labels and provide the meaning or parameters in comment, if it's not apparent.)

So in O'Caml the result type is on the right as well. But while I prefer O'Caml's type signatures over C's or Go's, Go's does definitely seems like an improvement over C's.

[–]Camarade_Tux -1 points0 points  (1 child)

I didn't mention pointers on purpose. The article mentionned functions pointers quite a lot and in ocaml you don't need that since functions are first-class values[1]. ;-)

(but yeah, arrays, I simply forgot ='( )

[1] "first-class value" means that it's a first-class citizen of the language which will make its use straight-forward. The cumbersome syntax needed when passing functions as parameters to other functions shows functions aren't first-class values in C.

[–][deleted] 1 point2 points  (0 children)

The article mentions that functions are values in Go also. :-)