1
votes

I am new to SML, trying to explore SML record and types, specifically how to have function inside a record.

For example, I created below type-

type foo={
    var1:int,
    f1: int -> int    // want to have result of function f1 here
 };

Now if I declare record of type 'foo'-

val rec1 = { var1= 10, ....}

I am not getting how to populate the 2nd parameter in the record. f1(10) is giving error.Also, can we declare and define the function inside 'type' like below -

type foo ={
    var1:int,
    f1 (x)=x+x
 };

Please share your opinion.

1

1 Answers

1
votes

You need to use a function expression:

val r = {var1 = 10, f = fn x => x}

No, you cannot define the value of a record field in a type definition. But you can define a little helper function as a "constructor":

fun new_foo i = {var1 = i, f = fn x => x+x}