A field of “channel” type gives error “Cannot default-initialize a variable with generic type”Good practice to hold a file or channel in a classHow do I use generics with an array of Classes?ReSharper warns: “Static field in generic type”How to create object that can be added to a List<T> array?How can I make different captures of a wildcard compatible?Initializing a Generic variable from a C# Type VariableSwift generics not preserving typeGeneric Table ModelHow to solve “no generic parameters allowed”Swift generic subclass of regular class “cannot convert value of type to expected argument type”swift convenience init and generic class

Half-rock- half-forest-gnome

Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?

Is it double speak?

If there were no space agencies, could a person go to space?

In Pokémon Go, why does one of my Pikachu have an option to evolve, but another one doesn't?

What is the resistivity of copper at 3 kelvin?

Can we use other things than single-word verbs in our dialog tags?

Does bottle color affect mold growth?

Need help understanding lens reach

How does The Fools Guild make its money?

Does this smartphone photo show Mars just below the Sun?

I was contacted by a private bank overseas to get my inheritance

"How do you solve a problem like Maria?"

Double blind peer review when paper cites author's GitHub repo for code

Short story about a teenager who has his brain replaced with a microchip (Psychological Horror)

Why is there a need to prevent a racist, sexist, or otherwise bigoted vendor from discriminating who they sell to?

How quickly could a country build a tall concrete wall around a city?

Traveling from Germany to other countries by train?

sytemctl status log output

Whats the name of this projection?

How does the oscilloscope trigger really work?

Is there a loss of quality when converting RGB to HEX?

Why do private jets such as Gulfstream fly higher than other civilian jets?

Did WWII Japanese soldiers engage in cannibalism of their enemies?



A field of “channel” type gives error “Cannot default-initialize a variable with generic type”


Good practice to hold a file or channel in a classHow do I use generics with an array of Classes?ReSharper warns: “Static field in generic type”How to create object that can be added to a List<T> array?How can I make different captures of a wildcard compatible?Initializing a Generic variable from a C# Type VariableSwift generics not preserving typeGeneric Table ModelHow to solve “no generic parameters allowed”Swift generic subclass of regular class “cannot convert value of type to expected argument type”swift convenience init and generic class






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















In this code, I am creating a class that contains a channel that may be opened upon request when initialized. To do so, I am passing a flag to indicate whether a channel is opened or not (together with its file name).



class Myclass

var writeflag : bool;
var output : channel;

proc init( writeflag = false, filename = "" )

this.writeflag = writeflag;
if writeflag // (1)
assert( filename != "" );
this.output = openwriter( filename );
// (2)



proc main()

var a = new owned Myclass( writeflag = true,
filename = "test.out" );
a.output.writeln( 123 );



However, the compiler rejects the above code with the message:




myclass.chpl:6: error: Cannot default-initialize a variable with generic type
myclass.chpl:6: note: 'not-fully-instantiated' has generic type 'channel'




On the other hand, if I comment out (1) and (2), the code works as expected and creates "test.out". I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...










share|improve this question
























  • channel.isclosed() might be helpful for your application, in case you weren't aware of it.

    – bencray
    Mar 27 at 13:40






  • 1





    You don't get the error when the if statement is commented out because the lack of an else branch is what is triggering the error message. Basically, the Chapel compiler knows the type for output when you assign to it, but if the if statement isn't triggered, it doesn't know what default value to give it (hence bencray's recommendation).

    – Lydia Duncan
    Mar 27 at 15:23











  • I see... I think it explains the behavior of the above code, and why such an error did not occur in my previous code. Thanks very much :-)

    – roygvib
    Mar 27 at 15:32


















4















In this code, I am creating a class that contains a channel that may be opened upon request when initialized. To do so, I am passing a flag to indicate whether a channel is opened or not (together with its file name).



class Myclass

var writeflag : bool;
var output : channel;

proc init( writeflag = false, filename = "" )

this.writeflag = writeflag;
if writeflag // (1)
assert( filename != "" );
this.output = openwriter( filename );
// (2)



proc main()

var a = new owned Myclass( writeflag = true,
filename = "test.out" );
a.output.writeln( 123 );



However, the compiler rejects the above code with the message:




