-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types
tag "argument" is not a mapping
#4532
Comments
First off, happy new year. I agree that the pseudo-mapping used in The simplest solution is to give the existing syntax its own name like "type mapping" or "typemap". While I'm open to revisiting the syntax to make it "as readable as possible", I'd argue that the current syntax is fine since to developers' eyes are already trained to read Twig mappings, JavaScript objects, JSON. The only thing I don't love is that the type needs to be quoted. I'd much prefer
I also have some reservations about the proposed syntax. For one, multiple types on one line looks really awkward and (arguably) less readable than the current syntax:
Maybe you intend to require one type per line? I actually like one-liners, even though I exclusively use them for single variables as a convention. Changing the syntax now would also be BC-breaking, unless we keep but deprecate the existing syntax. That should be possible by detecting whether the mapping starts with I realize that the docs specifically say the tag is experimental, but I for one am not looking forward to changing 289 tags in our code base, and more in TwigQI's tests. @Haehnchen would have to change the Symfony plugin. If those changes don't align in time, developer UX suffers temporarily. All in all, I'm not in favor of changing the syntax. |
The fact that this discussion happens after the tag has been added to the language is more concerning than the formalism of the tag itself. Either Twing is a typed language; or it is not. Anything in between doesn't make any sense, especially in the context of a templating language where the data is partially provided by the template executor - which can be of any imaginable form. |
@ericmorand some discussion happened before the merge as well. But once being able to use it, we get additional feedback that we don't get by just reading a spec. |
That's exactly the reason why it was marked as experimental back then.
That's just the wrong way to think about it. I agree with you that fundamentally, Twig is not a typed language. Its main goal it to abstract the real types. But, at the same time, if some developers want something more strict, why not give them the tools to do so? It's entirely optional. And if it allows for some performance optimizations when needed, it's great. |
The bare minimum is to update the docs for sure. See #4535
You're correct. For one-liners, using
We marked the tag as experimental for a reason ;) |
This I really like! It cuts down superfluous syntax while keeping backwards-compatibility. I wouldn't even mind deprecating the |
It's also the only thing that bothers me. In phpdoc, as in typescript, there are no quotes around type names. Quotes are reserved for lists of possible values (enum). |
The reason for the quotes is to allow things like |
It's more of a parser issue then? |
I think so, if I remember correctly. At the very least, Twig would have to standardize the syntax for types. The community didn't reach consensus on that. See #4256. |
This is something i'd love to have! {% types
tag: 'button|link'
external?: bool,
nofollow?: bool,
disabled?: 'disabled'
# ...
%} |
@smnandre your example showing A first-class syntax for types is hard to achieve in Twig, because token parsers have to work with tokens in the token stream produced by the Lexer, which don't play well with the need for types. And this would also require Twig to define a spec for the type syntax, which is a lot more work. {% types
tag: '"button|link"' # or '"button"|"link"' if you intended a union of 2 values
external?: 'bool',
nofollow?: 'bool',
disabled?: '"disabled"'
# ...
%} |
I'd never, ever, ever wish for Twig to become anything like TypeScript :)) (and i know you were using an example to prove your point, I just can't resist an opportunity to tell my relative "appreciation" of TS)
I sadly have experienced it more than once, working on either the props tags or Twig component parser/lexer/nodes.. 😪
This is why I purely added this on a DX level / dreaming out loud. Working for some times with front-end developers, I can assure you this would have helped a lot of times, solving the need to bring Enum here (a very very bad idea -- as I see it), or any constants/PHP stuff from the "domain" side. A template/component/partial (different words for the same concept, in the end..) that could explicitely list possible values, types, defaults**.... this could change the game. (**and yeah i'm listing OptionResolver features i know) But in the end i'm asking for nothing, nor demanding any change :) I really just wrote what I had in mind reading this, in a "what if" mood ;) |
While working a bit with types, I realized that I don't like the way we declare types via the
types
tag:types
uses a mapping, which is not true:foo?
{}
and,
look more like noise to me as it makes the syntax more verbose for no good reasons{% type some_mapping_declared_with_set %}
So, I'd like to get rid of this concept of a pseudo mapping.
For all tags, we're trying to have the "most possible readable syntax". This is highly subjective, but what about something more similar to what we're doing for other tags like:
I'm not 100% confident that my proposal is the best one, but at least, it's a baseline for discussion.
/cc @drjayvee @ruudk
The text was updated successfully, but these errors were encountered: