Check generic subclass in Swift












1















I have the following function in swift:



func test<C1,C2>(val1: C1, val2: C2) {
}


But I want to use where to constraint this function to when C2 is subclass of C1. I tried something like this:



func test<C1,C2>(val1: C1, val2: C2) where C2: C1 {
}


But it does not work and gives an error as Type 'C2' constraint to non-protocol, non-class type 'C1'. Any idea how I should do it?










share|improve this question

























  • Please check this one. I hope it's help you: dispatchswift.com/…

    – Sagar Chauhan
    Dec 31 '18 at 8:56











  • @SagarChauhan sadly my problem is not covered in it. Even though C: SomeClass should be available for check if C is subclass of SomeClass, it seems it does not work if SomeClass itself is a generic parameter like my problem.

    – Afshin
    Dec 31 '18 at 9:01











  • i think it is not possible!!

    – SPatel
    Dec 31 '18 at 9:26











  • Why do you need this constraint? What do you want to implement?

    – Qbyte
    Dec 31 '18 at 13:56






  • 2





    @Afshin Could you give a more specific example what you want to do? Is it maybe possible to do this with protocols since this function now only works with classes and not with structs and enums?

    – Qbyte
    Dec 31 '18 at 15:17
















1















I have the following function in swift:



func test<C1,C2>(val1: C1, val2: C2) {
}


But I want to use where to constraint this function to when C2 is subclass of C1. I tried something like this:



func test<C1,C2>(val1: C1, val2: C2) where C2: C1 {
}


But it does not work and gives an error as Type 'C2' constraint to non-protocol, non-class type 'C1'. Any idea how I should do it?










share|improve this question

























  • Please check this one. I hope it's help you: dispatchswift.com/…

    – Sagar Chauhan
    Dec 31 '18 at 8:56











  • @SagarChauhan sadly my problem is not covered in it. Even though C: SomeClass should be available for check if C is subclass of SomeClass, it seems it does not work if SomeClass itself is a generic parameter like my problem.

    – Afshin
    Dec 31 '18 at 9:01











  • i think it is not possible!!

    – SPatel
    Dec 31 '18 at 9:26











  • Why do you need this constraint? What do you want to implement?

    – Qbyte
    Dec 31 '18 at 13:56






  • 2





    @Afshin Could you give a more specific example what you want to do? Is it maybe possible to do this with protocols since this function now only works with classes and not with structs and enums?

    – Qbyte
    Dec 31 '18 at 15:17














1












1








1


5






I have the following function in swift:



func test<C1,C2>(val1: C1, val2: C2) {
}


But I want to use where to constraint this function to when C2 is subclass of C1. I tried something like this:



func test<C1,C2>(val1: C1, val2: C2) where C2: C1 {
}


But it does not work and gives an error as Type 'C2' constraint to non-protocol, non-class type 'C1'. Any idea how I should do it?










share|improve this question
















I have the following function in swift:



func test<C1,C2>(val1: C1, val2: C2) {
}


But I want to use where to constraint this function to when C2 is subclass of C1. I tried something like this:



func test<C1,C2>(val1: C1, val2: C2) where C2: C1 {
}


But it does not work and gives an error as Type 'C2' constraint to non-protocol, non-class type 'C1'. Any idea how I should do it?







swift generics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 16:15







Afshin

















asked Dec 31 '18 at 8:37









AfshinAfshin

3,0361625




3,0361625













  • Please check this one. I hope it's help you: dispatchswift.com/…

    – Sagar Chauhan
    Dec 31 '18 at 8:56











  • @SagarChauhan sadly my problem is not covered in it. Even though C: SomeClass should be available for check if C is subclass of SomeClass, it seems it does not work if SomeClass itself is a generic parameter like my problem.

    – Afshin
    Dec 31 '18 at 9:01











  • i think it is not possible!!

    – SPatel
    Dec 31 '18 at 9:26











  • Why do you need this constraint? What do you want to implement?

    – Qbyte
    Dec 31 '18 at 13:56






  • 2





    @Afshin Could you give a more specific example what you want to do? Is it maybe possible to do this with protocols since this function now only works with classes and not with structs and enums?

    – Qbyte
    Dec 31 '18 at 15:17



















  • Please check this one. I hope it's help you: dispatchswift.com/…

    – Sagar Chauhan
    Dec 31 '18 at 8:56











  • @SagarChauhan sadly my problem is not covered in it. Even though C: SomeClass should be available for check if C is subclass of SomeClass, it seems it does not work if SomeClass itself is a generic parameter like my problem.

    – Afshin
    Dec 31 '18 at 9:01











  • i think it is not possible!!

    – SPatel
    Dec 31 '18 at 9:26











  • Why do you need this constraint? What do you want to implement?

    – Qbyte
    Dec 31 '18 at 13:56






  • 2





    @Afshin Could you give a more specific example what you want to do? Is it maybe possible to do this with protocols since this function now only works with classes and not with structs and enums?

    – Qbyte
    Dec 31 '18 at 15:17

















Please check this one. I hope it's help you: dispatchswift.com/…

– Sagar Chauhan
Dec 31 '18 at 8:56





Please check this one. I hope it's help you: dispatchswift.com/…

– Sagar Chauhan
Dec 31 '18 at 8:56













@SagarChauhan sadly my problem is not covered in it. Even though C: SomeClass should be available for check if C is subclass of SomeClass, it seems it does not work if SomeClass itself is a generic parameter like my problem.

– Afshin
Dec 31 '18 at 9:01





@SagarChauhan sadly my problem is not covered in it. Even though C: SomeClass should be available for check if C is subclass of SomeClass, it seems it does not work if SomeClass itself is a generic parameter like my problem.

– Afshin
Dec 31 '18 at 9:01













i think it is not possible!!

– SPatel
Dec 31 '18 at 9:26





i think it is not possible!!

– SPatel
Dec 31 '18 at 9:26













Why do you need this constraint? What do you want to implement?

– Qbyte
Dec 31 '18 at 13:56





Why do you need this constraint? What do you want to implement?

– Qbyte
Dec 31 '18 at 13:56




2




2





@Afshin Could you give a more specific example what you want to do? Is it maybe possible to do this with protocols since this function now only works with classes and not with structs and enums?

– Qbyte
Dec 31 '18 at 15:17





@Afshin Could you give a more specific example what you want to do? Is it maybe possible to do this with protocols since this function now only works with classes and not with structs and enums?

– Qbyte
Dec 31 '18 at 15:17












0






active

oldest

votes











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%2f53985281%2fcheck-generic-subclass-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53985281%2fcheck-generic-subclass-in-swift%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas