caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] function overloading
@ 2003-11-05  8:57 Dustin Sallings
  2003-11-05 10:13 ` Issac Trotts
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Dustin Sallings @ 2003-11-05  8:57 UTC (permalink / raw)
  To: Caml Mailing List


	I swear I found an example demonstrating how to do pattern matching 
based on the type of an argument, but every pattern matching example I 
can find now seems to be focused on value and not type.

	I'm trying to do something like this:

let print_poly x = match x with
     | (x: bool) -> print_bool x
     | (x: int) ->  print_int x
;;

	Can someone tell me what I'm doing wrong (or perhaps tell me why I 
shouldn't be doing this at all)?

-- 
Dustin Sallings

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] function overloading
  2003-11-05  8:57 [Caml-list] function overloading Dustin Sallings
@ 2003-11-05 10:13 ` Issac Trotts
  2003-11-05 17:51   ` Dustin Sallings
  2003-11-05 10:39 ` Julien Demouth
  2003-11-07  7:05 ` skaller
  2 siblings, 1 reply; 5+ messages in thread
From: Issac Trotts @ 2003-11-05 10:13 UTC (permalink / raw)
  To: caml-list

On Wed, Nov 05, 2003 at 12:57:18AM -0800, Dustin Sallings wrote:
> 
> 	I swear I found an example demonstrating how to do pattern matching 
> based on the type of an argument, but every pattern matching example I 
> can find now seems to be focused on value and not type.
> 
> 	I'm trying to do something like this:
> 
> let print_poly x = match x with
>     | (x: bool) -> print_bool x
>     | (x: int) ->  print_int x
> ;;
> 
> 	Can someone tell me what I'm doing wrong (or perhaps tell me why I 
> shouldn't be doing this at all)?

You can do this:

type bool_or_int = Bool of bool | Int of int;;

let print_bool b = print_string (if b then "true" else "false");;

let print_poly x = match x with 
    Bool b -> print_bool b
  | Int i -> print_int i;;

let x = Bool true;;

print_poly x;;

-- 
Issac Trotts

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] function overloading
  2003-11-05  8:57 [Caml-list] function overloading Dustin Sallings
  2003-11-05 10:13 ` Issac Trotts
@ 2003-11-05 10:39 ` Julien Demouth
  2003-11-07  7:05 ` skaller
  2 siblings, 0 replies; 5+ messages in thread
From: Julien Demouth @ 2003-11-05 10:39 UTC (permalink / raw)
  To: Dustin Sallings, caml-list

Dustin Sallings wrote:
> 
>     I swear I found an example demonstrating how to do pattern matching 
> based on the type of an argument, but every pattern matching example I 
> can find now seems to be focused on value and not type.
> 

You've probably read an example of what GCaml offers. GCaml is an 
extension of OCaml allowing some "extensional polymorphic extensions". 
But you have to know that it is based on an old OCaml version. Have a 
look at:

	http://pauillac.inria.fr/~furuse/generics/

>     I'm trying to do something like this:
> 
> let print_poly x = match x with
>     | (x: bool) -> print_bool x
>     | (x: int) ->  print_int x
> ;;
> 
>     Can someone tell me what I'm doing wrong (or perhaps tell me why I 
> shouldn't be doing this at all)?
> 

It cannot work with OCaml because all the expressions in a pattern 
matching have to have the same type. The pattern matching is based on 
variables values and not on their types. It is how the language works.

Julien


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] function overloading
  2003-11-05 10:13 ` Issac Trotts
@ 2003-11-05 17:51   ` Dustin Sallings
  0 siblings, 0 replies; 5+ messages in thread
From: Dustin Sallings @ 2003-11-05 17:51 UTC (permalink / raw)
  To: ijtrotts; +Cc: caml-list


On Nov 5, 2003, at 2:13, Issac Trotts wrote:

> You can do this:
>
> type bool_or_int = Bool of bool | Int of int;;
>
> let print_bool b = print_string (if b then "true" else "false");;
>
> let print_poly x = match x with
>     Bool b -> print_bool b
>   | Int i -> print_int i;;
>
> let x = Bool true;;
>
> print_poly x;;

	This doesn't really solve my problem, I still have to know what the 
type was at some point.  Thanks for the help, though.

	It is good news that your print_bool and mine look identical, though 
(well, you have a space I don't have).  I must be understanding 
something here.  :)

	What I was hoping to do here was print out the termios structure from 
tcgetattr without having to create separate print functions for the 
boolean types, int types, and char types.  It just means I've got to 
pay slightly closer attention.

	Thanks again.

-- 
Dustin Sallings

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] function overloading
  2003-11-05  8:57 [Caml-list] function overloading Dustin Sallings
  2003-11-05 10:13 ` Issac Trotts
  2003-11-05 10:39 ` Julien Demouth
@ 2003-11-07  7:05 ` skaller
  2 siblings, 0 replies; 5+ messages in thread
From: skaller @ 2003-11-07  7:05 UTC (permalink / raw)
  To: Dustin Sallings; +Cc: Caml Mailing List

On Wed, 2003-11-05 at 19:57, Dustin Sallings wrote:
> 	I swear I found an example demonstrating how to do pattern matching 
> based on the type of an argument, but every pattern matching example I 
> can find now seems to be focused on value and not type.
> 
> 	I'm trying to do something like this:
> 
> let print_poly x = match x with
>      | (x: bool) -> print_bool x
>      | (x: int) ->  print_int x
> ;;
> 
> 	Can someone tell me what I'm doing wrong (or perhaps tell me why I 
> shouldn't be doing this at all)?

Try G'Caml.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2003-11-07  8:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-05  8:57 [Caml-list] function overloading Dustin Sallings
2003-11-05 10:13 ` Issac Trotts
2003-11-05 17:51   ` Dustin Sallings
2003-11-05 10:39 ` Julien Demouth
2003-11-07  7:05 ` skaller

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).