A Fighter with the Eldritch Knight martial archetype gets the feature War magic at 7th level (and Improved War Magic at 18th level). The War Magic feature (PHB, p. 75) states:
Beginning at 7th level, when you use your action to cast a cantrip, you can make one weapon attack as a bonus action.
The Improved War Magic feature changes "cantrip" to "spell", allowing you to cast a leveled spell as an action and make a weapon attack as a bonus action on the same turn.
Can I use this bonus-action attack before I cast the cantrip (spell)? That is, can I state that I'm going to cast a spell with my action, and thus be granted a bonus action, then use it to make a weapon attack, and then cast the spell as my action?
The rules for bonus action timing state:
You choose when to take a bonus action during your turn, unless the bonus action's timing is specified.
I'm not sure if the wording of War Magic counts as specifying the timing.
Motivation:
If this is possible, it would be especially interesting if used together with the Eldritch Knight's 10th-level feature Eldritch Strike, granting you its bonus on your first action, instead of your second. Eldritch Strike states:
When you hit a creature with a weapon attack, that creature has disadvantage on the next saving throw it makes against a spell you cast before the end of your next turn.