Question

Im trying to make a function that will return an element of type "point":

type point = {x : int, y : int};
fun pointadd (p1: point, p2: point) = (((#x p1) + (#x p2)), ((#y p1) + (#y p2)));

but SMLNJ doesn't seem to understand my intention that the result should be of type "point" as well:

use "test1.sml";
[opening test1.sml]
type point = {x:int, y:int}
val pointadd = fn : point * point -> int * int
Was it helpful?

Solution

point is a record type, but you are returning a tuple instead.

How about something like this:

fun pointadd (p1: point, p2: point) =
    { x = #x p1 + #x p2,
      y = #y p1 + #y p2 };

You can add a type guard on the return type to make the type nicer, but it's equivalent:

fun pointadd (p1: point, p2: point) : point =
    { x = #x p1 + #x p2,
      y = #y p1 + #y p2 };

OTHER TIPS

It's been quite a while since my SML days but afair the type system does not resolve defined types automatically when printing the type signature. You could try something like this:

fun pointadd (p1: point, p2: point) = (((#x p1) + (#x p2)), ((#y p1) + (#y p2))): point
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top