Julep: Internal Properties#54
Conversation
|
Here's one flavor of possible solution, written with the access of data fields in mind (proposal 2 from JuliaLang/julia#30975) Proposal 2: Module-local lowering of field accessChange the lowering of I think this does much better on several of the stated goals; it kind of "dissolves" goals 1-2, and does far better on goal 5, as it doesn't uglify the implementation details of a module. Implementation sketch: module Mod1
export Private, Public, use_field
# @private struct Private ...
struct Private
a::Int
end
struct Public
a::Int
end
# use_field(x) = x.a
use_field(x) = Mod1.getprop(x, :a) # New lowering for x.a
## The following are auto generated
# Each module gets a default `getprop`
getprop(x, s) = getproperty(x, s)
# Each type gets a method of the module-local `getprop`
getprop(x::Private, s) = getfield(x, s)
getprop(x::Public, s) = getfield(x, s)
# Invocations of `@private struct ...` generate something like
Base.getproperty(x::Private, s::Symbol) = error("`Private.$x` is an internal field")
end
module Mod2
getprop(x, s) = getproperty(x, s) # Auto generated
using ..Mod1
# use_field(x) = x.a
use_field(x) = Mod2.getprop(x, :a)
end
julia> Mod1.use_field(Mod1.Public(1))
1
julia> Mod1.use_field(Mod1.Private(1))
1
julia> Mod2.use_field(Mod1.Public(1))
1
julia> Mod2.use_field(Mod1.Private(1))
ERROR: `Private.Main.Mod1.Private(1)` is an internal fieldWhat about cases where implementation details are shared between modules? For example, between several closely cooperating submodules within a project? This could be handled by explicitly importing CompatibilityIntroducing the above is fairly compatible with the existing mostly A transition plan during the 1.x timeframe could be to make this lowering opt-in on a per-module basis, with a compiler meta attached to the module AST. Then switch the lowering completely in 2.0. |
|
Over on slack, @StefanKarpinski notes
I'm not sure I quite understand this comment but it seems like it might resolve the strange duplication of |
|
Good to see that you're thinking about this! X-refs JuliaLang/julia#12064 and JuliaLang/julia#12069. |
|
Thanks! There's also JuliaLang/julia#30204. I've added these to the document. |
9d8e951 to
3b082ad
Compare
|
@vtjnash I just came across your PR JuliaLang/julia#22147 (and understood the content of your comment JuliaLang/julia#25750 (comment)) which has many similarities to what I was thinking about here. Nice! |
Handy link to the document:
https://github.com/JuliaLang/Juleps/blob/cjf/internal-properties/InternalProperties.md
I started this over at JuliaLang/julia#30975, but keep wanting to edit the description. I figure it might be better to put it here.
This PR is an attempt to state the problem clearly, without settling on a particular solution. I think it would be useful to write down some potential solutions (maybe in the comments section), especially for the purposes of refining what the goals should be.
A concrete proposal can be added here or in a later PR.