Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6143961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:35:16+00:00 2026-05-23T18:35:16+00:00

I have a simple function that prints a string and exits: let fatal s

  • 0

I have a simple function that prints a string and exits:

let fatal s = 
  print_string "Log: ";
  print_endline s;
  exit 1

I can use printf to do something similar without the exit 1:

let log fmt = 
  printf ("Log: " ^^ fmt)

This log function takes a format string and returns a function that takes the parameters needed for that format string and prints “Log: ” in front. (Of course my prefix isn’t this simple for my real application.)

Taking these two and combining them is not easy. A first attempt:

let fatalf fmt =
   Printf.printf ("Log: " ^^ fmt) 
   ???
   exit 1

The problem is that I have to return the result of my printf expression so that the remaining arguments can be applied to it. Once I’ve returned this value, I don’t have flow control anymore to run exit.

The printf formatter %t looks useful, as it takes a function and runs it:

printf ("Log: " ^^ fmt ^^ "%!%t") ... (fun _ -> exit 1)

This doesn’t seem to work as the %t must be last so it’s run after the log message is written, but this means that the exit function must be after user-specified parameters, and since there’s no way to know how many parameters there will be intervening, one can’t generate a closure that does the full application of printf when given the intervening arguments.

I recall there being some support for named printf arguments, but that this was pulled as it was buggy. Is there any way to emulate that, or to achieve the desired “exit after arbitrary printf” behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:35:17+00:00Added an answer on May 23, 2026 at 6:35 pm

    You’re looking for Printf.kprintf:

    let fatalf fmt =
      Printf.kprintf (fun str ->
        Printf.eprintf "Fatal error: %s !\n%!" str;
        exit 1) fmt
    

    kprintf takes a continuation with type string -> 'a and applies it to the result of sprintf-ing with the supplied format. The result of the continuation is the result of the entire call, as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function that returns an NSString after decoding it. I use
I have a simple log function that needs to print the current date and
I have a simple function that I want to call in the code behind
I have a very simple function that takes a list of comma separated (x,y)
I have a simple recursive function RCompare() that calls a more complex function Compare()
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a simple function that sorts a dictionary: data = inputfile.readlines() lineData =
I was trying to implement a simple function that can concatenate any number of
I have a simple string that I'm trying to manipulate: Your order will be
I have a simple function that passes a variable var as a u_char array.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.