Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Amazing article!

I hate DOES> I was implementing it well after 1am last night and I hate it, I have this feeling as something gets harder to implement it means its not right, but I know DOES> is right, so its me, I just couldn't implement it well. It was super frustrating. But now I feel better :)

I am new to Forth but it feels like `create does>` has to be replaced with some new construct, I just want word code to operate on its data, but I need to gain more experience to find out, for now `create does>` will do.



You can replace it, there are two (nearly 3) forths that replace it:

https://github.com/dan4thewin/FreeForth2/blob/master/ff.asm

which uses a double loop to lookup first macro words, and then immediate words

https://github.com/ablevm/able-forth/blob/current/forth.scr

Ableforth implements a defer/expand operation \ to effectively quote words. The basic loop is then simply parse a text word, look it up and execute it.

Both make use of macros (code generators) to implement deferred behaviour, as well as code inlining. Ultimately all these operations implement defer by manipulating the execution flow, something that algebraic effects also do.

I have a feeling that algebraic effects can be used in a Forth to implement DOES.


Also Chuck Moore's ColorForth doesn't have "create does>" at all. Probably he considered the complexity was not worth it. I don't have it in my interpreter either, and I don't miss it; for the few near-misses I just use regular literals and calls. For instance, for the textbook example of "self-indexing arrays":

    : index-array swap cells + ;
    create a1 10 cells allot
    create a2 20 cells allot
    : array1 a1 index-array ;
    : array2 a2 index-array ;
I used to use the "implementation-dependent" trick of popping the return address (in e.g. index-array) to get the data. Less verbose, a bit more efficient. But my implementation doesn't permit it anymore.

Recently I've found out that implementing "self/this" pseudo-value and pseudo-method calls much more useful. The relation with this and "create does>" is that latter can be seen as poor man's closure, or poor man's object [1].

[1] https://stackoverflow.com/questions/2497801/closures-are-poo...


Two forths is one half.


In Dusk OS, I implement does> but with one level of indirection removed. Rather than using create, you bind any number to a behavior. Example:

: foo 1+ does> . ;

42 foo bar

bar \ prints 43

If you want, you can add the "traditional" indirection in the initialization part, for a similar effect.

So, not quite the same, but almost, and I think it echoes the intuition you have, which is also mine.


I miss decimal output for the stack contents by default (with .S).




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: