caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] writing some code using a function for which only the signature is known
@ 2011-01-21 13:49 mark
  2011-01-21 15:20 ` Daniel Bünzli
  0 siblings, 1 reply; 8+ messages in thread
From: mark @ 2011-01-21 13:49 UTC (permalink / raw)
  To: caml-list

For anything more than toy examples, it's better to use Dmitry's solution of
a specially defined exception, because you certainly don't want the
exception to get caught before it reaches the top level, but Assert_failure
or Failure are liable to get caught if you write code with exception
handling.

This is assuming, of course, that you're not using nasty catch-all exception
handling in your code.

Mark.

on 21/1/11 5:41 AM, Francois Berenger <berenger@riken.jp> wrote:

> Hello,
>
> If I am writing some code, and I don't want to dive
> into implementing some sub function I will need but don't
> have yet, what is the standard way to do this in ocaml?
>
> In python, there is the pass keyword, Haskell has some
> keyword which I don't remember for this also.
>
> Sorry for the dumb question maybe, I am returning to ocaml
> after a too long absence. :)
>
> Regards,
> Francois.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] writing some code using a function for which only the signature is known
  2011-01-21 13:49 [Caml-list] writing some code using a function for which only the signature is known mark
@ 2011-01-21 15:20 ` Daniel Bünzli
  0 siblings, 0 replies; 8+ messages in thread
From: Daniel Bünzli @ 2011-01-21 15:20 UTC (permalink / raw)
  To: caml-list

On Fri, Jan 21, 2011 at 14:49,  <mark@proof-technologies.com> wrote:

> For anything more than toy examples, it's better to use Dmitry's solution of
> a specially defined exception, because you certainly don't want the
> exception to get caught before it reaches the top level, but Assert_failure
> or Failure are liable to get caught if you write code with exception
> handling.

In that case simply use invalid_arg "TODO", Invalid_argument
exceptions should never be catched. This avoids having to define your
own exception.

Best,

Daniel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] writing some code using a function for which only the signature is known
  2011-01-21 15:32     ` oliver
@ 2011-01-21 18:26       ` dmitry grebeniuk
  0 siblings, 0 replies; 8+ messages in thread
From: dmitry grebeniuk @ 2011-01-21 18:26 UTC (permalink / raw)
  To: caml-list

Hello.

>> let myfunc _myparam1 _myparam2 = notimpl "myfunc"

> In the case of non implemented functions I would prefer to see the
> raisement of an exception in the code, and not covering it with
> a wrapper function.

  It's a matter of taste.  The function with name "notimpl"
or "not_implemented" doesn't affect readability, as for me.
Only convenience and flexibility (for example, I can change
the body of "notimpl" function to make the action depend
on the name of undefined function; of course, the last
step will be the same: aborting computations, either
raising exception or aborting the program with exit()).

>>  Notice the first underscores in arguments' names: they
>> are here to suppress warnings about unused bindings
>> (really, these bindings are unused in this function).

> Beginning underlines do suppress warning messages?

  Yes, if you have this warning enabled.  Nice feature:
you can enable this warning to catch logic errors in
the code, and ignore unused bindings you have specified
manually.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] writing some code using a function for which only the signature is known
  2011-01-21  6:12   ` dmitry grebeniuk
@ 2011-01-21 15:32     ` oliver
  2011-01-21 18:26       ` dmitry grebeniuk
  0 siblings, 1 reply; 8+ messages in thread
From: oliver @ 2011-01-21 15:32 UTC (permalink / raw)
  To: caml-list

On Fri, Jan 21, 2011 at 08:12:26AM +0200, dmitry grebeniuk wrote:
> Hello.
> 
> (sorry for dupe, I've sent first message privately, not in mailing
> list; and I've fixed some code here.)
> 
> > If I am writing some code, and I don't want to dive
> > into implementing some sub function I will need but don't
> > have yet, what is the standard way to do this in ocaml?
> 
>  I don't know any standart way, but I'm using "raise Exit"
> for this purpose.
> 
>  Or you can use exception that carry function's name,
> like this:
> 
> exception Not_implemented of string
> let notimpl funcname = raise (Not_implemented funcname)
> let myfunc _myparam1 _myparam2 = notimpl "myfunc"
[...]

Ah, now I understand what the problem of the OP was.

Why do you use the step with a seperated function that raises the exception?
For convenience only?

In the case of non implemented functions I would prefer to see the
raisement of an exception in the code, and not covering it with
a wrapper function.

[...]
>  Notice the first underscores in arguments' names: they
> are here to suppress warnings about unused bindings
> (really, these bindings are unused in this function).
[...]

Beginning underlines do suppress warning messages?


Ciao,
   Oliver

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] writing some code using a function for which only the signature is known
  2011-01-21  5:40 Francois Berenger
  2011-01-21  5:58 ` oliver
       [not found] ` <AANLkTikv07AgCJXKD_e_QvGGz5245svFGiTG85fpZC7P@mail.gmail.com>
@ 2011-01-21 10:56 ` Arlen Cuss
  2 siblings, 0 replies; 8+ messages in thread
From: Arlen Cuss @ 2011-01-21 10:56 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 990 bytes --]

