David Ciria
David Ciria

Reputation: 7

Invalid datatype function

The function getElement doesn't work. I think the problem is the cart type definition, but I don't know why.

datatype element = FIRE | LAND | WATER

datatype pokemon = PIKACHU | CHARMANDER | BULBASUR

datatype power = int

type cart = pokemon*power*element

val newcart = (BULBASUR, 34, WATER)

fun getElement (c: cart) = (#3)c

val element = getElement newcart

Upvotes: 0

Views: 38

Answers (1)

Sam Westrick
Sam Westrick

Reputation: 1286

The problem is that you wrote this:

datatype power = int

but clearly what you want is this:

type power = int

The problem with datatype power = int is that it declares a new type power with a single constructor, written "int", which just happens to be spelled exactly the same way as the name of the type int. For example, with your original code, the value (BULBASUR, int, WATER) has type cart, whereas (BULBASUR, 34, WATER) does not.

When you fix it to type power = int, the value (BULBASUR, 34, WATER) will have type cart.

Upvotes: 1

Related Questions