semantics of :: (type declaration)

135 views
Skip to first unread message

Mauro

unread,
Feb 12, 2014, 5:01:48 PM2/12/14
to juli...@googlegroups.com
I was wondering whether someone could clarify what the rational is to
have more subtle differences of the meaning of `::` in different
contexts than what I think is necessary.

(To give context, I've started wondering this whilst pondering the issue
#1090 "return type declarations" [0])

Post [1] gives a nice summary but basically the two different meanings
are:

1) "assert-here": asserts that expression/function-argument is of
specified type. Otherwise error.

2) "convert-in-whole-scope": this is for declaring the type of a
variable and the behavior is: check whether what is
being assigned is of the right type otherwise convert.
Throws no error (except if conversion fails).

[there is also some additional differences when in the global scope, but
let's leave those aside as they may converge towards above.]

The end-state of 1&2 is similar: the object in question is of specified
type afterwards or an error is thrown. But the effects can be quite
different.

So why not have (2) instead as "assert-in-whole-scope"? The manual
states that (2), i.e. variable type declarations, are used to write type
stable methods. But when doing that is it not better to make sure the
code runs without doing conversions? And if a conversion is needed, I
would rather want to be explicit about it and use `convert`.

Or stated differently, `f`~`ff` and `g`==`gg` but why not `g`==`ff`~`f`?
Where:

f(x::Int) = x
g(x) = (y::Int=x, y)

ff(x) = typeassert(x,Int)
gg(x) = convert(Int,x)


(Sorry, I've been asking lots of 'why is it like this?' questions
recently. Thanks for the patience!)

[0] https://github.com/JuliaLang/julia/issues/1090
Somewhat related threads:
[1] https://groups.google.com/d/msg/julia-users/-jInHvbuHt0/yefmOrDTUYwJ
[2] https://groups.google.com/d/msg/julia-dev/rPcamzpayJU/IuuFiXyDLfAJ
[3] https://groups.google.com/d/msg/julia-dev/g4oJnZhLt64/egkNc17BYccJ

Eric Davies

unread,
Feb 15, 2014, 7:43:33 PM2/15/14
to juli...@googlegroups.com
I admit this puzzles me as well, and could result in a lot of headaches when people move code from global to function scope or vice versa.
Reply all
Reply to author
Forward
0 new messages