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

Add record support to constants #15231

Closed

Conversation

Shan1024
Copy link
Member

@Shan1024 Shan1024 commented May 6, 2019

Purpose

This PR adds support to define record constants.

Resolves #14877

Sample usages

Simple constant map declarations

using records -

type BR1 record {|
    boolean key1;
    boolean key2;
|};

const BR1 br1 = { key1: true, key2: false };

using anonymous records -

const record {| boolean key1; boolean key2; |} br1 = { key1: true, key2: false };

Nested constant map declaration

using records -

type BR1 record {|
    boolean key1;
    boolean key2;
|};

type BR2 record {|
    boolean key3;
    boolean key4;
|};

type BR3 record {|
    BR1 key5;
    BR2 key6;
    anydata...;
|};

type BR4 record {|
    boolean key8;
    boolean key9;
|};

const BR4 br4 = { key8: true, key9: false };

const BR3 br3 = { key5: br1, key6: br2, key7: br4 };
const BR1 br1 = { key1: true, key2: false };
const BR2 br2 = { key3: false, key4: true };

using anonymous record -

const record {| boolean key8; boolean key9; |} br4 = { key8: true, key9: false };

const record {| record {| boolean key1; boolean key2; |} key5; record {| boolean key3; boolean key4; |} key6; anydata...; |} br3 = { key5: br1, key6: br2, key7: br4 };
const record {| boolean key1; boolean key2; |} br1 = { key1: true, key2: false };
const record {| boolean key3; boolean key4; |} br2 = { key3: false, key4: true };

Constant references in maps

const v = "v"; 
const record {| string...; |} r = { k: v };

Simple constant map key references in maps

const record {| string...; |} r1 = { k: "v" };
const record {| string...; |} r2 = { k: r1.k };

Chained constant map key references in maps

const record {| string...; |} r1 = { k1: "v" };
const const record {| record {| string...; |}...; |} r2 = { k2: r1 };

const record {| string...; |} r3 = { k: r2.k2.k1 };

Equality checks

const record {| boolean...; |} br1 = { key1: true };
const record {| boolean...; |} br2 = { key2: false };
const record {| record {| boolean...; |}...; |} br3 = { key3: br1, key4: br2 };

const record {| boolean...; |} br1_new = { key1: true };
const record {| boolean...; |} br2_new = { key2: false };
const record {| record {| boolean...; |}...; |} br3_new = { key3: br1_new, key4: br2_new };

const record {| record {| boolean...; |}...; |} br7 = { key3: { key1: true }, key2: { key4: false } };
const record {| record {| boolean...; |}...; |} br8 = { key3: { key1: true }, key2: { key4: false } };

// -----------------------------------------------------------

br1 == br1; // true
br1 == br1_new; // true

br1 === br1; // true
br1 === br1_new; // false

// -----------------------------------------------------------

br3 == br3; // true
br3 == br3_new; // true

br3 === br3; // true
br3 === br3_new; // false

// -----------------------------------------------------------

br3.key3 == br3.key3; // true
br3.key3 == br3_new.key3; // true

br3.key3 === br1; // true
br3.key3 === br3_new.key3; // false

// -----------------------------------------------------------

br7.key3 == br7.key3; // true
br7.key3 == br1; // true

br7.key3 === br7.key3; // true
br7.key3 === br1; // false

// -----------------------------------------------------------

br7 == br8; // true
br7 === br8; // false

br7.key3 == br8.key3; // true
br7.key3 === br8.key3; // false

Usages which will result in panics

record {| record {| string...; |}...; |} sr10 = { sr10k: sr11 };
const record {| string...; |} sr11 = { sr11k: "sr11v" };

function updateNestedConstantStringRecordValueWithExistingKey() {
    sr10.sr10k.sr11k = "sr11nv"; // panic
}

function updateNestedConstantStringRecordValueWithNewKey() {
    sr10.sr10k.newKey = "newValue"; // panic
}

function updateReturnedConstantStringRecordWithExistingKey() {
    record {| string...; |} m = getStringRecord();
    m.sr11k = "sr11kn"; // panic
}

function updateReturnedConstantStringRecord2WithNewKey() {
    record {| string...; |} m = getStringRecord();
    m.newKey = "newValue"; // panic
}

function getStringRecord() returns record {| string...; |} {
    return sr11;
}

// -----------------------------------------------------------

record {| string...; |}[] sa1 = [sr11];

function updateConstantStringRecordValueInArrayWithExistingKey() {
    sa1[0].sr11k = "sr11nv"; // panic
}

function updateConstantStringRecordValueInArrayWithNewKey() {
    sa1[0].newKey = "newValue"; // panic
}

function getConstantStringRecordValueInArray() returns string {
    return sa1[0].sr11k;
}

Check List

  • Read the Contributing Guide
  • Required Balo version update
  • Updated Change Log
  • Checked Tooling Support (#)
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

@codecov-io
Copy link

codecov-io commented May 8, 2019

Codecov Report

Merging #15231 into master will increase coverage by 0.19%.
The diff coverage is 0%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #15231      +/-   ##
============================================
+ Coverage     23.03%   23.23%   +0.19%     
- Complexity     4995     5065      +70     
============================================
  Files          1342     1342              
  Lines         67183    67497     +314     
  Branches       9112     9203      +91     
============================================
+ Hits          15477    15680     +203     
- Misses        50004    50079      +75     
- Partials       1702     1738      +36
Impacted Files Coverage Δ Complexity Δ
.../ballerinalang/util/diagnostic/DiagnosticCode.java 0% <0%> (ø) 0 <0> (ø) ⬇️
...iler/semantics/analyzer/ConstantValueResolver.java 0% <0%> (ø) 0 <0> (ø) ⬇️
.../compiler/semantics/analyzer/SemanticAnalyzer.java 0% <0%> (ø) 0 <0> (ø) ⬇️
...g/wso2/ballerinalang/compiler/desugar/Desugar.java 0% <0%> (ø) 0 <0> (ø) ⬇️
.../ballerinalang/compiler/codegen/CodeGenerator.java 0% <0%> (ø) 0 <0> (ø) ⬇️
...alang/compiler/semantics/analyzer/SymbolEnter.java 0% <0%> (ø) 0 <0> (ø) ⬇️
...viders/subproviders/ServiceCompletionProvider.java 72.09% <0%> (-19.58%) 9% <0%> (+5%)
...erinalang/langserver/completions/util/Snippet.java 94.31% <0%> (-5.69%) 3% <0%> (ø)
...rg/wso2/ballerinalang/compiler/bir/BIREmitter.java 0% <0%> (ø) 0% <0%> (ø) ⬇️
...va/org/wso2/ballerinalang/compiler/bir/BIRGen.java 0% <0%> (ø) 0% <0%> (ø) ⬇️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 800f8cd...a2c30e8. Read the comment docs.

@Shan1024 Shan1024 changed the title [WIP] Add record support to constants Add record support to constants May 8, 2019
@Shan1024
Copy link
Member Author

Shan1024 commented Jul 2, 2019

Closing the PR since this might be outdated after 2 months and no one bothered to even check and approve this PR.

@Shan1024 Shan1024 closed this Jul 2, 2019
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

Successfully merging this pull request may close these issues.

Add record support to constants
2 participants