myclass.chpl:6: error: Cannot default-initialize a variable with generic type
myclass.chpl:6: note: 'not-fully-instantiated' has generic type 'channel'




On the other hand, if I comment out (1) and (2), the code works as expected and creates "test.out". I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...










share|improve this question
























  • channel.isclosed() might be helpful for your application, in case you weren't aware of it.

    – bencray
    Mar 27 at 13:40






  • 1





    You don't get the error when the if statement is commented out because the lack of an else branch is what is triggering the error message. Basically, the Chapel compiler knows the type for output when you assign to it, but if the if statement isn't triggered, it doesn't know what default value to give it (hence bencray's recommendation).

    – Lydia Duncan
    Mar 27 at 15:23











  • I see... I think it explains the behavior of the above code, and why such an error did not occur in my previous code. Thanks very much :-)

    – roygvib
    Mar 27 at 15:32














4












4








4








In this code, I am creating a class that contains a channel that may be opened upon request when initialized. To do so, I am passing a flag to indicate whether a channel is opened or not (together with its file name).



class Myclass

var writeflag : bool;
var output : channel;

proc init( writeflag = false, filename = "" )

this.writeflag = writeflag;
if writeflag // (1)
assert( filename != "" );
this.output = openwriter( filename );
// (2)



proc main()

var a = new owned Myclass( writeflag = true,
filename = "test.out" );
a.output.writeln( 123 );



However, the compiler rejects the above code with the message:




myclass.chpl:6: error: Cannot default-initialize a variable with generic type
myclass.chpl:6: note: 'not-fully-instantiated' has generic type 'channel'




On the other hand, if I comment out (1) and (2), the code works as expected and creates "test.out". I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...










share|improve this question














In this code, I am creating a class that contains a channel that may be opened upon request when initialized. To do so, I am passing a flag to indicate whether a channel is opened or not (together with its file name).



class Myclass

var writeflag : bool;
var output : channel;

proc init( writeflag = false, filename = "" )

this.writeflag = writeflag;
if writeflag // (1)
assert( filename != "" );
this.output = openwriter( filename );
// (2)



proc main()

var a = new owned Myclass( writeflag = true,
filename = "test.out" );
a.output.writeln( 123 );



However, the compiler rejects the above code with the message:




myclass.chpl:6: error: Cannot default-initialize a variable with generic type
myclass.chpl:6: note: 'not-fully-instantiated' has generic type 'channel'




On the other hand, if I comment out (1) and (2), the code works as expected and creates "test.out". I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...







file generics chapel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 5:59









roygvibroygvib

5,5252 gold badges12 silver badges31 bronze badges




5,5252 gold badges12 silver badges31 bronze badges















  • channel.isclosed() might be helpful for your application, in case you weren't aware of it.

    – bencray
    Mar 27 at 13:40






  • 1





    You don't get the error when the if statement is commented out because the lack of an else branch is what is triggering the error message. Basically, the Chapel compiler knows the type for output when you assign to it, but if the if statement isn't triggered, it doesn't know what default value to give it (hence bencray's recommendation).

    – Lydia Duncan
    Mar 27 at 15:23











  • I see... I think it explains the behavior of the above code, and why such an error did not occur in my previous code. Thanks very much :-)

    – roygvib
    Mar 27 at 15:32


















  • channel.isclosed() might be helpful for your application, in case you weren't aware of it.

    – bencray
    Mar 27 at 13:40






  • 1





    You don't get the error when the if statement is commented out because the lack of an else branch is what is triggering the error message. Basically, the Chapel compiler knows the type for output when you assign to it, but if the if statement isn't triggered, it doesn't know what default value to give it (hence bencray's recommendation).

    – Lydia Duncan
    Mar 27 at 15:23











  • I see... I think it explains the behavior of the above code, and why such an error did not occur in my previous code. Thanks very much :-)

    – roygvib
    Mar 27 at 15:32

















channel.isclosed() might be helpful for your application, in case you weren't aware of it.

– bencray
Mar 27 at 13:40





channel.isclosed() might be helpful for your application, in case you weren't aware of it.

– bencray
Mar 27 at 13:40




1




1





You don't get the error when the if statement is commented out because the lack of an else branch is what is triggering the error message. Basically, the Chapel compiler knows the type for output when you assign to it, but if the if statement isn't triggered, it doesn't know what default value to give it (hence bencray's recommendation).

– Lydia Duncan
Mar 27 at 15:23





You don't get the error when the if statement is commented out because the lack of an else branch is what is triggering the error message. Basically, the Chapel compiler knows the type for output when you assign to it, but if the if statement isn't triggered, it doesn't know what default value to give it (hence bencray's recommendation).

– Lydia Duncan
Mar 27 at 15:23













I see... I think it explains the behavior of the above code, and why such an error did not occur in my previous code. Thanks very much :-)

– roygvib
Mar 27 at 15:32






I see... I think it explains the behavior of the above code, and why such an error did not occur in my previous code. Thanks very much :-)

– roygvib
Mar 27 at 15:32













2 Answers
2






active

oldest

votes


















2















I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...




You're on the right track. The channel record has some param fields which must be provided at compile-time. You can specify these parameters in the field declaration:



var output : channel(writing=true, kind=iokind.dynamic, locking=false);





share|improve this answer

























  • Thanks very much, I've confirmed that the code now works. And I have one minor question... Is "locking=false" better than "true" in the above particular case? (Here, I will not write from within a parallel region.) Is locking=false possibly more efficient when only serial write is to be performed?

    – roygvib
    Mar 27 at 18:10






  • 1





    "locking=false" is indeed more efficient. However, we suggest that you resort to it only in performance-critical code. Also, you'd need to pass "locking=false" to openwriter() as well.

    – Vass
    Mar 27 at 22:19



















0














Following the suggestions in the answer and comment, I have modified my code as follows. Because the concrete type is a bit long to remember, I have created a type alias and used it in the class specification (here with locking=true for generality, please see docs).



// utils.chpl
type Writer = channel( true, iokind.dynamic, true );
type Reader = channel( false, iokind.dynamic, true );

// test.chpl
use utils only Writer;

class Myclass

var writeflag : bool;
var output : Writer;

// ... the remaining is the same






share|improve this answer





























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370697%2fa-field-of-channel-type-gives-error-cannot-default-initialize-a-variable-with%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...




    You're on the right track. The channel record has some param fields which must be provided at compile-time. You can specify these parameters in the field declaration:



    var output : channel(writing=true, kind=iokind.dynamic, locking=false);





    share|improve this answer

























    • Thanks very much, I've confirmed that the code now works. And I have one minor question... Is "locking=false" better than "true" in the above particular case? (Here, I will not write from within a parallel region.) Is locking=false possibly more efficient when only serial write is to be performed?

      – roygvib
      Mar 27 at 18:10






    • 1





      "locking=false" is indeed more efficient. However, we suggest that you resort to it only in performance-critical code. Also, you'd need to pass "locking=false" to openwriter() as well.

      – Vass
      Mar 27 at 22:19
















    2















    I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...




    You're on the right track. The channel record has some param fields which must be provided at compile-time. You can specify these parameters in the field declaration:



    var output : channel(writing=true, kind=iokind.dynamic, locking=false);





    share|improve this answer

























    • Thanks very much, I've confirmed that the code now works. And I have one minor question... Is "locking=false" better than "true" in the above particular case? (Here, I will not write from within a parallel region.) Is locking=false possibly more efficient when only serial write is to be performed?

      – roygvib
      Mar 27 at 18:10






    • 1





      "locking=false" is indeed more efficient. However, we suggest that you resort to it only in performance-critical code. Also, you'd need to pass "locking=false" to openwriter() as well.

      – Vass
      Mar 27 at 22:19














    2












    2








    2








    I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...




    You're on the right track. The channel record has some param fields which must be provided at compile-time. You can specify these parameters in the field declaration:



    var output : channel(writing=true, kind=iokind.dynamic, locking=false);





    share|improve this answer














    I guess the problem is related to the generic nature of channel (according to the error message), but not very sure how to write such a code appropriately...




    You're on the right track. The channel record has some param fields which must be provided at compile-time. You can specify these parameters in the field declaration:



    var output : channel(writing=true, kind=iokind.dynamic, locking=false);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 27 at 13:39









    bencraybencray

    1,2024 silver badges16 bronze badges




    1,2024 silver badges16 bronze badges















    • Thanks very much, I've confirmed that the code now works. And I have one minor question... Is "locking=false" better than "true" in the above particular case? (Here, I will not write from within a parallel region.) Is locking=false possibly more efficient when only serial write is to be performed?

      – roygvib
      Mar 27 at 18:10






    • 1





      "locking=false" is indeed more efficient. However, we suggest that you resort to it only in performance-critical code. Also, you'd need to pass "locking=false" to openwriter() as well.

      – Vass
      Mar 27 at 22:19


















    • Thanks very much, I've confirmed that the code now works. And I have one minor question... Is "locking=false" better than "true" in the above particular case? (Here, I will not write from within a parallel region.) Is locking=false possibly more efficient when only serial write is to be performed?

      – roygvib
      Mar 27 at 18:10






    • 1





      "locking=false" is indeed more efficient. However, we suggest that you resort to it only in performance-critical code. Also, you'd need to pass "locking=false" to openwriter() as well.

      – Vass
      Mar 27 at 22:19

















    Thanks very much, I've confirmed that the code now works. And I have one minor question... Is "locking=false" better than "true" in the above particular case? (Here, I will not write from within a parallel region.) Is locking=false possibly more efficient when only serial write is to be performed?

    – roygvib
    Mar 27 at 18:10





    Thanks very much, I've confirmed that the code now works. And I have one minor question... Is "locking=false" better than "true" in the above particular case? (Here, I will not write from within a parallel region.) Is locking=false possibly more efficient when only serial write is to be performed?

    – roygvib
    Mar 27 at 18:10




    1




    1





    "locking=false" is indeed more efficient. However, we suggest that you resort to it only in performance-critical code. Also, you'd need to pass "locking=false" to openwriter() as well.

    – Vass
    Mar 27 at 22:19






    "locking=false" is indeed more efficient. However, we suggest that you resort to it only in performance-critical code. Also, you'd need to pass "locking=false" to openwriter() as well.

    – Vass
    Mar 27 at 22:19














    0














    Following the suggestions in the answer and comment, I have modified my code as follows. Because the concrete type is a bit long to remember, I have created a type alias and used it in the class specification (here with locking=true for generality, please see docs).



    // utils.chpl
    type Writer = channel( true, iokind.dynamic, true );
    type Reader = channel( false, iokind.dynamic, true );

    // test.chpl
    use utils only Writer;

    class Myclass

    var writeflag : bool;
    var output : Writer;

    // ... the remaining is the same






    share|improve this answer































      0














      Following the suggestions in the answer and comment, I have modified my code as follows. Because the concrete type is a bit long to remember, I have created a type alias and used it in the class specification (here with locking=true for generality, please see docs).



      // utils.chpl
      type Writer = channel( true, iokind.dynamic, true );
      type Reader = channel( false, iokind.dynamic, true );

      // test.chpl
      use utils only Writer;

      class Myclass

      var writeflag : bool;
      var output : Writer;

      // ... the remaining is the same






      share|improve this answer





























        0












        0








        0







        Following the suggestions in the answer and comment, I have modified my code as follows. Because the concrete type is a bit long to remember, I have created a type alias and used it in the class specification (here with locking=true for generality, please see docs).



        // utils.chpl
        type Writer = channel( true, iokind.dynamic, true );
        type Reader = channel( false, iokind.dynamic, true );

        // test.chpl
        use utils only Writer;

        class Myclass

        var writeflag : bool;
        var output : Writer;

        // ... the remaining is the same






        share|improve this answer















        Following the suggestions in the answer and comment, I have modified my code as follows. Because the concrete type is a bit long to remember, I have created a type alias and used it in the class specification (here with locking=true for generality, please see docs).



        // utils.chpl
        type Writer = channel( true, iokind.dynamic, true );
        type Reader = channel( false, iokind.dynamic, true );

        // test.chpl
        use utils only Writer;

        class Myclass

        var writeflag : bool;
        var output : Writer;

        // ... the remaining is the same







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 29 at 12:58

























        answered Mar 29 at 12:08









        roygvibroygvib

        5,5252 gold badges12 silver badges31 bronze badges




        5,5252 gold badges12 silver badges31 bronze badges






























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370697%2fa-field-of-channel-type-gives-error-cannot-default-initialize-a-variable-with%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript