Kazuya,

I've been following or questions for a while and the common pattern I see
is: You make it feel like we're doing homework for you.

Right now you're just throwing a bunch of questions at the group and
waiting for an answer.
For example, this question could have been something like:

"I was reading about constant and variable assignments and I don't really
understand what happens when an untyped value is assigned to a variable or
constant"
or
"How are the types defined for variables and constants when their values
are first assigned?"

It would be even better if you can elaborate more on what you've read,
and/or how you think it happens.


Now, answering your question;

https://golang.org/ref/spec#Variable_declarations

"A variable declaration creates one or more variables, binds corresponding
identifiers to them, and gives each a type and an initial value."
" If that value is an untyped constant, it is first converted to its
default type; if it is an untyped boolean value, it is first converted to
type bool."

https://golang.org/ref/spec#Constants (linked in the previous text)

"An untyped constant has a default type which is the type to which the
constant is implicitly converted in contexts where a typed value is
required,
for instance, in a short variable declaration such as i := 0 where there is
no explicit type. The default type of an untyped constant is
bool, rune, int, float64, complex128 or string respectively, depending on
whether it is a boolean, rune, integer, floating-point, complex, or string
constant."

I think that is pretty clear but just summing it up:

All variables and constants are assigned a type on declaration. If the
value assigned on declaration is untyped, then the variable or constant
will be assigned one of the following types:
bool, rune, int, float64, complex128 or string


Thank you

Francisco

On Tue, Feb 5, 2019 at 5:24 AM <alan.f...@gmail.com> wrote:
>
> Yes, I should perhaps have prefaced my previous post by saying it depends
what you mean by 'type inference' :)
>
> Rightly or wrongly, I think of it as either inferring the type of a
variable or constant from its initialization expression or, in the case of
a constant, inferring that it is an untyped constant of a certain kind -
boolean, rune, integer, floating-point, complex or string.
>
> There is, of course, no explicit way to specify that a constant is of an
untyped kind in any case.
>
> However, if you take the stricter view that type inference can only apply
to typed constants, then I'd agree with you that (a) doesn't use type
inference.
>
> Alan
>
> On Monday, February 4, 2019 at 6:17:05 PM UTC, 伊藤和也 wrote:
>>
>> I agree that (b), (c), (d) use type inference but not (a) because in
(a), both "num1" and "100" are untyped constants so they don't have types
so there is no need to use type inference.
>>
>> (a) const num1 = 100
>> (b) var num2 = num1
>>
>> (c) const num1 = int(100)
>> (d) num2 := num1
>>
>> However, if type inference is used to infer that "num1" is untyped from
untyped "100", I agree that (a) also uses type inference.
>> (a) const num1 = 100
>> 2019年2月4日月曜日 2時43分45秒 UTC+9 伊藤和也:
>>>
>>> In all the constant and variable declarations below, is type inference
used? If not, which declarations use type inference?
>>> (a) const num1 = 100
>>> (b) var num2 = num1
>>>
>>> (c) const num1 = int(100)
>>> (d) num2 := num1
>
> --
> You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to