Skip to content
Brandon Elam Barker edited this page Jun 2, 2020 · 5 revisions

Like any Haskell type, you can have a zero-cost embedding of MATLAB types into a newtype for better type-safety and clarity. This is particularly useful for MStruct, MStructArray, or even MAnyArray, since the API itself doesn't allow us to further refine these types statically and they may represent very different types of objects. But newtypes are also very useful for the same types of objects that have different logical meanings within a program, and is likely their original purpose, e.g. maybe one string returned from one function is of a very different meaning in your program than another string, and you don't want to confuse them accidentally, so you use newtype to prevent this statically. Here is an example using haskell-matlab.

However, for performance reasons, you may also want to know about Coercible when performing certain operations (like <$>) on containers of newtypes. So instead of doing unMyNewtype <$> [myNewTypes], you could instead just have (coerce [myNewTypes] :: [MStruct]), to get back a list of bare (unwrapped) values (in this case, a list of MStructs).

Clone this wiki locally