Behavior of Swift Any.Type and genericsWhy can't I pass a Protocol.Type to a generic T.Type parameter?Create Generic method constraining T to an EnumCollections.emptyList() returns a List<Object>?Can't operator == be applied to generic types in C#?How to create a generic array in Java?When do Java generics require <? extends T> instead of <T> and is there any downside of switching?Generic TryParseHow to call Objective-C code from Swift#pragma mark in Swift?Swift Beta performance: sorting arraysswift 2 Func Default Type Argument
I hit a pipe with a mower and now it won't turn
What was the impact of Fischer vs. Spassky 1972 on the relationship between the USA and the Soviet Union?
Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?
Computing the sum of an infinite series as a variant of a geometric series
Are these intended activities legal to do in the USA under the VWP?
Who voices the character "Finger" in The Fifth Element?
How to add text on top of symbols for vector layers in QGIS
How to Prove a System Is Invertible?
Donkey as Democratic Party symbolic animal
What exactly did Ant-Man see that made him say that their plan worked?
Is there a legal way for US presidents to extend their terms beyond four years?
One folder having two different locations on Ubuntu 18.04
Procedurally generate regions on island
Cast volatile array to non volatile array
Was it really unprofessional of me to leave without asking for a raise first?
Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?
How do I tell the reader that my character is autistic in Fantasy?
Is it okay to fade a human face just to create some space to place important content over it?
Meaning of じゃないんじゃない?
Can I travel from Germany to England alone as an unaccompanied minor?
Sharing referee/AE report online to point out a grievous error in refereeing
Using Raspberry Pi to flash its own SD card
What's the safest way to inform a new user of their password on an invite-only website?
Conjugate る-ending verbs into negative form
Behavior of Swift Any.Type and generics
Why can't I pass a Protocol.Type to a generic T.Type parameter?Create Generic method constraining T to an EnumCollections.emptyList() returns a List<Object>?Can't operator == be applied to generic types in C#?How to create a generic array in Java?When do Java generics require <? extends T> instead of <T> and is there any downside of switching?Generic TryParseHow to call Objective-C code from Swift#pragma mark in Swift?Swift Beta performance: sorting arraysswift 2 Func Default Type Argument
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Given a function with a generic parameter, I would like to pass a value of which type is Any.Type to the function but it doesn't compile.
func foo<T>(_ type: T.Type)
print(type)
foo(Any.self) // ok
foo(Int.self) // ok
let a: Any.Type = Any.self // ok
let b: Any.Type = Int.self // ok
foo(a) // error: expected an argument list of type '(T.Type)'
foo(b) // error: expected an argument list of type '(T.Type)'
My assumption is that:
foo(Any.self)successfully compiles and the type placeholderTwould holdAny.Similarly, if a value of type
Any.Typeis given, theTshould be resolved intoAny
Could you explain why this code doesn't work?
swift generics metatype
add a comment |
Given a function with a generic parameter, I would like to pass a value of which type is Any.Type to the function but it doesn't compile.
func foo<T>(_ type: T.Type)
print(type)
foo(Any.self) // ok
foo(Int.self) // ok
let a: Any.Type = Any.self // ok
let b: Any.Type = Int.self // ok
foo(a) // error: expected an argument list of type '(T.Type)'
foo(b) // error: expected an argument list of type '(T.Type)'
My assumption is that:
foo(Any.self)successfully compiles and the type placeholderTwould holdAny.Similarly, if a value of type
Any.Typeis given, theTshould be resolved intoAny
Could you explain why this code doesn't work?
swift generics metatype
7
See Why can't I pass a Protocol.Type to a generic T.Type parameter? – forfoo(Any.self),Any.selfis treated asAny.Protocolallowing the code to compile.
– Hamish
Mar 25 at 13:29
@Hamish Thank you for the link! It really helped me understand how it works.
– Yonguk Jeong
Mar 26 at 10:26
add a comment |
Given a function with a generic parameter, I would like to pass a value of which type is Any.Type to the function but it doesn't compile.
func foo<T>(_ type: T.Type)
print(type)
foo(Any.self) // ok
foo(Int.self) // ok
let a: Any.Type = Any.self // ok
let b: Any.Type = Int.self // ok
foo(a) // error: expected an argument list of type '(T.Type)'
foo(b) // error: expected an argument list of type '(T.Type)'
My assumption is that:
foo(Any.self)successfully compiles and the type placeholderTwould holdAny.Similarly, if a value of type
Any.Typeis given, theTshould be resolved intoAny
Could you explain why this code doesn't work?
swift generics metatype
Given a function with a generic parameter, I would like to pass a value of which type is Any.Type to the function but it doesn't compile.
func foo<T>(_ type: T.Type)
print(type)
foo(Any.self) // ok
foo(Int.self) // ok
let a: Any.Type = Any.self // ok
let b: Any.Type = Int.self // ok
foo(a) // error: expected an argument list of type '(T.Type)'
foo(b) // error: expected an argument list of type '(T.Type)'
My assumption is that:
foo(Any.self)successfully compiles and the type placeholderTwould holdAny.Similarly, if a value of type
Any.Typeis given, theTshould be resolved intoAny
Could you explain why this code doesn't work?
swift generics metatype
swift generics metatype
asked Mar 25 at 13:20
Yonguk JeongYonguk Jeong
1845 bronze badges
1845 bronze badges
7
See Why can't I pass a Protocol.Type to a generic T.Type parameter? – forfoo(Any.self),Any.selfis treated asAny.Protocolallowing the code to compile.
– Hamish
Mar 25 at 13:29
@Hamish Thank you for the link! It really helped me understand how it works.
– Yonguk Jeong
Mar 26 at 10:26
add a comment |
7
See Why can't I pass a Protocol.Type to a generic T.Type parameter? – forfoo(Any.self),Any.selfis treated asAny.Protocolallowing the code to compile.
– Hamish
Mar 25 at 13:29
@Hamish Thank you for the link! It really helped me understand how it works.
– Yonguk Jeong
Mar 26 at 10:26
7
7
See Why can't I pass a Protocol.Type to a generic T.Type parameter? – for
foo(Any.self), Any.self is treated as Any.Protocol allowing the code to compile.– Hamish
Mar 25 at 13:29
See Why can't I pass a Protocol.Type to a generic T.Type parameter? – for
foo(Any.self), Any.self is treated as Any.Protocol allowing the code to compile.– Hamish
Mar 25 at 13:29
@Hamish Thank you for the link! It really helped me understand how it works.
– Yonguk Jeong
Mar 26 at 10:26
@Hamish Thank you for the link! It really helped me understand how it works.
– Yonguk Jeong
Mar 26 at 10:26
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55338763%2fbehavior-of-swift-any-type-and-generics%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
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55338763%2fbehavior-of-swift-any-type-and-generics%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
7
See Why can't I pass a Protocol.Type to a generic T.Type parameter? – for
foo(Any.self),Any.selfis treated asAny.Protocolallowing the code to compile.– Hamish
Mar 25 at 13:29
@Hamish Thank you for the link! It really helped me understand how it works.
– Yonguk Jeong
Mar 26 at 10:26