1

The design pattern known as "functional core, imperative shell" is about separating side-effects from pure calculations, where business logic is supposed to be pure and then coordinated by the top function in the call, the "shell". Is it possible to design a framework (in any language) that enforces this pattern?

Here are some articles about the pattern: https://gist.github.com/kbilsted/abdc017858cad68c3e7926b03646554e

2
  • Typically you will not be able to prevent user code from performing arbitrary IO. It may be possible to "sandbox" the user code and intercept IO operations, but this very much depends on the language and environment if this is possible.
    – JacquesB
    Oct 7, 2020 at 13:53
  • @JacquesB One idea could be to only inject effect factories, and not the effectful libs themselves. Otherwise, yes, a linter would be needed to scan code for "forbidden" functions. Oct 9, 2020 at 10:07

1 Answer 1

1

You should be able to enforce this in a language like Haskell, which doesn't allow any functions to have side-effects, and only allows input and output to happen by way of functions returning the IO type, which represents an input/output action.

If your shell requires functions to be plugged in to it that return some other type, e.g. a representation of a Response in a web framework, then it should be impossible for that function to do anything else.

1
  • Yes, Haskell is the obvious choice. :) In other languages, you could still wrap side-effects in command objects, but you wouldn't be able to enforce the absence of side-effects. AFAIK. Sep 7, 2020 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.