+ 1 -

language design control flow error handling syntax

Exceptional syntax

Nick Benton Andrew Kennedy (2001)

From the points of view of programming pragmatics, rewriting and operational semantics, the syntactic construct used for exception handling in ML-like programming languages, and in much theoretical work on exceptions, has subtly undesirable features. We propose and discuss a more well-behaved construct.

Lire maintenant ?

gasche gasche

L’idée principale est de combiner les deux syntaxes let x = foo in bar et try foo with baz en une syntaxe commune let-try x = foo with baz in bar, ayant la sémantique suivante: si foo renvoie une erreur, on la passe à baz et on renvoie le résultat (ou on la propage), et sinon on évalue bar qui peut contenir la variable x.

Ça permet de simplifier et de rendre plus efficace l’exemple classique:


let rec read_all acc =
  let result =
    try Some (read_line ())
    with End_of_file -> None in
   match result with
   | None -> List.rev acc
   | Some line -> read_all (line :: acc)

en


let rec read_lines acc =
  let-try line = read_line ()
  with End_of_file -> List.rev acc
  in read_all (line :: acc)

Inscrivez-vous pour participer à la discussion.