{"id":4097,"date":"2023-03-22T09:32:12","date_gmt":"2023-03-22T08:32:12","guid":{"rendered":"https:\/\/gdksoftware.com\/knowledgebase\/delphi-and-inline-var-declarations"},"modified":"2023-03-22T13:57:43","modified_gmt":"2023-03-22T12:57:43","slug":"declaracoes-delphi-e-inlive-var","status":"publish","type":"knowledge","link":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var","title":{"rendered":"Declara\u00e7\u00f5es Delphi e Inline var"},"content":{"rendered":"<p>Desde Delphi 10.3, voc\u00ea tem a possibilidade em Delphi de declarar vari\u00e1veis em linha diretamente em seu c\u00f3digo. N\u00e3o est\u00e1vamos acostumados a isso em Delphi, ent\u00e3o voc\u00ea pode achar isso um pouco confuso. Mas tem algumas vantagens.<\/p>\n<h2>Declara\u00e7\u00f5es de escopo<\/h2>\n<p>Em primeiro lugar, faz com que as vari\u00e1veis sejam escopadas. Portanto, elas s\u00f3 funcionam no escopo onde s\u00e3o declaradas. Elas tamb\u00e9m s\u00f3 est\u00e3o dispon\u00edveis, \u00e9 claro, ap\u00f3s o momento em que s\u00e3o declaradas. Isto reduz bastante a chance de erros. \u00c9 menos prov\u00e1vel que voc\u00ea reutilize uma vari\u00e1vel com a chance de inicializa\u00e7\u00e3o incorreta. A refatora\u00e7\u00e3o tamb\u00e9m facilita a c\u00f3pia do c\u00f3digo para outra fun\u00e7\u00e3o.<\/p>\n<p>Ao mover o c\u00f3digo em uma fun\u00e7\u00e3o, voc\u00ea tamb\u00e9m pode ver imediatamente que outras inicializa\u00e7\u00f5es voc\u00ea precisa levar consigo.<br \/>\nNo exemplo de c\u00f3digo abaixo, voc\u00ea pode ver como isto funciona. Suponha que voc\u00ea mova a parte se Cliente.IsNew a partir daqui, voc\u00ea ver\u00e1 automaticamente (atrav\u00e9s do Code Insight, mas tamb\u00e9m atrav\u00e9s da compila\u00e7\u00e3o) que a vari\u00e1vel Cliente \u00e9 desconhecida. Este n\u00e3o \u00e9 o caso com a declara\u00e7\u00e3o cl\u00e1ssica.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">if<\/span> IsCustomer <span style=\"color: #008800; font-weight: bold;\">then<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">begin<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> Customer <span style=\"color: #333333;\">:=<\/span> Order<span style=\"color: #333333;\">.<\/span>Customer<span style=\"color: #333333;\">;<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">if<\/span> Customer<span style=\"color: #333333;\">.<\/span>IsNew <span style=\"color: #008800; font-weight: bold;\">then<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">begin<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> DisplayName <span style=\"color: #333333;\">:=<\/span> Customer<span style=\"color: #333333;\">.<\/span>CompanyName <span style=\"color: #333333;\">+<\/span> Customer<span style=\"color: #333333;\">.<\/span>Name<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #333333;\">...<\/span>\r\n    SendEmail(Customer<span style=\"color: #333333;\">,<\/span> DisplayName)<span style=\"color: #333333;\">;<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">end<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">end<\/span><span style=\"color: #333333;\">;<\/span>\r\n<\/pre>\n<\/div>\n<h2><\/h2>\n<h2>Reconhecimento de tipo<\/h2>\n<p>Outra vantagem \u00e9 o reconhecimento autom\u00e1tico do tipo. Portanto, voc\u00ea n\u00e3o precisa mais indicar de que tipo uma vari\u00e1vel \u00e9. Isto tamb\u00e9m reduz a necessidade de incluir uma unidade no arquivo no qual voc\u00ea est\u00e1 trabalhando. O Code Insight tamb\u00e9m reconhece o tipo, assim voc\u00ea pode simplesmente continuar trabalhando com ele.<\/p>\n<p>Suponha que voc\u00ea tenha as seguintes unidades:<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">unit<\/span> DataModel<span style=\"color: #333333;\">.<\/span>Customer<span style=\"color: #333333;\">;<\/span> \r\n\r\n<span style=\"color: #008800; font-weight: bold;\">interface<\/span> \r\n\r\n<span style=\"color: #008800; font-weight: bold;\">type<\/span> \r\n  ICustomer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">interface<\/span> \r\n    [<span style=\"background-color: #fff0f0;\">'GUID'<\/span>] \r\n    \r\n    <span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">CompanyName<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">string<\/span><span style=\"color: #333333;\">;<\/span> \r\n    <span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #003388; font-weight: bold;\">Name<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">string<\/span><span style=\"color: #333333;\">;<\/span> \r\n  <span style=\"color: #008800; font-weight: bold;\">end<\/span><span style=\"color: #333333;\">;<\/span>\r\n<\/pre>\n<\/div>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">unit<\/span> DataModel<span style=\"color: #333333;\">.<\/span>CustomerOrder<span style=\"color: #333333;\">;<\/span>   \r\n\r\n<span style=\"color: #008800; font-weight: bold;\">interface<\/span>  \r\n\r\n<span style=\"color: #008800; font-weight: bold;\">uses<\/span> DataModel<span style=\"color: #333333;\">.<\/span>Customer<span style=\"color: #333333;\">;<\/span>  \r\n \r\n<span style=\"color: #008800; font-weight: bold;\">type<\/span>  \r\n  ICustomerOrder <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">interface<\/span>  \r\n    [<span style=\"background-color: #fff0f0;\">'GUID'<\/span>] \r\n     \r\n    <span style=\"color: #008800; font-weight: bold;\">function<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Customer<\/span><span style=\"color: #333333;\">:<\/span> ICustomer<span style=\"color: #333333;\">;<\/span>  \r\n<span style=\"color: #008800; font-weight: bold;\">end<\/span><span style=\"color: #333333;\">;<\/span>\r\n<\/pre>\n<\/div>\n<p>No primeiro exemplo, utilizamos isto da mesma forma:<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> Customer <span style=\"color: #333333;\">:=<\/span> Order<span style=\"color: #333333;\">.<\/span>Customer<span style=\"color: #333333;\">;<\/span> <span style=\"color: #888888;\">\/\/ Returns a ICustomer<\/span>\r\n<\/pre>\n<\/div>\n<p>Quando uma fun\u00e7\u00e3o retorna um tipo espec\u00edfico (como uma interface neste exemplo), n\u00e3o \u00e9 necess\u00e1rio definir esta vari\u00e1vel ou incluir a unidade.<\/p>\n<p>Se voc\u00ea usar interfaces, h\u00e1 uma exce\u00e7\u00e3o a esta regra. E isto \u00e9 quando voc\u00ea cria um objeto e quer us\u00e1-lo como uma vari\u00e1vel de interface. Ent\u00e3o voc\u00ea tem que declarar o tipo tamb\u00e9m, caso contr\u00e1rio a vari\u00e1vel \u00e9 automaticamente digitada como a variante `T&#8230;`.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> Customer<span style=\"color: #333333;\">:<\/span> ICustomer <span style=\"color: #333333;\">:=<\/span> TCustomer<span style=\"color: #333333;\">.<\/span>Create<span style=\"color: #333333;\">;<\/span>\r\n<\/pre>\n<\/div>\n<h2><\/h2>\n<h2>Mais f\u00e1cil em loops<\/h2>\n<p>As vari\u00e1veis que voc\u00ea declara para um loop tamb\u00e9m podem ser declaradas diretamente em linha. Isto geralmente \u00e9 melhor, porque voc\u00ea sempre declara a vari\u00e1vel especificamente para um la\u00e7o. Mais uma vez, isso evita que voc\u00ea use uma vari\u00e1vel de loop ap\u00f3s seu c\u00f3digo de loop e potencialmente criando problemas com ela.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #008800; font-weight: bold;\">var<\/span> i <span style=\"color: #333333;\">:=<\/span> o <span style=\"color: #008800; font-weight: bold;\">to<\/span> List<span style=\"color: #333333;\">.<\/span>Count <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #008800; font-weight: bold;\">do<\/span>\r\n  <span style=\"color: #333333;\">...<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #008800; font-weight: bold;\">var<\/span> Customer <span style=\"color: #008800; font-weight: bold;\">in<\/span> MailingList<span style=\"color: #333333;\">.<\/span>Customers <span style=\"color: #008800; font-weight: bold;\">do<\/span>\r\n  <span style=\"color: #333333;\">...<\/span>\r\n<\/pre>\n<\/div>\n<h2><\/h2>\n<h2>Uso em Delphi 10.3<\/h2>\n<p>Embora a declara\u00e7\u00e3o de vari\u00e1veis em linha seja implementada em Delphi 10.3, o Code Insight n\u00e3o a reconhece. No entanto, o compilador o reconhece. Portanto, em Delphi 10.3, n\u00e3o se deixe enganar pela linha vermelha abaixo de seu c\u00f3digo, ele tamb\u00e9m funciona l\u00e1.<\/p>\n","protected":false},"featured_media":0,"parent":0,"template":"","class_list":["post-4097","knowledge","type-knowledge","status-publish","hentry","knowledge-category-arquivo-delphi","knowledge-category-delphi-versions"],"acf":{"author":1384,"type_hero":"compact","hero_image":4092,"hero_image_position":"","hero_title":"Declara\u00e7\u00f5es Delphi e Inline var","hero_content":"","hero_link":null,"hero_show_h1":false},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Declara\u00e7\u00f5es Delphi e Inline var - GDK Software<\/title>\n<meta name=\"description\" content=\"Desde Delphi 10.3, voc\u00ea tem a possibilidade em Delphi de declarar vari\u00e1veis em linha diretamente em seu c\u00f3digo. Voc\u00ea pode achar isso um pouco confuso, mas podemos te ajudar!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Declara\u00e7\u00f5es Delphi e Inline var\" \/>\n<meta property=\"og:description\" content=\"Desde Delphi 10.3, voc\u00ea tem a possibilidade em Delphi de declarar vari\u00e1veis em linha diretamente em seu c\u00f3digo. Voc\u00ea pode achar isso um pouco confuso, mas podemos te ajudar!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var\" \/>\n<meta property=\"og:site_name\" content=\"GDK Software\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-22T12:57:43+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. tempo de leitura\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/declaracoes-delphi-e-inlive-var\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/declaracoes-delphi-e-inlive-var\",\"name\":\"Declara\u00e7\u00f5es Delphi e Inline var - GDK Software\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br#website\"},\"datePublished\":\"2023-03-22T08:32:12+00:00\",\"dateModified\":\"2023-03-22T12:57:43+00:00\",\"description\":\"Desde Delphi 10.3, voc\u00ea tem a possibilidade em Delphi de declarar vari\u00e1veis em linha diretamente em seu c\u00f3digo. Voc\u00ea pode achar isso um pouco confuso, mas podemos te ajudar!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/declaracoes-delphi-e-inlive-var#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/declaracoes-delphi-e-inlive-var\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/declaracoes-delphi-e-inlive-var#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowledgebase\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Delphi\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/knowledgebase-category\\\/arquivo-delphi\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Declara\u00e7\u00f5es Delphi e Inline var\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br#website\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\",\"name\":\"GDK Software\",\"description\":\"Zet de stip op je horizon\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Declara\u00e7\u00f5es Delphi e Inline var - GDK Software","description":"Desde Delphi 10.3, voc\u00ea tem a possibilidade em Delphi de declarar vari\u00e1veis em linha diretamente em seu c\u00f3digo. Voc\u00ea pode achar isso um pouco confuso, mas podemos te ajudar!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var","og_locale":"pt_BR","og_type":"article","og_title":"Declara\u00e7\u00f5es Delphi e Inline var","og_description":"Desde Delphi 10.3, voc\u00ea tem a possibilidade em Delphi de declarar vari\u00e1veis em linha diretamente em seu c\u00f3digo. Voc\u00ea pode achar isso um pouco confuso, mas podemos te ajudar!","og_url":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var","og_site_name":"GDK Software","article_modified_time":"2023-03-22T12:57:43+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. tempo de leitura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var","url":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var","name":"Declara\u00e7\u00f5es Delphi e Inline var - GDK Software","isPartOf":{"@id":"https:\/\/gdksoftware.com\/pt-br#website"},"datePublished":"2023-03-22T08:32:12+00:00","dateModified":"2023-03-22T12:57:43+00:00","description":"Desde Delphi 10.3, voc\u00ea tem a possibilidade em Delphi de declarar vari\u00e1veis em linha diretamente em seu c\u00f3digo. Voc\u00ea pode achar isso um pouco confuso, mas podemos te ajudar!","breadcrumb":{"@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/declaracoes-delphi-e-inlive-var#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gdksoftware.com\/pt-br"},{"@type":"ListItem","position":2,"name":"Knowledgebase","item":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento"},{"@type":"ListItem","position":3,"name":"Delphi","item":"https:\/\/gdksoftware.com\/pt-br\/knowledgebase-category\/arquivo-delphi"},{"@type":"ListItem","position":4,"name":"Declara\u00e7\u00f5es Delphi e Inline var"}]},{"@type":"WebSite","@id":"https:\/\/gdksoftware.com\/pt-br#website","url":"https:\/\/gdksoftware.com\/pt-br","name":"GDK Software","description":"Zet de stip op je horizon","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gdksoftware.com\/pt-br?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"}]}},"_links":{"self":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/knowledge\/4097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/knowledge"}],"about":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/types\/knowledge"}],"acf:post":[{"embeddable":true,"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/team\/1384"}],"wp:attachment":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/media?parent=4097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}