Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active May 21, 2020 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/bb83f1ac495da9027ddc2bd60829217f to your computer and use it in GitHub Desktop.
Save 0atman/bb83f1ac495da9027ddc2bd60829217f to your computer and use it in GitHub Desktop.
Example of how nim siloes side-effecting code
import httpClient
import strutils
func protocol(url: string): bool = ## ok, pure function
return "http://" in url
func get(url: string): string = ## side-effects.nim(7, 6) Error: 'get' can have side effects
if protocol url:
return newHttpClient().getContent(url)
proc get(url: string): string = ## This now works. `get()` must be defined as a `proc` as it causes side effects
if protocol url:
return newHttpClient().getContent(url)
@0atman
Copy link
Author

0atman commented Apr 2, 2020

$ nim c side-effects.nim
side-effects.nim(7, 6) Error: 'get' can have side effects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment