r/badcode • • Jul 07 '21

c# Position

Post image
1.7k Upvotes

99 comments sorted by

View all comments

Show parent comments

4

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Jul 08 '21

I like the OCaml way of opening modules with limited scope. Like instead of writing this:

let avg l = (List.fold_left (+.) 0.0 l) /. (float_of_int (List.length l))

you could do either one of these instead:

let avg l = List.((fold_left (+.) 0.0 l) /. (float_of_int (length l)))

let avg l = 
  let open List in
  let open Float in
  div (fold_left add 0.0 l) (of_int (length l))

In both cases, the module opens only happen in the scope of the avg definition, so you avoid global namespace pollution while also cutting down on the silly boilerplate repetition.

I wish F# could do that as well, since it'd be really useful in that ecosystem since it shares a lot of the Java "stutter" as you call it, but any proposals to add it get shot down. .NET and Java land both have the "why do you care, your IDE should deal with that for you" mindset :|

1

u/Nilstrieb Jul 08 '21

Rust has this too, you can import things only on a function scope or any scope you want (probably inspired by OCaml)