question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(Optionally) automated environment via namespaces

See original GitHub issue

I find myself calling create and passing around different env’s of types in many files, and I’ve only just started really leaning on sanctuary in a large project, and its going to get worse.

I’ve thought about different ways to structure this to prevent the verbosity, but I thought I’d at least vent the radical thought that keeps coming to mind.

What I really want, is an automated env. I don’t want to pass env’s around, I want to define a type and ideally I want sanctuary-def to manage the concatenation for me.

This came up when I was working on sum-type, I wanted to be able to nest union types, and I wasn’t sure if that meant I needed to insert the previously created types into the env of any new types. So to date (I think), that’s exactly what I do. Its technically still pure, it just automatically gives each type a new env that includes the previously defined types. I don’t know if I actually need to do that, but whether or not I do, it at least makes me wonder, why don’t we do this by default?

I don’t want to have to think about order of operations across possibly 100’s of modules. I want to be able to write $.RecordType({ ... }) and know I can use that type anywhere now.

Now there’s problems, maybe we don’t want some subset of a project to include the environment from another part of a project. I think this is solved in programming languages via a namespace.

So what if we had $.namespace

// file1
// string -> Array ( Any -> Any ) -> ???
$.namespace('main', function([def, S, SumType]){
   $.NullaryType(...)
   $.UnaryType(...)
   $.RecordType(...)

   def(....)
   def(....)

   SumType(...)
})
//file2
// string -> Array ( Any -> Any ) -> ???
$.namespace('main', function([def]){
   $.NullaryType(...)
   $.UnaryType(...)
   $.RecordType(...)

   
})

And then in one place, we can initialise that namespace.

// String -> Array ({ checkTypes: Boolean, env }) -> ???
$.createNamespace('main', [
  $.create
  ,S.create
  ,SumType.create
])

Just walking through the signatures, $.createNamespace would accept a namespace name, and a list of functions that receive { checkTypes, env }, sanctuary-def would somehow initialize them for us, and manage concatenating the environment behind the scenes. This is very hand wavey! 😄

I’m not sure what $.createNamespace would return, I have a few ideas. One option, it could return a merged object of whatever each $.namespace returned, essentially giving us an analogue to modules. Another idea could be to return an interface that gives the caller control over exception handling (whether to throw, or write errors to a stream etc)

This API could let us do cool things, like use different namespaces for testing, or hot paths. Or control execution of code that depends on sanctuary-def types. Now we can toggle env for the entire namespace (across files) in one place. We also don’t need to concern ourselves with concatenating different type arrays, because namespaces would automatically do that for us.

It’s very opinionated, and maybe would be better off as a library, but I don’t know if it would be possible to do this in user-land.

Its a very broad debatable idea, but seems interesting to me. I’d love to hear some criticism!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
JAForbescommented, Apr 26, 2017

I like that revision!

Maybe $.namespace could accept args for documentation base urls, and package name prefixes, and the namespace itself could autoprefix type and function names with package prefixes. Potentially we could just provided the hash, or final path of a documentation url per type.

makeNs(
  'my-package/main'
  ,'https://my-package.github.io/docs/'
  ,{ checkTypes: true, env }
)

That would automatically prefix any documentation urls in that namespace with that base url, and any type with that package prefix.

1reaction
gabejohnsoncommented, Apr 25, 2017

How about

// file1.js
// string -> StrMap Type -> ???
export default $.environment(({def, $, SumType}) => ({
   TypeA: $.NullaryType(...),
   TypeB: $.UnaryType(...),
   TypeC: $.RecordType(...),
   fn1: def(....),
   fn2: def(....),
   TypeD: SumType(...)
}));

// file2.js
// string -> Array ( Any -> Any ) -> ???
export default $.environment(({$}) => ({
   TypeA: $.NullaryType(...),
   TypeB: $.UnaryType(...),
   TypeC: $.RecordType(...),
}));

// main.js
import env1 from 'file1';
import env2 from 'file2';

const makeNs = $.namespace({
  def: S.create,
  $: $.create,
  SumType: SumType.create
});

const ns = makeNs('main', S.concat(env1, env2)); // env2 overwrites bindings from env1
const { bindings: {TypeA} } = ns;
const { TypeA: f1TypeA } = makeNs('', env1);

$.environment or $.namespace could also concatenate the bindings with whichever standard/global types/functions exist.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Kubernetes Namespaces to Manage Environments
Specifying the namespace is optional in Kubernetes because by default Kubernetes uses the "default" namespace.
Read more >
Automating Multi-Environment Kubernetes Virtual Clusters ...
Kubernetes supports multiple virtual clusters within the same physical cluster. These virtual clusters are called Namespaces. Namespaces are a way to divide ...
Read more >
Optionally attach namespace and node metadata to targets ...
In this scenario, managed namespaces are dynamically created on behalf of users where they have full control over the resources deployed in the ......
Read more >
Customize k8s namespace per environment for managed ...
The namespace management policy can be chosen at cluster creation time. The two options are to use a single namespace per project or...
Read more >
Working with Kubernetes clusters and namespaces in Cloud ...
A cloud administrator can change the project associated with a Kubernetes namespace or cluster on this page so that the administrator can ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found