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.

consider the kind of capturing when deciding variance

See original GitHub issue

Consider:

class Foo<out T>(variable T t)
{
    noop(Foo("hello").t);
}

I’d argue that the above should compile. Whether you are only setting, only getting, or both setting and getting the captured attribute should decide whether it is, respectively, contravariant, covariant, or invariant.

You might be wondering why one would mark the attribute as variable if one is not going to set it. But the catch is: one may be setting this.t, and not other.t.

In my particular use‐case, I have an attribute that is identifiable, immutable, and potentially expensive to check for equality. I want to set this attribute to the comparee so that I can use identity equality instead of value equality next time around.

Here is a simplification of what I’m trying to do:

class Foo<out Expensive>(variable Expensive expensive)
    given Expensive
        satisfies Identifiable
{
    hash => 0; // TODO
    shared actual Boolean equals(Object that)
    {
        if(is Foo<Expensive> that)
        {
            if(expensive === that.expensive)
            {
                return(true);
            }
            else if(expensive == that.expensive)
            {
                expensive = that.expensive;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        else
        {
            return(false);
        }
    }
}

Of course, this limitation can be worked around by having a second non‐variable attribute that delegates to the variable attribute and capturing that instead, but I think the compiler should be smart enough for the developer to not have to think and worry about that.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jvasileffcommented, Dec 27, 2017

The other option would be to get rid of the capture analysis and instead acknowledge an implied in Nothing (or out Anything for in TPs):

class Foo<out T>(variable T t, Foo<T> other) {
    t = other.t;       // ok
    other.t = nothing; // ok
    other.t = t;       // error, T is not assignable to Nothing    
}
1reaction
gavinkingcommented, Apr 10, 2018

OK, well, this seems to be working, and I’m very happy with the code improvements I made as a side-effect, so I have merged the branch. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Calculate Variance (+Why It's Important for Your ...
Knowing variance is a critical step in catching poor habits in their early stages, and lending urgency to missed targets or flaws in...
Read more >
Variance Analysis - principlesofaccounting.com
Favorable variances result when actual costs are less than standard costs, and vice versa. The following illustration is intended to demonstrate the very...
Read more >
Variance Investigation - Kaplan Knowledge Bank
Investigating variances is a key step in using variance analysis as part of performance management. When should a variance be investigated ...
Read more >
8.5 Describe How Companies Use Variance Analysis
Companies use variance analysis in different ways. The starting point is the determination of standards against which to compare actual ...
Read more >
Describe how companies use variance analysis
Companies use variance analysis in different ways. The starting point is the determination of standards against which to compare actual results. Many companies ......
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