Giter Site home page Giter Site logo

Comments (6)

vtjnash avatar vtjnash commented on June 18, 2024

This strikes me as a bug in the type safety of Ref.

true, this may be worth a special case for Ref. In C, the void type is more nearly Union{} in Julia. The common mapping in Julia to Void has some slightly different behaviors (such as this one).

from gsl.jl.

JeffBezanson avatar JeffBezanson commented on June 18, 2024

I don't fully agree with that; Union{} is closer to a noreturn declaration. void in C is pretty close to being a normal observable value.

This problem would occur with any type; assigning a Ptr{Int} to a Ref{Int} field would do the same thing. The only fix I can think of so far is to throw an error for converting a Ptr to a Ref. Ptr could still be a subtype of Ref. That seems to handle the contexts where Ptr can vs. can't be substituted as a Ref.

from gsl.jl.

vtjnash avatar vtjnash commented on June 18, 2024

If you declare the field type wrong, the code will be wrong. There's nothing unique to Ref there, since the same applies to any other error in field declaration. otoh, if you declare the fields correctly, then you can't substitute Ref for Ptr (since that requires an unsafe_convert), which is the intended direction for usage-safety.

void is not constructible, cannot be used as a field, and is convertible to/from all other types but cannot itself be constructed: http://programmers.stackexchange.com/a/254191
In C++, void &x would be an error because void cannot be constructed, which is more similar to how Union{} behaves in Julia. In particular, the C standard specifies that the presence of the void type is special in that it does not have a value (usage of it would be an error), but the code must still be executed for side-effects.

from gsl.jl.

JeffBezanson avatar JeffBezanson commented on June 18, 2024

I think this problem reflects some unclarity about when to use Ref instead of Ptr. The docs basically say "use Ref". Maybe all we need to do is update the docs to say "never use Ref as a struct field"? However this is a bit complicated by the fact that if you use Ref{Int} as a field type, assigning an Int to it will actually work. Assigning a Ptr to a Ref field may be wrong, but it's wrong in a very confusing way that could benefit from an error message.

from gsl.jl.

jiahao avatar jiahao commented on June 18, 2024

As I understood from discussion with @JeffBezanson and @vtjnash offline, the general rule of thumb is to use Ref{T} only in covariant context to replace the x = Array(T, 1); ccall(... &x) idiom in old Julia. In other scenarios like the return value of a ccall and a Julia type mimicking a C struct, Ptr{T} should be used instead as the proper representation of a C pointer (an integer representing a potentially useful address, lacking the address validation and GC rooting created by Ref to a Julia object).

from gsl.jl.

jiahao avatar jiahao commented on June 18, 2024

I think all reasonable actions pertaining to this issue have been taken. The Julia manual has been updated and Refs excised.

from gsl.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.