From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id EE9707EE25 for ; Thu, 7 Nov 2013 08:34:29 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.214.42; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.214.42 as permitted sender) identity=mailfrom; client-ip=209.85.214.42; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-bk0-f42.google.com) identity=helo; client-ip=209.85.214.42; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-bk0-f42.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aj0DAC5Ce1LRVdYqlGdsb2JhbABZgz9TrDaSVoEbCBYOAQEBAQcLCwkSKoIlAQEEAScZARsSCwEDAQsGBQsNDSEhAQERAQUBChIGEwgKh1wBAwkGDZ83jFeDCYQkChknAwpkiQEBBQyMW4JyB4QwA5YhgWuBL4sjg0wYKYRSOw X-IPAS-Result: Aj0DAC5Ce1LRVdYqlGdsb2JhbABZgz9TrDaSVoEbCBYOAQEBAQcLCwkSKoIlAQEEAScZARsSCwEDAQsGBQsNDSEhAQERAQUBChIGEwgKh1wBAwkGDZ83jFeDCYQkChknAwpkiQEBBQyMW4JyB4QwA5YhgWuBL4sjg0wYKYRSOw X-IronPort-AV: E=Sophos;i="4.93,650,1378850400"; d="scan'208";a="41390115" Received: from mail-bk0-f42.google.com ([209.85.214.42]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 07 Nov 2013 08:33:50 +0100 Received: by mail-bk0-f42.google.com with SMTP id w16so64641bkz.15 for ; Wed, 06 Nov 2013 23:34:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=Hu9koVL2RmJO0fTZ2QewQdzxYXxUI3MTbIlLkcnMNNw=; b=R09IZExf9ybski4IiEZsQ8chaDeGxpOxllQDi6TLDFmw52Y8PzBUitIRb+ZpMPlr5Q RHGI0YRLzUQFcFRpJgE+hsatZ3p3tQxN/+r0SJet3woVaUiEySLlablWNkGqlLiTx8tK Is7eEez3ojprm1Eo+HQHQEXRdeA4QcODoVJ7fA/CrQT7xLD9IZeoYL/qmbzCk2olGEdI wKkCDbJMQiWRzAs3XD8DPzPcILRCNmcqpAhP66ceEN92lj9L9pRbZnfqtEPMaqXw+8Jr R4VhXsWVoqJl+ONQDJk1u7HxuC/vmAlTLeV/AEpu0u25F3Oi1XkItvtX+4kva4R3SrzU lmwg== X-Received: by 10.204.168.132 with SMTP id u4mr5005036bky.28.1383809669250; Wed, 06 Nov 2013 23:34:29 -0800 (PST) MIME-Version: 1.0 Received: by 10.204.122.72 with HTTP; Wed, 6 Nov 2013 23:33:49 -0800 (PST) In-Reply-To: <1383742480351@names.co.uk> References: <1383742480351@names.co.uk> From: Gabriel Scherer Date: Thu, 7 Nov 2013 08:33:49 +0100 Message-ID: To: Mark Adams Cc: caml users Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] is there a way for turning strings to "format"s? > There's always the nasty 'Obj.magic' for conversion between arbitrary types: > > let computed_format = Printf.sprintf "%%0%dd" 5 in > Printf.printf (Obj.magic computed_format) 42 This is terribly wrong. Not only does it remove any form of static typing on today's version of OCaml, it is also assured to segfault as soon as the format representation changes (which may happen in a close future). Do not use Obj.magic. Matej: Deciding the number of decimal points or 0-padding dynamically is one of those eccentric features that are in fact already part of format. # Printf.printf "%0*d" 6 2;; 000002- : unit = () Format will be your friend as long as you stay inside the things they directly support -- look at the documentation carefully to discover some surprisingly expressive features. If you ever step outside this zone, the temptation is great to hack your way back in, but that will only lead to trouble (consider simply using OCaml code to implement your printing logic then). Do not, ever, use Obj.magic on formatting functions. On Wed, Nov 6, 2013 at 1:54 PM, "Mark Adams" wrote: > There's always the nasty 'Obj.magic' for conversion between arbitrary types: > > let computed_format = Printf.sprintf "%%0%dd" 5 in > Printf.printf (Obj.magic computed_format) 42 > > Surely there must be a nicer way of doing this, but I don't know of one. > > Mark. > > on 6/11/13 12:40 PM, Matej Kosik <5764c029b688c1c0d24a2e97cd764f@gmail.com> > wrote: > >> Hi, >> >> I would like to ask, how to do something like: >> >> let computed_format = Printf.sprintf "%%0%dd" 5 in >> Printf.printf computed_format 42 >> >> The above code is rejected, because "computed_format" is of type "string" >> whereas >> >> ('a -> 'b, out_channel, unit) format = >> ('a -> 'b, out_channel, unit, unit, unit, unit) format6 >> >> type was expected. >> >> My question is: is it possible to turn strings to values that would be >> accepted by Printf.printf as its first parameter? >> >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa.inria.fr/sympa/arc/caml-list >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs >> >> >> > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs