Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module scoping #479

Closed
mfidemraizer opened this issue Jan 23, 2018 · 6 comments
Closed

Module scoping #479

mfidemraizer opened this issue Jan 23, 2018 · 6 comments

Comments

@mfidemraizer
Copy link

Hi!

As @davidchambers and I have been discussing, there's an interesting feature that would allow us to create Sanctuary module scopes.

What's the purpose of this?

  • We would be able to register new types for a given section of code or module instead of being forced to declare them just for the entire project.
  • Scopes would allow us to disable type checking for a given section of code or module too.

Pseudo-code:

const 
         Sa = S.create(...),
         s = Sa.scope({ checkTypes: <true|false>, env: [...] })

That is, child Sanctuary scope would inherit parent's env while it would be able to override checkTypes.

I find this convenient in order to let us define new types as you need them instead of having to load them during program startup.

@davidchambers
Copy link
Member

Perhaps we don't need a dedicated function for this: IIFEs work well.

In this example we disable type checking for a block of code:

(S => S.add ('abc') (123)) (S.create ({checkTypes: false, env: []}));
// => 'abc123'

In this example we use a stricter environment which does not permit the use of NaN:

(S => {
  S.I (Infinity);
  // => Infinity
  S.I (NaN);
  // ! TypeError: Unrecognized value
  //
  //   I :: a -> a
  //        ^
  //        1
  //
  //   1)  NaN :: (no types)
  //
  //   The value at position 1 is not a member of any type in the environment.
  //
  //   The environment contains the following types:
  //
  //     - ValidNumber
}) (S.create ({checkTypes: true, env: [$.ValidNumber]}));

Similarly, we could provide an environment which contains some of our own types.

@mfidemraizer
Copy link
Author

@davidchambers And what about a new param inherit which means that omitted settings are inherited if they're already configured?

Or with S you're referring to an S instance already gotten from calling S.create?

@davidchambers
Copy link
Member

And what about a new param inherit which means that omitted settings are inherited if they're already configured?

Something like this, do you mean?

S.create ({inherit: true, env: [$.ValidNumber]})

In the example above checkTypes would be set to whichever value was used when S was defined. This feels very JavaScripty, and would not type check. ;)

We could do this instead:

S.create ({checkTypes: S.Nothing, env: S.Just ([$.ValidNumber])})

Simpler, though, would be to expose S.checkTypes. One could then write:

S.create ({checkTypes: S.checkTypes, env: [$.ValidNumber]})

@mfidemraizer
Copy link
Author

I need to think about how to approach the underlying problem (adding new types in a seamless way into a given scope).

At the end of the day, there should be an approach to avoid having to declare types in a header section that represents giving them to S.create.

What disturbs me is not being able to declare new types where I need them: now I need to declare them in a separate file / scope. That is, actual code requiring these types isn't expressive: how do I know the whole type when I read my code?

@mfidemraizer
Copy link
Author

Probably this could be an approach:

S.scope(s => {
    s.def([String, String, Number, Object])
    const x = s.map(s.toMaybe, ["hello", "world", 28, { text: "hello world" }])
})

s.def data types would own a scoped life-style (i.e. they would end its life once the scope is destroyed).

@davidchambers
Copy link
Member

Can this issue be closed now that we have S.env, @mfidemraizer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants