[m-users.] Field access functions usage
Dirk Ziegemeyer
dirk at ziegemeyer.de
Fri Aug 16 07:04:46 AEST 2019
Hi,
I’d like to use a field access function as a higher order term. As explained in reference manual section 3.4.1, I declare the function explicitly. This seems to affect the „normal“ field access syntax which does not compile any more.
Example:
:- type foo
---> foo(
field_1 :: int,
field_2 :: string
).
:- func field_1(foo) = int.
main(!IO) :-
Foo = init_foo,
_X = field_1(Foo),
_Y = list.map(field_1, [Foo]).
:- func init_foo = foo.
init_foo = X :-
X^field_1 = 5,
X^field_2 = "bar".
Compiler error:
field_access_test.m:039: In clause for `init_foo = out':
field_access_test.m:039: in argument 1 of call to function
field_access_test.m:039: `field_access_test.field_1'/1:
field_access_test.m:039: mode error: variable `X' has instantiatedness
field_access_test.m:039: `unique(foo(free, unique("bar")))',
field_access_test.m:039: expected instantiatedness was `ground’.
As soon as I add the declaration of the compiler-generated function field_1/1, init_foo/0 does not compile any more. What is wrong in my code?
Dirk.
More information about the users
mailing list