Page 1 of 1

Does cobra have special syntax for immutable types?

PostPosted: Fri Mar 22, 2013 7:31 am
by Nefarel
In C# it is very verbose to create immutable type. Does Cobra have some shorter syntax?:)

Re: Does cobra have special syntax for immutable types?

PostPosted: Fri Mar 22, 2013 8:38 am
by Charles
It does not. Do you have suggestions?

Re: Does cobra have special syntax for immutable types?

PostPosted: Fri Mar 22, 2013 1:39 pm
by torial
Charles wrote:It does not. Do you have suggestions?


How about using the "is readonly" attribute for classes? Coupled with any properties must be set by the constructor (or object initializer), and are automatically readonly (kind of like static class in C#, except I think you have to explicitly say each item in the class is static).

Re: Does cobra have special syntax for immutable types?

PostPosted: Sat Mar 23, 2013 4:04 am
by hopscc
What type of immutability are you looking for ?

Kinds of Immutability

For MP perhaps better to default to immutable objects and have some syntax to explicitly relax that....

Re: Does cobra have special syntax for immutable types?

PostPosted: Sat Mar 23, 2013 4:36 am
by kirai84
hopscc wrote:What type of immutability are you looking for ?

Kinds of Immutability

For MP perhaps better to default to immutable objects and have some syntax to explicitly relax that....


Simply all the fields are readonly, at least by default, when a class is marked as "is readonly". They did it in Oxygen and its great when you try to make things in more functional way. It's still far from make Cobra a really handy language for that type of programming though.

Re: Does cobra have special syntax for immutable types?

PostPosted: Tue Mar 26, 2013 11:51 pm
by Charles
What about collections? If I create a list like so:
var name = ['abc', 'python', 'cobra']

And I want the list to be immutable, what idiom/syntax should we use?

Re: Does cobra have special syntax for immutable types?

PostPosted: Thu Mar 28, 2013 2:45 am
by kirai84
Charles wrote:What about collections? If I create a list like so:
var name = ['abc', 'python', 'cobra']

And I want the list to be immutable, what idiom/syntax should we use?

It could be
var name = readonly ['abc', 'python', 'cobra']

or
var name = const ['abc', 'python', 'cobra']

Re: Does cobra have special syntax for immutable types?

PostPosted: Fri Mar 29, 2013 8:26 pm
by hopscc
Why not use the existing declaration forms (augmented) instead of a variant syntax form for this one construct ?
extend the isname set ...

#No opinion on the exact keyword - for 'readonly' could use 'const' or 'immutable' 

# augment/provide a clarifying isname declaration
var name is readonly = ['abc', 'python', 'cobra']
# or
var name = ['abc', 'python', 'cobra'] to readonly


'to' is for casting type (and nilability) - casting immutability in the same way isnt a big step...

..
You can take the position that literals are already immutable anyway - just used for assignment/initialisation of a (modifiable) variable ...
In that case the casting/inference-from-assignment would be casting away the immutability.

var name = ['abc', 'python', 'cobra'] to mutable # or 'variable'/'nonconst'/'var',....


Similarly to casting to and from nilable, provide some punctuation for to and from mutability
var name = ['abc', 'python', 'cobra'] to * # '*' for mutable, '|' for immutable say - read as fuzzy vs rigid