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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현