Quantcast

Good way to organize constants?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Good way to organize constants?

FANG Colin
Say, I have a few constants

const VTYPE_BINARY = 'B'
const VTYPE_INTEGER = 'I'
const VTYPE_CONTINUOUS = 'C'

What's a good way to have a namespace on it?

So that I can use Vtype.BINARY, Vtype.INTEGER, Vtype.CONTINUOUS

Should I put those in a separator module? or create a type and get an instance of it?

module Vtype
    const ....
end

immutable Vtype
    BINARY::Char
    INTEGER::Char
    CONTINUOUS::Char
end

const vtype = Vtype('B','I','C')?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Good way to organize constants?

Mauro
Checkout my package: https://github.com/mauro3/Parameters.jl. Maybe of
use.

On Wed, 2016-11-16 at 13:18, FANG Colin <[hidden email]> wrote:

> Say, I have a few constants
>
> const VTYPE_BINARY = 'B'
> const VTYPE_INTEGER = 'I'
> const VTYPE_CONTINUOUS = 'C'
>
> What's a good way to have a namespace on it?
>
> So that I can use Vtype.BINARY, Vtype.INTEGER, Vtype.CONTINUOUS
>
> Should I put those in a separator module? or create a type and get an
> instance of it?
>
> module Vtype
>     const ....
> end
>
> immutable Vtype
>     BINARY::Char
>     INTEGER::Char
>     CONTINUOUS::Char
> end
>
> const vtype = Vtype('B','I','C')?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Good way to organize constants?

Milan Bouchet-Valat
In reply to this post by FANG Colin
Le mercredi 16 novembre 2016 à 04:18 -0800, FANG Colin a écrit :

> Say, I have a few constants
>
> const VTYPE_BINARY = 'B'
> const VTYPE_INTEGER = 'I'
> const VTYPE_CONTINUOUS = 'C'
>
> What's a good way to have a namespace on it?
>
> So that I can use Vtype.BINARY, Vtype.INTEGER, Vtype.CONTINUOUS
>
> Should I put those in a separator module? or create a type and get an
> instance of it?
>
> module Vtype
>     const ....
> end
>
> immutable Vtype
>     BINARY::Char
>     INTEGER::Char
>     CONTINUOUS::Char
> end
>
> const vtype = Vtype('B','I','C')?
Putting it in a module is fine if you want to regroup these under the
same namespace. That's the standard practice with enums too (when
constants correspond to integer codes, which isn't the case here).


Regards
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Good way to organize constants?

FANG Colin
Is there going to be overhead if I create constant group modules and use them via module_name.abc?

On 16 November 2016 at 13:31, Milan Bouchet-Valat <[hidden email]> wrote:
Le mercredi 16 novembre 2016 à 04:18 -0800, FANG Colin a écrit :
> Say, I have a few constants
>
> const VTYPE_BINARY = 'B'
> const VTYPE_INTEGER = 'I'
> const VTYPE_CONTINUOUS = 'C'
>
> What's a good way to have a namespace on it?
>
> So that I can use Vtype.BINARY, Vtype.INTEGER, Vtype.CONTINUOUS
>
> Should I put those in a separator module? or create a type and get an
> instance of it?
>
> module Vtype
>     const ....
> end
>
> immutable Vtype
>     BINARY::Char
>     INTEGER::Char
>     CONTINUOUS::Char
> end
>
> const vtype = Vtype('B','I','C')?
Putting it in a module is fine if you want to regroup these under the
same namespace. That's the standard practice with enums too (when
constants correspond to integer codes, which isn't the case here).


Regards

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Good way to organize constants?

Milan Bouchet-Valat
Le mercredi 16 novembre 2016 à 13:38 +0000, FANG Colin a écrit :
> Is there going to be overhead if I create constant group modules and
> use them via module_name.abc?
Not that I know of.


Regards

> On 16 November 2016 at 13:31, Milan Bouchet-Valat <[hidden email]>
> wrote:
> > Le mercredi 16 novembre 2016 à 04:18 -0800, FANG Colin a écrit :
> > > Say, I have a few constants
> > >
> > > const VTYPE_BINARY = 'B'
> > > const VTYPE_INTEGER = 'I'
> > > const VTYPE_CONTINUOUS = 'C'
> > >
> > > What's a good way to have a namespace on it?
> > >
> > > So that I can use Vtype.BINARY, Vtype.INTEGER, Vtype.CONTINUOUS
> > >
> > > Should I put those in a separator module? or create a type and
> > get an
> > > instance of it?
> > >
> > > module Vtype
> > >     const ....
> > > end
> > >
> > > immutable Vtype
> > >     BINARY::Char
> > >     INTEGER::Char
> > >     CONTINUOUS::Char
> > > end
> > >
> > > const vtype = Vtype('B','I','C')?
> > Putting it in a module is fine if you want to regroup these under
> > the
> > same namespace. That's the standard practice with enums too (when
> > constants correspond to integer codes, which isn't the case here).
> >
> >
> > Regards
> >
>
Loading...