lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Italo Maia once stated:
> Ok ... then, could someone provide an example of a weakly typed language?

  K&R C (or C prior to the ANSI C Standard of 1989).  For example, in file
A:

	double foo(a,b)
	double a;
	double b;
	{
	  return a + b;
	}

and in file B:

	bar()
	{
	  return foo(3 + 4);
	}

Without a declaration, foo() defaults to returning an integer, so you might
be surprised when bar() returns 16 instead of 7 [1].  Even fixing it:

	extern double foo();
	double bar()
	{
	  return foo(3+4);
	}

is problematic because the parameters are still passed in as integers, not
doubles (and here, the result is 0 [1]).

  BCPL, the predecessor to C, is even worse---*everything* is treated as an
unsigned integer and a "pointer" is just an index into a large integer array
called "memory" [2].  You still had structures though, which makes BCPL skip
the "typeless" moniker.

  Certain assemblers could be considered "weakly typed," like Microsoft's
MASM.  There, if you do:

	val	dw	5	; assign a 16 bits to the value 5
		push	[val]	; push the quantity at val onto the stack

MASM knows to generate a 16-bit push because of the 'dw' designation.  If
you change it do:

	val	dq	5
		push	[val]

it would know to use a 32-bit push instruction.  Not all assemblers will do
this.

> If there is no problem to operate on different types because the operation
> is defined, I'm quite unsure of what could be weakly typed.

  In assembly, there are different instructions that operate on different
sized data:

		add	al,5
		add	ax,5
		add	eax,5
		add	rax,5
		movss	xmm1,5
		addss	xmm0,xmm1

but it's up to the programmer (or programmer and assembler) to pick the
right instruction.

  -spc

[1]	At least, on my x86 32-bit system.  Your milage may vary.

[2]	On the Amiga, the file system was written in BCPL, and a BCPL
	"pointer" was really an index into a 32-bit integer array---to get
	an address, you had to multiply the BCPL pointer by 4.