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;








3















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:



  1. foo(Any.self) successfully compiles and the type placeholder T would hold Any.


  2. Similarly, if a value of type Any.Type is given, the T should be resolved into Any


Could you explain why this code doesn't work?










share|improve this question

















  • 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











  • @Hamish Thank you for the link! It really helped me understand how it works.

    – Yonguk Jeong
    Mar 26 at 10:26

















3















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:



  1. foo(Any.self) successfully compiles and the type placeholder T would hold Any.


  2. Similarly, if a value of type Any.Type is given, the T should be resolved into Any


Could you explain why this code doesn't work?










share|improve this question

















  • 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











  • @Hamish Thank you for the link! It really helped me understand how it works.

    – Yonguk Jeong
    Mar 26 at 10:26













3












3








3


1






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:



  1. foo(Any.self) successfully compiles and the type placeholder T would hold Any.


  2. Similarly, if a value of type Any.Type is given, the T should be resolved into Any


Could you explain why this code doesn't work?










share|improve this question














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:



  1. foo(Any.self) successfully compiles and the type placeholder T would hold Any.


  2. Similarly, if a value of type Any.Type is given, the T should be resolved into Any


Could you explain why this code doesn't work?







swift generics metatype






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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? – 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












  • 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











  • @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












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%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.



















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%2f55338763%2fbehavior-of-swift-any-type-and-generics%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

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

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해