9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: rog@vitanuova.com
To: 9fans@cse.psu.edu, inferno-list@vitanuova.com
Subject: [9fans] limbo polymorphism
Date: Thu, 20 May 2004 20:51:36 +0100	[thread overview]
Message-ID: <dc00ea4e95195d2cdb419f275f2f4953@vitanuova.com> (raw)
In-Reply-To: <20040520192144.10183.qmail@g.galapagos.bx.psu.edu>

>| you could express it quite nicely with the new limbo
>| polymorphism stuff:
>
>Is there a good reference to look at for that?

ahem.

i don't believe there is, until someone gets around to updating the
reference manual.

the syntax does seem reasonably stable now, so that should probably
happen...

it's really quite straightforward though:

a function or adt can be parameterised with one or more type names (in
square brackets).  these types can be used in the body of the function
or adt as any other type.

e.g.
	reverse[T](x: list of T): list of T;

	Table: adt[T] {
		items:	array of list of (int, T);

		new: fn(nslots: int): ref Table[T];
		add:	fn(t: self ref Table, id: int, x: T): int;
		del:	fn(t: self ref Table, id: int): int;
		find:	fn(t: self ref Table, id: int): T;
	};

a "for" clause can define operations on the types (by default
there are none save assignment (and possibly equality, although
i think that will be deprecated).

e.g.
	sort[T](a: array of T) for {
		T =>
			cmp: fn (t0, t1: T): int;
		};

	Env: adt[V]
		for {
		V =>
			discard: fn(v: self V);
		}
	{
		items:	array of list of (string, V);

		new: fn(nslots: int): ref Env[V];
		set:	fn(t: self ref Env, id: string, x: V);
		get:	fn(t: self ref Env, id: string): V;
		clear: fn(t: self ref Env);
	};

[the last syntax is going to change, i think, so that
the for clause comes *after* the adt declaration, rather
than in the middle as currently.]

when calling a type-parameterised function, the compiler infers the
type parameter from the given arguments (currently it's not possible
to explicitly name the type, although that'll come, once john susses
the grammar).

with a function or adt declared polymorphically, you can use any
"pointer type" (i.e.  ref adts, chans, arrays, lists and strings).

for polymorphic types that require member functions, the type has to
be an adt containing at least the required member functions.

so, given the above declaration, you could do:

	x := reverse("one" :: "two" :: "three" :: nil);

to get a list containing "three"::"two"::"one"::nil (assuming
reverse() reverses a list!).



  reply	other threads:[~2004-05-20 19:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-20 18:42 [9fans] first capital letter in function names at man pages, why? rog
2004-05-20 19:21 ` Scott Schwartz
2004-05-20 19:51   ` rog [this message]
2004-05-20 20:10     ` [9fans] limbo polymorphism Charles Forsyth
2004-05-20 20:07 ` [9fans] first capital letter in function names at man pages, why? boyd, rounin
2004-05-21 14:05   ` rog
2004-05-21 15:12     ` boyd, rounin
2004-05-21 16:25       ` rog

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dc00ea4e95195d2cdb419f275f2f4953@vitanuova.com \
    --to=rog@vitanuova.com \
    --cc=9fans@cse.psu.edu \
    --cc=inferno-list@vitanuova.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).