[squeak-dev] FFI ExternalTypeAlias

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Thu Jun 18 19:03:34 UTC 2020

Hi all,
following the question of Marcel about usage of ExternalTypeAlias:

Type alias are subclasses of ExternalStructure.
I used them in HDF5 because there was no other means to define an alias at
the time.
Now we have another mean by adding a class side method in ExternalType (I
would have rather imagined the usage of some annotation to get a
declarative style)

This has one advantage: type safety. You cannot pass a Foo to a function
expecting a Bar, even if they both alias int...

But this has one major drawback: with current FFI, you cannot pass a
Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for
an integer type...

Thus, you have to wrap every Bar argument into a Bar instance...
Gasp, this is going too far...

What about functions returning such alias?
Function returning struct by value allocate a struct, but it's not the case
here, FFI plugin is clever enough to recognize an atomic type alias and
simply return a Smalltalk Integer...
So it's not Bar all the way down, I have to manually wrap Bar...
This is not consistent.
If we return an int, we must accept an int, otherwise, we cannot chain FFI

I could use the lightweight alias instead, but there was another usage
where it was convenient to use ExternalTypeAlias: enum.
Indeed, I simply defined all enum symbols as class side method. For example
for enum bar { beg=0, mid=1, end=2 }; I simply create a type Bar aliasing
int and having class side methods beg ^0, mid ^1, end ^2.
This way, I normally can pass a Bar mid to an external function.
(currently, we must defne mid ^Bar new value: 1; yourself)

If I use a simpler alias, where are those ids going to?

IMO we cannot keep statu quo in the plugin, we gotta to make the
inputs/outputs behave consistently.
 The choice is this one:
- should a function expecting an ExternalTypeAlias of atomic type accept an
immediate value of compatible type? (the permissive implementation)
- should a function returning an ExternalTypeAlias of atomic type
instantiate the Alias? (the typesafe implementation, with higher runtime
cost due to pressure on GC)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200618/3ef7b418/attachment.html>

More information about the Squeak-dev mailing list