On Thu, Jun 20, 2013 at 5:32 AM, RickyS wrote:
What you say is consistent, as the error message (mismatched types)
re-appears when m is declared as:
const m int = 100
this is not an untyped constant, so that conversion to a suitable type rule
doesn't apply anymore.
But: This means the internal representation of a Duration (or any type) is
not truly encapsulated. If a Duration were typed as a struct that *
contained* an int64 then multiplication by a constant would not work,
either. This breaks my expectations, but my expectations were created by
using other languages.
This also means that defining Duration as:
type Duration int64
creates Duration as an alias of int64, which the language docs *deny*.
it doesn't create an alias, it's a new type that happens to share the
arithmetic rules of int64.
the rule for Go multiplication is pretty simple, just one rule, both side
must be of the same type.
constant could be either typed (like your m above), or untyped (when you
say `const m = 100`),
when an untyped constant is used in an expression, say, m *
time.Duration(100), then it must
have a type, and as the other side of the expression is typed
time.Duration, it is typed time.Duration.
this explains why:
100 * time.Duration(100) works, and also
when m is: const m = 100, m * time.Duration(100) works.
but when m is: const m int = 100, m * time.Duration(100) won't compile.
even if you declare m as: const m int64 = 100, m * time.Duration(100) is
still an error.
the only way (if you intend to give m a type at the time of declaration),
const m time.Duration = 100
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 firstname.lastname@example.org.
For more options, visit https://groups.google.com/groups/opt_out.