Forums
Does cobra have special syntax for immutable types?
8 posts
• Page 1 of 1
Does cobra have special syntax for immutable types?
In C# it is very verbose to create immutable type. Does Cobra have some shorter syntax?:)
- Nefarel
- Posts: 6
Re: Does cobra have special syntax for immutable types?
It does not. Do you have suggestions?
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Does cobra have special syntax for immutable types?
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).
- torial
- Posts: 229
- Location: IA
Re: Does cobra have special syntax for immutable types?
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....
Kinds of Immutability
For MP perhaps better to default to immutable objects and have some syntax to explicitly relax that....
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Does cobra have special syntax for immutable types?
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.
- kirai84
- Posts: 24
Re: Does cobra have special syntax for immutable types?
What about collections? If I create a list like so:
And I want the list to be immutable, what idiom/syntax should we use?
var name = ['abc', 'python', 'cobra']
And I want the list to be immutable, what idiom/syntax should we use?
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Does cobra have special syntax for immutable types?
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']
- kirai84
- Posts: 24
Re: Does cobra have special syntax for immutable types?
Why not use the existing declaration forms (augmented) instead of a variant syntax form for this one construct ?
extend the isname set ...
'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.
Similarly to casting to and from nilable, provide some punctuation for to and from mutability
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
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
8 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 13 guests