Skip to content

Conversation

@pao
Copy link
Member

@pao pao commented Oct 16, 2012

Looks like strpack may needs some "esc"s in it. I may get to this myself, but in the meantime I thought I'd report it.

module MyMod
import Base.*

load("strpack.jl")

type TestStruct
    int1::Int32
    float1::Float32
end
TestStruct(i, f) = TestStruct(convert(Int32, i), convert(Float32, f))

function test()
    io = IOString()
    t = TestStruct(2, 3.2)
    pack(io, t)
end

export test
end # module
julia> import MyMod.*

julia> test()
in anonymous: DataAlign not defined
 in anonymous at no file
 in pack at /home/tim/src/julia/extras/strpack.jl:334
 in test at /tmp/strpmod.jl:15

Commenting out the 3 lines that create this as a module lets everything work properly.

Presumably, strpack itself should be converted to a module. It also depends on iostring.jl and lru.jl, so I wouldn't want to make drastic changes without consulting @pao .

@pao
Copy link
Member

pao commented Oct 14, 2012

Yeah, not surprised. I was wanting to get a couple smaller modules done (hence monads, QuickCheck) before tackling this beast.

I've got through Wednesday to work on this before I disappear through the weekend. Will post here with patches once they start flowing.

@timholy
Copy link
Member Author

timholy commented Oct 14, 2012

Meanwhile, I've figured out how to solve the immediate problem. (Of course, it would probably still be a good idea to turn strpack into a module.) It turns out not to be a case of strpack needing esc, but instead an issue of when the @eval and -> inside strpack (functions struct_unpack and struct_pack) run. The workaround is to force these to run while still inside the defining module: in this example, right after defining the TestStruct default constructor, just add the line

pack(TestStruct(0, 0))

and all is well. Without that line, the pack and unpack functions don't get defined until the user calls test(), and by that point DataAlign is not available.

@pao
Copy link
Member

pao commented Oct 14, 2012

Ah, I see. You can use the Struct constructor directly to do that more cleanly: Struct(TestStruct) should accomplish the same thing.

@timholy
Copy link
Member Author

timholy commented Oct 14, 2012

Good, thanks.

@pao
Copy link
Member

pao commented Oct 16, 2012

Just starting on this, but getting patches up as promised.

Up to this point this drops the s"" syntax (as previously threatened) and creates generic pack/unpack functions per type instead of anonymous functions, since the anonymous functions didn't buy any module encapsulation.

The tricky bit so far is referring to non-exported functions in the quoted blocks using their fully-qualified names since the block must be @evaled to bring the new methods into existence.

@timholy
Copy link
Member Author

timholy commented Oct 17, 2012

Cool! Are you looking for testing/feedback at this stage, or should I wait until you declare completion?

@pao
Copy link
Member

pao commented Oct 17, 2012

I'm happy to take feedback any time, but I do still have work to do. I did quickly check that the basics work, but my original test code won't work because I killed the macro it uses. I would not be surprised if certain specific things fail right now.

Unless I get something done tonight, though, I don't expect to get back to this until next week.

@ViralBShah
Copy link
Member

Another candidate for a package? strpack is one of those things that I think would migrate back into Base, once stabilized.

@StefanKarpinski
Copy link
Member

I don't really see why this would go into Base.

@ViralBShah
Copy link
Member

I was under the impression that this would eventually make it possible to pass structs to C code. If not, then perhaps, we can close this.

@timholy
Copy link
Member Author

timholy commented Nov 27, 2012

It already does make it possible to pass structs to C code, and it is indeed very useful. But I think that this aspect of strpack (and strpack does more than just this) will eventually be superseded by built-in immutable types. The advantage of the latter is that it will be zero-copy, which strpack manifestly is not.

If extras/ is being emptied, then definitely strpack should become a package.

@dmbates
Copy link
Member

dmbates commented Nov 27, 2012

@ViralBShah See the code at the end of https://github.com/dmbates/JSparse.jl/blob/master/src/JSparse.jl The idiom
pack(struct(object)).data where struct creates the desired C struct allows you to pass structs through ccall

@timholy In your documentation you may want to note that the sequence

iostr = IOString()
pack(iostr, struct)

can be condensed to pack(struct)

@pao
Copy link
Member

pao commented Nov 27, 2012

Yes, to be clear, the ability to construct C-compatible structures is a side effect. This will still have its uses for serializing/deserializing other binary data, particularly when dealing with oddball formats or when a purpose-built serializer/deserializer isn't worth it.

I'll close this with the intent to move to its own package.

@pao pao closed this Nov 27, 2012
@timholy
Copy link
Member Author

timholy commented Nov 27, 2012

@dmbates re the documentation: it's true that it can be condensed, but I was worried that the global iostr variable might look like magic, and I felt the longer version would be clearer.

@timholy timholy mentioned this pull request Dec 2, 2012
jishnub pushed a commit that referenced this pull request Oct 19, 2025
)

Stdlib: LinearAlgebra
URL: https://github.com/JuliaLang/LinearAlgebra.jl.git
Stdlib branch: master
Julia branch: master
Old commit: d568106
New commit: 7e11b5e
Julia version: 1.13.0-DEV
LinearAlgebra version: 1.13.0
Bump invoked by: @IanButterworth
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaLang/LinearAlgebra.jl@d568106...7e11b5e

```
$ git log --oneline d568106..7e11b5e
7e11b5e Add `AbstractArray` conversions for `AbstractQ` (#1470)
52c41f7 Add missing methods to `diagind` documentation (#1473)
28ee87e Overload array constructors for BunchKaufman (#1461) (#1466)
6f73f65 Add 'eigmin'/'eigmax' methods for 'Eigen' (#1468)
7b21cab Use `Iterators.rest` within `generic_norm` to simplify code (#1459)
57ac0eb Make `parentof_applytri` fully type-stable (#1243)
880a9fe Add `_sym_uplo` to skip validation (#1441)
8d6ca14 Public function to access the `uplo` for `Symmetric`/`Hermitian` (#1440)
7a4b27e Make `dot` with Bool-arrays type-stable (#1456)
6fe77f8 Make `dot` with Bool-arrays type-stable
5af75df Remove zeroing in `similar` for `Hermitian` (#1455)
5685390 Index into diag in `Tridigaonal` * `Diagonal` (#1454)
51923a5 Forward structure-preserving broadcasting to diag for `Diagonal` (#1423)
35a4427 Iterator norm in `isapprox` for `Array`s (#1378)
98723df Structured broadcasting for UpperHessenberg (#1325)
```

Co-authored-by: IanButterworth <1694067+IanButterworth@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants