3 ms·
Haskell does not really have pointers, but let's say it does (or that ioref fulfills it). How would you then (as an example) declare an array of pointers pointi
by andi999 5y ago
Haskell does not really have pointers, but let's say it does (or that ioref fulfills it). How would you then (as an example) declare an array of pointers pointing to functions accepting int and returning float declare in Haskell?
- Ar-Curunir 5y agoLike How Rust does it? via Box or using &
- lifthrasiir 5y agoIt would be `[fn(i32) -> f32; N]` for the reference (equivalent to `float (*[N])(int)` in C). Box or & would be only required for also allowing function-like values, as in `[Box<dyn Fn(i32) -> f32>; N]`.
- anyfoo 5y agoArray Int (IORef (Int -> Float)) The first "Int" is the type of the array's index, which in C is always "int" and hence not specified. (As you note yourself, pointers, or even just arrays, are not very much of a Haskell concept, but I still think this is pretty readable and very obviously parseable if you know the involved types "Array i e" and "IORef a".)
- FeepingCreature 5y agoFor a C-like take, D: float function(int)[], or if you genuinely wanted an array of pointers to function pointers, float function(int)*[].
- andi999 5y agoThanks. Yes I wanted array of pointers. So then how would you do a pointer to an array of functions in D? * float function(int)[] ? Edit: or float function(int)[] *
- FeepingCreature 5y agofloat function(int)[]*, yes.
- nextaccountic 5y agoIn Rust, an array with 10 elements that are function pointers whose functions receive i32 and return f32 is like this: [fn(i32) -> f32; 10] https://doc.rust-lang.org/reference/types/function-pointer.html https://doc.rust-lang.org/reference/types/function-pointer.h... That's maybe cheating because this type doesn't use Rust raw pointers; C's char* in Rust is something like const char or mut char. But, if you wanted a raw pointer (and were ok with a double pointer: an array of pointers to function pointers), it would be like this: [*mut (fn(i32) -> f32); 10] Which I think we can drop the (), since there is no ambiguity [*mut fn(i32) -> f32; 10]
- nextaccountic 5y agook, so HN formatting ate a couple * here and there. I meant that char* something; in C is like let something: *mut char; or let something: *const char; In Rust. So complex pointers are easily expressible: they are just *mut something complex