On Fri, 2011-01-21 at 14:40 +0900, Francois Berenger wrote:
> Hello,
> 
> If I am writing some code, and I don't want to dive
> into implementing some sub function I will need but don't
> have yet, what is the standard way to do this in ocaml?
> 
> In python, there is the pass keyword, Haskell has some
> keyword which I don't remember for this also.
> 
> Sorry for the dumb question maybe, I am returning to ocaml
> after a too long absence. :)
> 
> Regards,
> Francois.
> 

I tend to use failwith (type is string -> 'a); it just raises Failure
with your string, so..

# let my_fun par1 par2 par3 =
    failwith "myfun not implemented";;
val my_fun : 'a -> 'b -> 'c -> 'd = <fun>
# let sub_fun = function
   | true -> 42
   | false -> my_fun 92 [1,2,3] 'x';;
val sub_fun : bool -> int = <fun>
# sub_fun true;;
- : int = 42
# sub_fun false;;
Exception: Failure "myfun not implemented".
# 

It type checks anything because it raises an exception.

Arlen

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] writing some code using a function for which only the signature is known
       [not found] ` <AANLkTikv07AgCJXKD_e_QvGGz5245svFGiTG85fpZC7P@mail.gmail.com>
@ 2011-01-21  6:12   ` dmitry grebeniuk
  2011-01-21 15:32     ` oliver
  0 siblings, 1 reply; 8+ messages in thread
From: dmitry grebeniuk @ 2011-01-21  6:12 UTC (permalink / raw)
  To: caml-list

Hello.

(sorry for dupe, I've sent first message privately, not in mailing
list; and I've fixed some code here.)

> If I am writing some code, and I don't want to dive
> into implementing some sub function I will need but don't
> have yet, what is the standard way to do this in ocaml?

 I don't know any standart way, but I'm using "raise Exit"
for this purpose.

 Or you can use exception that carry function's name,
like this:

exception Not_implemented of string
let notimpl funcname = raise (Not_implemented funcname)
let myfunc _myparam1 _myparam2 = notimpl "myfunc"

  And there are some cases when you need to specify
the type of function (either "you need", or "it
will ease your life substantially"), even if the function
is not implemented.

 Moreover, it's better to describe arguments of such function
(to document the code, to add type constraints, to ease
implementation of the function later (either full or partial)):

let  process_them  _len  _contents  _on_finish  =  raise Exit

 Notice the first underscores in arguments' names: they
are here to suppress warnings about unused bindings
(really, these bindings are unused in this function).

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] writing some code using a function for which only the signature is known
  2011-01-21  5:40 Francois Berenger
@ 2011-01-21  5:58 ` oliver
       [not found] ` <AANLkTikv07AgCJXKD_e_QvGGz5245svFGiTG85fpZC7P@mail.gmail.com>
  2011-01-21 10:56 ` Arlen Cuss
  2 siblings, 0 replies; 8+ messages in thread
From: oliver @ 2011-01-21  5:58 UTC (permalink / raw)
  To: caml-list

Hi,

On Fri, Jan 21, 2011 at 02:40:26PM +0900, Francois Berenger wrote:
> Hello,
> 
> If I am writing some code, and I don't want to dive
> into implementing some sub function I will need but don't
> have yet, what is the standard way to do this in ocaml?
[...]


Maybe it's too late, or it was too much wine ;)
but I don't understand waht you are talking about.


Can you be more verbose/elaborate or give an example (maybe
in Python or Haskell) on what you want to achieve?


Your subject also seems not to match the text of your mailbody...


...if you want just to write a function that fit's into
a certain signature, it can be a lot or nothing.

The signature defines the interface.
The functionality that you want to implement
can be even more complex, but what gets in and what gets out
is in the interface.

So: what do you want  to know?

(Maybe other readers understand you?)


Ciao,
   Oliver

P.S.: OK, I just looked up the "pass" command from Python.
      It seems just to be something that does nothing,
      but is sytactically needed, so the compiler does not complain.

      I OCaml, where type checking is a crucial topic,
      you can insert dummies like giving back unit ( () )
      or empty strings( "" ) or so, if you need to do that
      just to match the type.

      Or in some cases, option type can help you here.

      But, as mentioned, a question that is more concrete explaining your problem
      would be nice.

      Maybe you just can post some code you tried and did not get running?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Caml-list] writing some code using a function for which only the signature is known
@ 2011-01-21  5:40 Francois Berenger
  2011-01-21  5:58 ` oliver
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Francois Berenger @ 2011-01-21  5:40 UTC (permalink / raw)
  To: caml-list

Hello,

If I am writing some code, and I don't want to dive
into implementing some sub function I will need but don't
have yet, what is the standard way to do this in ocaml?

In python, there is the pass keyword, Haskell has some
keyword which I don't remember for this also.

Sorry for the dumb question maybe, I am returning to ocaml
after a too long absence. :)

Regards,
Francois.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-01-21 18:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-21 13:49 [Caml-list] writing some code using a function for which only the signature is known mark
2011-01-21 15:20 ` Daniel Bünzli
  -- strict thread matches above, loose matches on Subject: below --
2011-01-21  5:40 Francois Berenger
2011-01-21  5:58 ` oliver
     [not found] ` <AANLkTikv07AgCJXKD_e_QvGGz5245svFGiTG85fpZC7P@mail.gmail.com>
2011-01-21  6:12   ` dmitry grebeniuk
2011-01-21 15:32     ` oliver
2011-01-21 18:26       ` dmitry grebeniuk
2011-01-21 10:56 ` Arlen Cuss

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).