Coder Social home page Coder Social logo

Comments (9)

mhammond avatar mhammond commented on September 28, 2024

Type aliases are complicated in Python and we use strings exactly to avoid the problem with forward references. I don't understand what change you are proposing should be made to uniffi (ie, this seems a problem with Python in general and nothing specific to uniffi?)

from uniffi-rs.

giarc3 avatar giarc3 commented on September 28, 2024

Sorry for not being clear. You said "we use strings exactly to avoid the problem with forward references" but we're getting compilation errors because we're not doing anything like that for aliases today, only for function types. But when I went down the path of dealing with the problem the same way, I ran into the "Variable not allowed in type expression" error.

So I can think of two possible ways to deal with this problem, but was hoping others could come up with something better:

  1. Implement sorting of type aliases. Ideally it would be able to automatically sort based on references to other types, but an easier solution could be a way to manually indicate ordering.
  2. Switch to typing.TypeAlias. This wouldn't work for pre-3.10 Python, which is why I was asking what the minimum supported version is today.

from uniffi-rs.

mhammond avatar mhammond commented on September 28, 2024

We support all currently supported Python versions, which IIRC, is currently 3.8+ (even though CI is currently using 3.11)

I'm afraid I still don't have a good understanding of the problem you are hitting though - would it be possible to demonstrate it by making the smallest possible patch to one of our tests? Eg, test_simple_fns has one test, test_futures has another, but similar tests in any other fixture would be great too. We'd need such a test for any potential fix anyway, so this would help get the ball rolling.

from uniffi-rs.

giarc3 avatar giarc3 commented on September 28, 2024

Sure, I opened a PR here #2068 to demonstrate the error in the proc-macro fixture, but I can move it to a different test if you'd prefer

from uniffi-rs.

mhammond avatar mhammond commented on September 28, 2024

That's perfect, thanks!

from uniffi-rs.

mhammond avatar mhammond commented on September 28, 2024

In that PR, you are trying to define a "custom type" in terms of another custom type. The custom type docs indicate that they are used to wrap a "builtin" - there's no intention they are able to wrap non-builtin types. In other words, the bug in that PR seems to be that we don't fail with a clear error about the wrapped type.

I'd like to better understand your use-case - in that example, why wouldn't your API just use CustomNewtype directly? ie, what value does ASecondCustomNewtype offer?

from uniffi-rs.

giarc3 avatar giarc3 commented on September 28, 2024

I'd like to better understand your use-case

Sure! The example in the test was a minimal reproducible example; I definitely would just use CustomNewtype in that case.

My use case is mainly around composition of types in dicts. I have several functions with similar-looking signatures that are actually distinct and like to use newtypes to help distinguish this.

For example, I'd like to do

pub struct FieldId(pub String);
pub struct EncryptedField(pub Vec<u8>);
pub struct EncryptedFields(pub HashMap<FieldId, EncryptedField>);

When I use custom_newtype! on these 3 structs, it will generate aliases in alphabetical order and EncryptedFields won't yet know what a FieldId is.

from uniffi-rs.

mhammond avatar mhammond commented on September 28, 2024

In other words, the bug in that PR seems to be that we don't fail with a clear error about the wrapped type.

Welp, I guess I jumped the gun there - we do support any type, not just "builtin" types. In the linked PR I updated the docs and added tests for some of these cases.

from uniffi-rs.

giarc3 avatar giarc3 commented on September 28, 2024

That's fantastic, thanks so much for doing that @mhammond

from uniffi-rs.

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.